Use enums for LabelOptions

This commit is contained in:
Jan Böhmer 2023-06-12 23:39:30 +02:00
parent 485b35fbd4
commit 71cd4057a7
23 changed files with 329 additions and 157 deletions

View file

@ -43,6 +43,7 @@ namespace App\Services\LabelSystem;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\LabelSystem\BarcodeType;
use App\Entity\LabelSystem\LabelOptions;
use App\Services\LabelSystem\Barcodes\BarcodeContentGenerator;
use Com\Tecnick\Barcode\Barcode;
@ -91,44 +92,31 @@ final class BarcodeGenerator
{
$barcode = new Barcode();
switch ($options->getBarcodeType()) {
case 'qr':
$type = 'QRCODE';
$type = match ($options->getBarcodeType()) {
BarcodeType::NONE => null,
BarcodeType::QR => 'QRCODE',
BarcodeType::DATAMATRIX => 'DATAMATRIX',
BarcodeType::CODE39 => 'C39',
BarcodeType::CODE93 => 'C93',
BarcodeType::CODE128 => 'C128A',
default => throw new InvalidArgumentException('Unknown label type!'),
};
break;
case 'datamatrix':
$type = 'DATAMATRIX';
break;
case 'code39':
$type = 'C39';
break;
case 'code93':
$type = 'C93';
break;
case 'code128':
$type = 'C128A';
break;
case 'none':
return null;
default:
throw new InvalidArgumentException('Unknown label type!');
if ($type === null) {
return null;
}
$bobj = $barcode->getBarcodeObj($type, $this->getContent($options, $target));
return $bobj->getSvgCode();
return $barcode->getBarcodeObj($type, $this->getContent($options, $target))->getSvgCode();
}
public function getContent(LabelOptions $options, AbstractDBElement $target): ?string
{
return match ($options->getBarcodeType()) {
'qr', 'datamatrix' => $this->barcodeContentGenerator->getURLContent($target),
'code39', 'code93', 'code128' => $this->barcodeContentGenerator->get1DBarcodeContent($target),
'none' => null,
$barcode = $options->getBarcodeType();
return match (true) {
$barcode->is2D() => $this->barcodeContentGenerator->getURLContent($target),
$barcode->is1D() => $this->barcodeContentGenerator->get1DBarcodeContent($target),
$barcode === BarcodeType::NONE => null,
default => throw new InvalidArgumentException('Unknown label type!'),
};
}

View file

@ -42,6 +42,7 @@ declare(strict_types=1);
namespace App\Services\LabelSystem;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\LabelSystem\LabelSupportedElement;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
@ -55,12 +56,12 @@ use ReflectionClass;
final class LabelExampleElementsGenerator
{
public function getElement(string $type): object
public function getElement(LabelSupportedElement $type): object
{
return match ($type) {
'part' => $this->getExamplePart(),
'part_lot' => $this->getExamplePartLot(),
'storelocation' => $this->getStorelocation(),
LabelSupportedElement::PART => $this->getExamplePart(),
LabelSupportedElement::PART_LOT => $this->getExamplePartLot(),
LabelSupportedElement::STORELOCATION => $this->getStorelocation(),
default => throw new InvalidArgumentException('Unknown $type.'),
};
}

View file

@ -53,12 +53,6 @@ use InvalidArgumentException;
*/
final class LabelGenerator
{
public const CLASS_SUPPORT_MAPPING = [
'part' => Part::class,
'part_lot' => PartLot::class,
'storelocation' => Storelocation::class,
];
public const MM_TO_POINTS_FACTOR = 2.83465;
public function __construct(private readonly LabelHTMLGenerator $labelHTMLGenerator)
@ -66,9 +60,9 @@ final class LabelGenerator
}
/**
* @param object|object[] $elements An element or an array of elements for which labels should be generated
* @param object|object[] $elements An element or an array of elements for which labels should be generated
*/
public function generateLabel(LabelOptions $options, $elements): string
public function generateLabel(LabelOptions $options, object|array $elements): string
{
if (!is_array($elements) && !is_object($elements)) {
throw new InvalidArgumentException('$element must be an object or an array of objects!');
@ -98,11 +92,8 @@ final class LabelGenerator
public function supports(LabelOptions $options, object $element): bool
{
$supported_type = $options->getSupportedElement();
if (!isset(static::CLASS_SUPPORT_MAPPING[$supported_type])) {
throw new InvalidArgumentException('Supported type name of the Label options not known!');
}
return is_a($element, static::CLASS_SUPPORT_MAPPING[$supported_type]);
return is_a($element, $supported_type->getEntityClass());
}
/**

View file

@ -41,6 +41,7 @@ declare(strict_types=1);
namespace App\Services\LabelSystem;
use App\Entity\LabelSystem\LabelProcessMode;
use Symfony\Bundle\SecurityBundle\Security;
use App\Entity\Contracts\NamedElementInterface;
use App\Entity\LabelSystem\LabelOptions;
@ -64,14 +65,14 @@ final class LabelHTMLGenerator
$twig_elements = [];
if ('twig' === $options->getLinesMode()) {
if (LabelProcessMode::TWIG === $options->getProcessMode()) {
$sandboxed_twig = $this->sandboxedTwigProvider->getTwig($options);
$current_user = $this->security->getUser();
}
$page = 1;
foreach ($elements as $element) {
if (isset($sandboxed_twig, $current_user) && 'twig' === $options->getLinesMode()) {
if (isset($sandboxed_twig, $current_user) && LabelProcessMode::TWIG === $options->getProcessMode()) {
try {
$lines = $sandboxed_twig->render(
'lines',

View file

@ -42,6 +42,7 @@ declare(strict_types=1);
namespace App\Services\LabelSystem;
use App\Entity\LabelSystem\LabelProfile;
use App\Entity\LabelSystem\LabelSupportedElement;
use App\Repository\LabelProfileRepository;
use App\Services\UserSystem\UserCacheKeyGenerator;
use Doctrine\ORM\EntityManagerInterface;
@ -54,10 +55,20 @@ final class LabelProfileDropdownHelper
{
}
public function getDropdownProfiles(string $type): array
/**
* Return all label profiles for the given supported element type
* @param LabelSupportedElement|string $type
* @return array
*/
public function getDropdownProfiles(LabelSupportedElement|string $type): array
{
//Useful for the twig templates, where we use the string representation of the enum
if (is_string($type)) {
$type = LabelSupportedElement::from($type);
}
$secure_class_name = str_replace('\\', '_', LabelProfile::class);
$key = 'profile_dropdown_'.$this->keyGenerator->generateKey().'_'.$secure_class_name.'_'.$type;
$key = 'profile_dropdown_'.$this->keyGenerator->generateKey().'_'.$secure_class_name.'_'.$type->value;
/** @var LabelProfileRepository $repo */
$repo = $this->entityManager->getRepository(LabelProfile::class);

View file

@ -49,6 +49,7 @@ use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\Contracts\NamedElementInterface;
use App\Entity\Contracts\TimeStampableInterface;
use App\Entity\LabelSystem\LabelOptions;
use App\Entity\LabelSystem\LabelProcessMode;
use App\Entity\Parameters\AbstractParameter;
use App\Entity\Parts\MeasurementUnit;
use App\Entity\Parts\Part;
@ -123,7 +124,7 @@ final class SandboxedTwigProvider
public function getTwig(LabelOptions $options): Environment
{
if ('twig' !== $options->getLinesMode()) {
if (LabelProcessMode::TWIG !== $options->getProcessMode()) {
throw new InvalidArgumentException('The LabelOptions must explicitly allow twig via lines_mode = "twig"!');
}