Completed
Push — sf2.7 ( cc13d7...fb6965 )
by Laurent
04:29
created

SupplierController::sortAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/**
3
 * DefaultController controller des fournisseurs.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version   since 1.0.0
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Controller;
16
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Translation\Translator;
19
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
22
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
23
use Doctrine\ORM\QueryBuilder;
24
use AppBundle\Entity\Supplier;
25
use AppBundle\Form\Type\SupplierType;
26
27
/**
28
 * Supplier controller.
29
 *
30
 * @category Controller
31
 *
32
 * @Route("/suppliers")
33
 */
34
class SupplierController extends Controller
35
{
36
    /**
37
     * Lists all Supplier entities.
38
     *
39
     * @Route("/", name="suppliers")
40
     * @Method("GET")
41
     * @Template()
42
     */
43 View Code Duplication
    public function indexAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
44
    {
45
        $em = $this->getDoctrine()->getManager();
46
        $qb = $em->getRepository('AppBundle:Supplier')->createQueryBuilder('s');
47
        $this->addQueryBuilderSort($qb, 'supplier');
48
        $paginator = $this->get('knp_paginator')->paginate($qb, $request->query->get('page', 1), 20);
49
        
50
        return array(
51
            'paginator' => $paginator,
52
        );
53
    }
54
55
    /**
56
     * Finds and displays a Supplier entity.
57
     *
58
     * @Route("/{slug}/show", name="suppliers_show")
59
     * @Method("GET")
60
     * @Template()
61
     */
62
    public function showAction(Supplier $supplier)
63
    {
64
        $deleteForm = $this->createDeleteForm($supplier->getId(), 'suppliers_delete');
65
66
        return array(
67
            'supplier' => $supplier,
68
            'delete_form' => $deleteForm->createView(),
69
        );
70
    }
71
72
    /**
73
     * Displays a form to create a new Supplier entity.
74
     *
75
     * @Route("/new", name="suppliers_new")
76
     * @Method("GET")
77
     * @Template()
78
     */
79
    public function newAction()
80
    {
81
        $supplier = new Supplier();
82
        $form = $this->createForm(new SupplierType(), $supplier);
83
84
        return array(
85
            'supplier' => $supplier,
86
            'form'   => $form->createView(),
87
        );
88
    }
89
90
    /**
91
     * Creates a new Supplier entity.
92
     *
93
     * @Route("/create", name="suppliers_create")
94
     * @Method("POST")
95
     * @Template("AppBundle:Supplier:new.html.twig")
96
     */
97
    public function createAction(Request $request)
98
    {
99
        $supplier = new Supplier();
100
        $form = $this->createForm(new SupplierType(), $supplier);
101
        if ($form->handleRequest($request)->isValid()) {
102
            $em = $this->getDoctrine()->getManager();
103
            $em->persist($supplier);
104
            $em->flush();
105
106
            return $this->redirectToRoute('suppliers_show', array('slug' => $supplier->getSlug()));
107
        }
108
109
        return array(
110
            'supplier' => $supplier,
111
            'form'   => $form->createView(),
112
        );
113
    }
114
115
    /**
116
     * Displays a form to edit an existing Supplier entity.
117
     *
118
     * @Route("/{slug}/edit", name="suppliers_edit")
119
     * @Method("GET")
120
     * @Template()
121
     */
122
    public function editAction(Supplier $supplier)
123
    {
124
        $editForm = $this->createForm(new SupplierType(), $supplier, array(
125
            'action' => $this->generateUrl('suppliers_update', array('slug' => $supplier->getSlug())),
126
            'method' => 'PUT',
127
        ));
128
        $deleteForm = $this->createDeleteForm($supplier->getId(), 'suppliers_delete');
129
130
        return array(
131
            'supplier' => $supplier,
132
            'edit_form'   => $editForm->createView(),
133
            'delete_form' => $deleteForm->createView(),
134
        );
135
    }
136
137
    /**
138
     * Edits an existing Supplier entity.
139
     *
140
     * @Route("/{slug}/update", name="suppliers_update")
141
     * @Method("PUT")
142
     * @Template("AppBundle:Supplier:edit.html.twig")
143
     */
144
    public function updateAction(Supplier $supplier, Request $request)
145
    {
146
        $editForm = $this->createForm(new SupplierType(), $supplier, array(
147
            'action' => $this->generateUrl('suppliers_update', array('slug' => $supplier->getSlug())),
148
            'method' => 'PUT',
149
        ));
150
        if ($editForm->handleRequest($request)->isValid()) {
151
            $this->getDoctrine()->getManager()->flush();
152
            $this->addFlash('info', 'gestock.edit.ok');
153
        }
154
        $deleteForm = $this->createDeleteForm($supplier->getId(), 'suppliers_delete');
155
156
        return array(
157
            'supplier' => $supplier,
158
            'edit_form'   => $editForm->createView(),
159
            'delete_form' => $deleteForm->createView(),
160
        );
161
    }
162
163
164
    /**
165
     * Save order.
166
     *
167
     * @Route("/order/{field}/{type}", name="suppliers_sort")
168
     */
169
    public function sortAction($field, $type)
170
    {
171
        $this->setOrder('supplier', $field, $type);
172
173
        return $this->redirectToRoute('suppliers');
174
    }
175
176
    /**
177
     * @param string $name  session name
178
     * @param string $field field name
179
     * @param string $type  sort type ("ASC"/"DESC")
180
     */
181
    protected function setOrder($name, $field, $type = 'ASC')
182
    {
183
        $this->getRequest()->getSession()->set('sort.' . $name, array('field' => $field, 'type' => $type));
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
184
    }
185
186
    /**
187
     * @param  string $name
188
     * @return array
189
     */
190
    protected function getOrder($name)
191
    {
192
        $session = $this->getRequest()->getSession();
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
193
194
        return $session->has('sort.' . $name) ? $session->get('sort.' . $name) : null;
195
    }
196
197
    /**
198
     * @param QueryBuilder $qb
199
     * @param string       $name
200
     */
201 View Code Duplication
    protected function addQueryBuilderSort(QueryBuilder $qb, $name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
202
    {
203
        $alias = current($qb->getDQLPart('from'))->getAlias();
204
        if (is_array($order = $this->getOrder($name))) {
205
            $qb->orderBy($alias . '.' . $order['field'], $order['type']);
206
        }
207
    }
208
209
    /**
210
     * Deletes a Supplier entity.
211
     *
212
     * @Route("/{id}/delete", name="suppliers_delete", requirements={"id"="\d+"})
213
     * @Method("DELETE")
214
     */
215
    public function deleteAction(Supplier $supplier, Request $request)
216
    {
217
        $form = $this->createDeleteForm($supplier->getId(), 'suppliers_delete');
218
        if ($form->handleRequest($request)->isValid()) {
219
            $em = $this->getDoctrine()->getManager();
220
            $em->remove($supplier);
221
            $em->flush();
222
        }
223
224
        return $this->redirectToRoute('suppliers');
225
    }
226
227
    /**
228
     * Create Delete form
229
     *
230
     * @param integer                       $id
231
     * @param string                        $route
232
     * @return \Symfony\Component\Form\Form
233
     */
234
    protected function createDeleteForm($id, $route)
235
    {
236
        return $this->createFormBuilder(null, array('attr' => array('id' => 'delete')))
0 ignored issues
show
Bug introduced by
The method getForm() does not exist on Symfony\Component\Form\FormConfigBuilder. Did you maybe mean getFormConfig()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
237
            ->setAction($this->generateUrl($route, array('id' => $id)))
238
            ->setMethod('DELETE')
239
            ->getForm()
240
        ;
241
    }
242
243
}
244