mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-30 13:34:28 +02:00
Fixed coding style.
This commit is contained in:
parent
e9493e52ec
commit
f5d685dfd4
71 changed files with 619 additions and 531 deletions
|
@ -1,4 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
|
@ -20,24 +23,18 @@
|
|||
|
||||
namespace App\Services\LabelSystem\Barcodes;
|
||||
|
||||
|
||||
use App\Entity\Base\AbstractDBElement;
|
||||
use App\Entity\Parts\Manufacturer;
|
||||
use App\Entity\Parts\Part;
|
||||
use App\Entity\Parts\PartLot;
|
||||
use App\Entity\Parts\Storelocation;
|
||||
use App\Entity\Parts\Supplier;
|
||||
use App\Exceptions\EntityNotSupportedException;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
final class BarcodeContentGenerator
|
||||
{
|
||||
private $urlGenerator;
|
||||
|
||||
public const PREFIX_MAP = [
|
||||
Part::class => 'P',
|
||||
PartLot::class => 'L',
|
||||
Storelocation::class => 'S'
|
||||
Storelocation::class => 'S',
|
||||
];
|
||||
|
||||
private const URL_MAP = [
|
||||
|
@ -45,6 +42,7 @@ final class BarcodeContentGenerator
|
|||
PartLot::class => 'lot',
|
||||
Storelocation::class => 'location',
|
||||
];
|
||||
private $urlGenerator;
|
||||
|
||||
public function __construct(UrlGeneratorInterface $urlGenerator)
|
||||
{
|
||||
|
@ -53,7 +51,7 @@ final class BarcodeContentGenerator
|
|||
|
||||
/**
|
||||
* Generates a fixed URL to the given Element that can be embedded in a 2D code (e.g. QR code).
|
||||
* @param AbstractDBElement $target
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getURLContent(AbstractDBElement $target): string
|
||||
|
@ -69,15 +67,16 @@ final class BarcodeContentGenerator
|
|||
|
||||
/**
|
||||
* Returns a Code that can be used in a 1D barcode.
|
||||
* The return value has a format of "L0123"
|
||||
* @param AbstractDBElement $target
|
||||
* The return value has a format of "L0123".
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get1DBarcodeContent(AbstractDBElement $target): string
|
||||
{
|
||||
$prefix = $this->classToString(self::PREFIX_MAP, $target);
|
||||
$id = sprintf('%04d', $target->getID() ?? 0);
|
||||
return $prefix . $id;
|
||||
|
||||
return $prefix.$id;
|
||||
}
|
||||
|
||||
private function classToString(array $map, object $target): string
|
||||
|
@ -87,12 +86,12 @@ final class BarcodeContentGenerator
|
|||
return $map[$class];
|
||||
}
|
||||
|
||||
foreach($map as $class => $string) {
|
||||
foreach ($map as $class => $string) {
|
||||
if (is_a($target, $class)) {
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException('Unknown object class ' . get_class($target));
|
||||
throw new \InvalidArgumentException('Unknown object class '.get_class($target));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
|
@ -44,39 +47,6 @@ final class BarcodeExampleElementsGenerator
|
|||
}
|
||||
}
|
||||
|
||||
private function getStorelocation(): Storelocation
|
||||
{
|
||||
$storelocation = new Storelocation();
|
||||
$storelocation->setName('Location 1');
|
||||
$storelocation->setComment('Example comment');
|
||||
$storelocation->updatedTimestamps();
|
||||
|
||||
$parent = new Storelocation();
|
||||
$parent->setName('Parent');
|
||||
|
||||
$storelocation->setParent($parent);
|
||||
|
||||
return $storelocation;
|
||||
}
|
||||
|
||||
private function getStructuralData(string $class): AbstractStructuralDBElement
|
||||
{
|
||||
if (!is_a($class, AbstractStructuralDBElement::class, true)) {
|
||||
throw new \InvalidArgumentException('$class must be an child of AbstractStructuralDBElement');
|
||||
}
|
||||
|
||||
/** @var AbstractStructuralDBElement $parent */
|
||||
$parent = new $class();
|
||||
$parent->setName('Example');
|
||||
|
||||
/** @var AbstractStructuralDBElement $child */
|
||||
$child = new $class();
|
||||
$child->setName((new \ReflectionClass($class))->getShortName());
|
||||
$child->setParent($parent);
|
||||
|
||||
return $child;
|
||||
}
|
||||
|
||||
public function getExamplePart(): Part
|
||||
{
|
||||
$part = new Part();
|
||||
|
@ -98,7 +68,6 @@ final class BarcodeExampleElementsGenerator
|
|||
$part->setMinAmount(100);
|
||||
$part->setNeedsReview(true);
|
||||
|
||||
|
||||
return $part;
|
||||
}
|
||||
|
||||
|
@ -115,4 +84,37 @@ final class BarcodeExampleElementsGenerator
|
|||
|
||||
return $lot;
|
||||
}
|
||||
}
|
||||
|
||||
private function getStorelocation(): Storelocation
|
||||
{
|
||||
$storelocation = new Storelocation();
|
||||
$storelocation->setName('Location 1');
|
||||
$storelocation->setComment('Example comment');
|
||||
$storelocation->updatedTimestamps();
|
||||
|
||||
$parent = new Storelocation();
|
||||
$parent->setName('Parent');
|
||||
|
||||
$storelocation->setParent($parent);
|
||||
|
||||
return $storelocation;
|
||||
}
|
||||
|
||||
private function getStructuralData(string $class): AbstractStructuralDBElement
|
||||
{
|
||||
if (! is_a($class, AbstractStructuralDBElement::class, true)) {
|
||||
throw new \InvalidArgumentException('$class must be an child of AbstractStructuralDBElement');
|
||||
}
|
||||
|
||||
/** @var AbstractStructuralDBElement $parent */
|
||||
$parent = new $class();
|
||||
$parent->setName('Example');
|
||||
|
||||
/** @var AbstractStructuralDBElement $child */
|
||||
$child = new $class();
|
||||
$child->setName((new \ReflectionClass($class))->getShortName());
|
||||
$child->setParent($parent);
|
||||
|
||||
return $child;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
|
@ -20,7 +23,6 @@
|
|||
|
||||
namespace App\Services\LabelSystem\Barcodes;
|
||||
|
||||
|
||||
final class BarcodeNormalizer
|
||||
{
|
||||
private const PREFIX_TYPE_MAP = [
|
||||
|
@ -31,8 +33,8 @@ final class BarcodeNormalizer
|
|||
|
||||
/**
|
||||
* Parses barcode content and normalizes it.
|
||||
* Returns an array in the format ['part', 1]: First entry contains element type, second the ID of the element
|
||||
* @param string $input
|
||||
* Returns an array in the format ['part', 1]: First entry contains element type, second the ID of the element.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function normalizeBarcodeContent(string $input): array
|
||||
|
@ -53,9 +55,10 @@ final class BarcodeNormalizer
|
|||
$prefix = $matches[1];
|
||||
$id = (int) $matches[2];
|
||||
|
||||
if (!isset(self::PREFIX_TYPE_MAP[$prefix])) {
|
||||
throw new \InvalidArgumentException('Unknown prefix ' . $prefix);
|
||||
if (! isset(self::PREFIX_TYPE_MAP[$prefix])) {
|
||||
throw new \InvalidArgumentException('Unknown prefix '.$prefix);
|
||||
}
|
||||
|
||||
return [self::PREFIX_TYPE_MAP[$prefix], $id];
|
||||
}
|
||||
|
||||
|
@ -64,9 +67,10 @@ final class BarcodeNormalizer
|
|||
$prefix = $matches[1];
|
||||
$id = (int) $matches[2];
|
||||
|
||||
if (!isset(self::PREFIX_TYPE_MAP[$prefix])) {
|
||||
throw new \InvalidArgumentException('Unknown prefix ' . $prefix);
|
||||
if (! isset(self::PREFIX_TYPE_MAP[$prefix])) {
|
||||
throw new \InvalidArgumentException('Unknown prefix '.$prefix);
|
||||
}
|
||||
|
||||
return [self::PREFIX_TYPE_MAP[$prefix], $id];
|
||||
}
|
||||
|
||||
|
@ -80,7 +84,6 @@ final class BarcodeNormalizer
|
|||
return ['part', (int) $matches[1]];
|
||||
}
|
||||
|
||||
|
||||
throw new \InvalidArgumentException('Unknown barcode format!');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
|
@ -20,7 +23,6 @@
|
|||
|
||||
namespace App\Services\LabelSystem\Barcodes;
|
||||
|
||||
|
||||
use App\Entity\Parts\PartLot;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\EntityNotFoundException;
|
||||
|
@ -31,7 +33,6 @@ final class BarcodeRedirector
|
|||
private $urlGenerator;
|
||||
private $em;
|
||||
|
||||
|
||||
public function __construct(UrlGeneratorInterface $urlGenerator, EntityManagerInterface $entityManager)
|
||||
{
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
|
@ -39,10 +40,13 @@ final class BarcodeRedirector
|
|||
}
|
||||
|
||||
/**
|
||||
* Determines the URL to which the user should be redirected, when scanning a QR code
|
||||
* @param string $type The type of the element that was scanned (e.g. 'part', 'lot', etc.)
|
||||
* @param int $id The ID of the element that was scanned
|
||||
* Determines the URL to which the user should be redirected, when scanning a QR code.
|
||||
*
|
||||
* @param string $type The type of the element that was scanned (e.g. 'part', 'lot', etc.)
|
||||
* @param int $id The ID of the element that was scanned
|
||||
*
|
||||
* @return string The URL to which should be redirected.
|
||||
*
|
||||
* @throws EntityNotFoundException
|
||||
*/
|
||||
public function getRedirectURL(string $type, int $id): string
|
||||
|
@ -53,7 +57,7 @@ final class BarcodeRedirector
|
|||
case 'lot':
|
||||
//Try to determine the part to the given lot
|
||||
$lot = $this->em->find(PartLot::class, $id);
|
||||
if ($lot === null) {
|
||||
if (null === $lot) {
|
||||
throw new EntityNotFoundException();
|
||||
}
|
||||
|
||||
|
@ -63,7 +67,7 @@ final class BarcodeRedirector
|
|||
return $this->urlGenerator->generate('part_list_store_location', ['id' => $id]);
|
||||
|
||||
default:
|
||||
throw new \InvalidArgumentException('Unknown $type: ' . $type);
|
||||
throw new \InvalidArgumentException('Unknown $type: '.$type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue