. */ namespace App\Services\LabelSystem; use App\Entity\Parts\PartLot; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityNotFoundException; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; class BarcodeParser { protected $urlGenerator; protected $em; public function __construct(UrlGeneratorInterface $urlGenerator, EntityManagerInterface $entityManager) { $this->urlGenerator = $urlGenerator; $this->em = $entityManager; } /** * 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 getQRRedirectTarget(string $type, int $id): string { switch ($type) { case 'part': return $this->urlGenerator->generate('app_part_show', ['id' => $id]); case 'lot': //Try to determine the part to the given lot $lot = $this->em->find(PartLot::class, $id); if ($lot === null) { throw new EntityNotFoundException(); } return $this->urlGenerator->generate('app_part_show', ['id' => $lot->getPart()->getID()]); default: throw new \InvalidArgumentException('Unknown $type: ' . $type); } } }