LabelController::indexAction()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace Loevgaard\PakkelabelsBundle\Controller;
4
5
use Loevgaard\PakkelabelsBundle\Entity\Label;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\HttpFoundation\BinaryFileResponse;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
14
/**
15
 * @Route("/label")
16
 */
17
class LabelController extends Controller
18
{
19
    /**
20
     * @Method("GET")
21
     * @Route("", name="loevgaard_pakkelabels_label_index")
22
     *
23
     * @param Request $request
24
     *
25
     * @return Response
26
     */
27
    public function indexAction(Request $request)
28
    {
29
        $repos = $this->get('loevgaard_pakkelabels.label_repository');
30
31
        $filterForm = $this->createForm('Loevgaard\PakkelabelsBundle\Form\FilterLabelType');
32
33
        $qb = $repos->getQueryBuilder();
34
35
        if ($request->query->has($filterForm->getName())) {
36
            // manually bind values from the request
37
            $filterForm->submit($request->query->get($filterForm->getName()));
38
39
            // build the query from the given form object
40
            $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($filterForm, $qb);
41
        }
42
43
        /** @var Label[] $labels */
44
        $labels = $repos->findAllWithPaging($request->query->getInt('page', 1), 100, [], $qb);
45
46
        return $this->render('@LoevgaardPakkelabels/label/index.html.twig', [
47
            'labels' => $labels,
48
            'filterForm' => $filterForm->createView()
49
        ]);
50
    }
51
52
    /**
53
     * @Method("GET")
54
     * @Route("/{id}/show", name="loevgaard_pakkelabels_label_show")
55
     *
56
     * @param Label $label
57
     *
58
     * @return Response
59
     */
60
    public function showAction(Label $label): Response
61
    {
62
        return $this->render('@LoevgaardPakkelabels/label/show.html.twig', [
63
            'label' => $label,
64
        ]);
65
    }
66
67
    /**
68
     * @Method("GET")
69
     * @Route("/{id}/image", name="loevgaard_pakkelabels_label_image")
70
     *
71
     * @param Label $label
72
     *
73
     * @return BinaryFileResponse
74
     */
75
    public function imageAction(Label $label): BinaryFileResponse
76
    {
77
        $labelFactory = $this->get('loevgaard_pakkelabels.label_file_factory');
78
79
        // @todo remember to catch exception, maybe create a new exception class
80
        $labelFile = $labelFactory->read($label, true);
81
82
        return new BinaryFileResponse($labelFile);
83
    }
84
85
    /**
86
     * @Method("GET")
87
     * @Route("/{id}/reset-status", name="loevgaard_pakkelabels_label_reset_status")
88
     *
89
     * @param Label $label
90
     *
91
     * @return RedirectResponse
92
     */
93
    public function resetStatusAction(Label $label): RedirectResponse
94
    {
95
        $labelRepository = $this->get('loevgaard_pakkelabels.label_repository');
96
        $label->resetStatus();
97
        $labelRepository->save($label);
98
99
        return $this->redirectToRoute('loevgaard_pakkelabels_label_index');
100
    }
101
}
102