. */ namespace App\Services\LabelSystem; use App\Entity\Contracts\NamedElementInterface; use App\Entity\LabelSystem\LabelOptions; use App\Services\ElementTypeNameGenerator; use App\Services\LabelSystem\Barcodes\BarcodeContentGenerator; use Twig\Environment; class LabelHTMLGenerator { protected $twig; protected $elementTypeNameGenerator; protected $replacer; protected $barcodeGenerator; public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator, LabelTextReplacer $replacer, Environment $twig, BarcodeGenerator $barcodeGenerator) { $this->twig = $twig; $this->elementTypeNameGenerator = $elementTypeNameGenerator; $this->replacer = $replacer; $this->barcodeGenerator = $barcodeGenerator; } public function getLabelHTML(LabelOptions $options, array $elements): string { if (empty($elements)) { throw new \InvalidArgumentException('$elements must not be empty'); } $twig_elements = []; foreach ($elements as $element) { $twig_elements[] = [ 'element' => $element, 'lines' => $this->replacer->replace($options->getLines(), $element), 'barcode' => $this->barcodeGenerator->generateSVG($options, $element), 'barcode_content' => $this->barcodeGenerator->getContent($options, $element), ]; } return $this->twig->render('LabelSystem/labels/base_label.html.twig', [ 'meta_title' => $this->getPDFTitle($options, $elements[0]), 'elements' => $twig_elements, 'options' => $options, ]); } protected function getPDFTitle(LabelOptions $options, object $element) { if ($element instanceof NamedElementInterface) { return $this->elementTypeNameGenerator->getTypeNameCombination($element, false); } return 'Part-DB label'; } }