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!'),
};
}