1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\PakkelabelsBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Loevgaard\PakkelabelsBundle\Entity\Label; |
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
10
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
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
|
|
View Code Duplication |
public function indexAction(Request $request) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$paginator = $this->get('knp_paginator'); |
30
|
|
|
|
31
|
|
|
/** @var EntityManager $em */ |
32
|
|
|
$em = $this->get('doctrine')->getManager(); |
33
|
|
|
|
34
|
|
|
$qb = $em->createQueryBuilder(); |
35
|
|
|
$qb->select('l') |
36
|
|
|
->from('LoevgaardPakkelabelsBundle:Label', 'l') |
37
|
|
|
->orderBy('l.id', 'desc') |
38
|
|
|
; |
39
|
|
|
|
40
|
|
|
/** @var Label[] $labels */ |
41
|
|
|
$labels = $paginator->paginate( |
42
|
|
|
$qb, |
43
|
|
|
$request->query->getInt('page', 1), |
44
|
|
|
100 |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
return $this->render('@LoevgaardPakkelabels/label/index.html.twig', [ |
48
|
|
|
'labels' => $labels, |
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->create($label, true); |
81
|
|
|
|
82
|
|
|
return new BinaryFileResponse($labelFile); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.