Passed
Push — master ( ec0d02...c0f595 )
by Jan
06:31 queued 10s
created

ScanController::dialog()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 23
rs 9.4555
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace App\Controller;
22
23
24
use App\Form\LabelSystem\ScanDialogType;
25
use App\Services\LabelSystem\Barcodes\BarcodeRedirector;
26
use App\Services\LabelSystem\Barcodes\BarcodeNormalizer;
27
use Doctrine\ORM\EntityNotFoundException;
28
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
29
use Symfony\Component\Form\FormError;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\HttpFoundation\Response;
32
use Symfony\Component\Routing\Annotation\Route;
33
34
/**
35
 * @Route("/scan")
36
 * @package App\Controller
37
 */
38
class ScanController extends AbstractController
39
{
40
    protected $barcodeParser;
41
    protected $barcodeNormalizer;
42
43
    public function __construct(BarcodeRedirector $barcodeParser, BarcodeNormalizer $barcodeNormalizer)
44
    {
45
        $this->barcodeParser = $barcodeParser;
46
        $this->barcodeNormalizer = $barcodeNormalizer;
47
    }
48
49
    /**
50
     * @Route("", name="scan_dialog")
51
     */
52
    public function dialog(Request $request): Response
53
    {
54
        $this->denyAccessUnlessGranted('@tools.label_scanner');
55
56
        $form = $this->createForm(ScanDialogType::class);
57
        $form->handleRequest($request);
58
59
        if ($form->isSubmitted() && $form->isValid()) {
60
            $input = $form['input']->getData();
61
            try {
62
                [$type, $id] = $this->barcodeNormalizer->normalizeBarcodeContent($input);
63
                try {
64
                    return $this->redirect($this->barcodeParser->getRedirectURL($type, $id));
65
                } catch (EntityNotFoundException $exception) {
66
                    $this->addFlash('success', 'scan.qr_not_found');
67
                }
68
            } catch (\InvalidArgumentException $exception) {
69
                $this->addFlash('error', 'scan.format_unknown');
70
            }
71
        }
72
73
        return $this->render('LabelSystem/Scanner/dialog.html.twig', [
74
            'form' => $form->createView(),
75
        ]);
76
    }
77
78
    /**
79
     * The route definition for this action is done in routes.yaml, as it does not use the _locale prefix as the other routes
80
     * @param  string  $type
81
     * @param  int  $id
82
     */
83
    public function scanQRCode(string $type, int $id): Response
84
    {
85
        try {
86
            $this->addFlash('success', 'scan.qr_success');
87
            return $this->redirect($this->barcodeParser->getRedirectURL($type, $id));
88
        } catch (EntityNotFoundException $exception) {
89
            $this->addFlash('success', 'scan.qr_not_found');
90
            return $this->redirectToRoute('homepage');
91
        }
92
    }
93
}