. */ namespace App\Controller; use App\Form\LabelSystem\ScanDialogType; use App\Services\LabelSystem\Barcodes\BarcodeRedirector; use App\Services\LabelSystem\Barcodes\BarcodeNormalizer; use Doctrine\ORM\EntityNotFoundException; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\FormError; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; /** * @Route("/scan") * @package App\Controller */ class ScanController extends AbstractController { protected $barcodeParser; protected $barcodeNormalizer; public function __construct(BarcodeRedirector $barcodeParser, BarcodeNormalizer $barcodeNormalizer) { $this->barcodeParser = $barcodeParser; $this->barcodeNormalizer = $barcodeNormalizer; } /** * @Route("/", name="scan_dialog") */ public function dialog(Request $request): Response { $this->denyAccessUnlessGranted('@tools.label_scanner'); $form = $this->createForm(ScanDialogType::class); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $input = $form['input']->getData(); try { [$type, $id] = $this->barcodeNormalizer->normalizeBarcodeContent($input); try { return $this->redirect($this->barcodeParser->getRedirectURL($type, $id)); } catch (EntityNotFoundException $exception) { $this->addFlash('success', 'scan.qr_not_found'); } } catch (\InvalidArgumentException $exception) { $this->addFlash('error', 'scan.format_unknown'); } } return $this->render('LabelSystem/Scanner/dialog.html.twig', [ 'form' => $form->createView(), ]); } /** * The route definition for this action is done in routes.yaml, as it does not use the _locale prefix as the other routes * @param string $type * @param int $id */ public function scanQRCode(string $type, int $id): Response { try { $this->addFlash('success', 'scan.qr_success'); return $this->redirect($this->barcodeParser->getRedirectURL($type, $id)); } catch (EntityNotFoundException $exception) { $this->addFlash('success', 'scan.qr_not_found'); return $this->redirectToRoute('homepage'); } } }