Completed
Push — master ( 64ecaf...80f2c6 )
by Laurent
02:52
created

AbstractController::abstractDeleteAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
/**
3
 * AbstractController controller des méthodes communes.
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\HttpFoundation\Session\Session;
19
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
20
use Doctrine\ORM\QueryBuilder;
21
22
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
23
24
/**
25
 * Abstract controller.
26
 *
27
 * @category Controller
28
 */
29
abstract class AbstractController extends Controller
30
{
31
    /**
32
     * Lists all items entity.
33
     *
34
     * @param string $entityName Name of Entity
35
     * @param \Symfony\Component\HttpFoundation\Request $request Sort request
36
     * @return array
37
     */
38
    public function abstractIndexAction($entityName, Request $request = null)
39
    {
40
        $etm = $this->getDoctrine()->getManager();
41
        $paginator = '';
42
        $entities = $this->getEntity($entityName, $etm);
43
        
44
        if ($request !== null) {
45
            $item = $this->container->getParameter('knp_paginator.page_range');
46
            $this->addQueryBuilderSort($entities, strtolower($entityName));
47
            $paginator = $this->get('knp_paginator')->paginate($entities, $request->query->get('page', 1), $item);
48
        }
49
50
        return array('entities'  => $entities, 'ctEntity' => count($entities), 'paginator' => $paginator,);
51
    }
52
53
    /**
54
     * Get the entity
55
     *
56
     * @param string $entityName Name of Entity
57
     * @param \Doctrine\Common\Persistence\ObjectManager $etm ObjectManager instances
58
     * @return type
59
     */
60
    protected function getEntity($entityName, $etm)
61
    {
62
        switch ($entityName) {
63
            case 'Article':
64
                $entities = $etm->getRepository('AppBundle:'.$entityName)->getArticles();
65
                break;
66
            case 'Supplier':
67
                $entities = $etm->getRepository('AppBundle:'.$entityName)->getSuppliers();
68
                break;
69
            case 'User':
70
                $entities = $etm->getRepository('AppBundle:'.$entityName)->getUsers();
71
                break;
72
            case 'FamilyLog':
73
                $entities = $etm->getRepository('AppBundle:'.$entityName)->childrenHierarchy();
74
                break;
75
            case 'UnitStorage':
76
                $entities = $etm->getRepository('AppBundle:'.$entityName)->createQueryBuilder('u');
77
                break;
78
            default:
79
                $entities = $etm->getRepository('AppBundle:'.$entityName)->findAll();
80
        }
81
        return $entities;
82
    }
83
84
    /**
85
     * Finds and displays an item entity.
86
     *
87
     * @param Object $entity     Entity
88
     * @param string $entityName Name of Entity
89
     * @return array
90
     */
91
    public function abstractShowAction($entity, $entityName)
92
    {
93
        $deleteForm = $this->createDeleteForm($entity->getId(), $entityName.'_delete');
94
95
        return array(
96
            $entityName => $entity,
97
            'delete_form' => $deleteForm->createView(),
98
        );
99
    }
100
101
    /**
102
     * Displays a form to create a new item entity.
103
     *
104
     * @param string      $entity     Entity
105
     * @param string      $entityPath Path of Entity
106
     * @param string      $typePath   Path of FormType
107
     * @param string|null $options    Options of Form
108
     * @return array
109
     */
110
    public function abstractNewAction($entity, $entityPath, $typePath, $options = null)
111
    {
112
        $etm = $this->getDoctrine()->getManager();
113
        $ctEntity = count($etm->getRepository('AppBundle:'.$entity)->findAll());
114
        
115
        if ($entity === 'Company' || $entity === 'Settings' && $ctEntity >= 1) {
116
            $return = $this->redirectToRoute('_home');
117
            $this->addFlash('danger', 'gestock.settings.'.strtolower($entity).'.add2');
118
        }
119
120
        $entityNew = $etm->getClassMetadata($entityPath)->newInstance();
121
        if ($entity === 'User') {
122
            $form = $this->createForm($typePath, $entityNew, array(
123
                'action' => $this->generateUrl(strtolower($entity).'_create'),
124
                'roles' => $options['roles'],
125
            ));
126
        } else {
127
            $form = $this->createForm($typePath, $entityNew, array(
128
                'action' => $this->generateUrl(strtolower($entity).'_create'),
129
            ));
130
        }
131
        $return = array(strtolower($entity) => $entityNew, 'form'   => $form->createView(),);
132
133
        return $return;
134
    }
135
136
    /**
137
     * Creates a new item entity.
138
     *
139
     * @param Request $request   Request in progress
140
     * @param string $entity     Entity <i>First letter Upper</i>
141
     * @param string $entityPath Path of Entity
142
     * @param string $typePath   Path of FormType
143
     * @return array
144
     */
145
    public function abstractCreateAction(Request $request, $entity, $entityPath, $typePath)
146
    {
147
        $param = array();
148
        $etm = $this->getDoctrine()->getManager();
149
        $entityNew = $etm->getClassMetadata($entityPath)->newInstance();
150
        $form = $this->createForm($typePath, $entityNew, array(
151
            'action' => $this->generateUrl(strtolower($entity).'_create'),
152
        ));
153
        $form->handleRequest($request);
154
        $return = [$entity => $entityNew, 'form' => $form->createView(),];
155
156
        if ($form->isValid()) {
157
            $etm = $this->getDoctrine()->getManager();
158
            $etm->persist($entityNew);
159
            $etm->flush();
160
            $this->addFlash('info', 'gestock.create.ok');
161
162
            $param = $this->testReturnParam($entityNew, strtolower($entity));
163
            $route = $form->get('addmore')->isClicked() ? strtolower($entity).'_new' : strtolower($entity).'_show';
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Form\FormInterface as the method isClicked() does only exist in the following implementations of said interface: Symfony\Component\Form\SubmitButton.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
164
165
            $return = $this->redirectToRoute($route, $param);
166
        }
167
168
        return $return;
169
    }
170
171
    /**
172
     * Displays a form to edit an existing item entity.
173
     *
174
     * @param Object $entity     Entity
175
     * @param string $entityName Name of Entity
176
     * @param string $typePath   Path of FormType
177
     * @return array
178
     */
179
    public function abstractEditAction($entity, $entityName, $typePath)
180
    {
181
        $param = $this->testReturnParam($entity, $entityName);
182
        $editForm = $this->createForm($typePath, $entity, array(
183
            'action' => $this->generateUrl($entityName.'_update', $param),
184
            'method' => 'PUT',
185
        ));
186
        if ($entityName === 'group') {
187
            $this->addRoles($editForm, $entity);
188
        }
189
        $deleteForm = $this->createDeleteForm($entity->getId(), $entityName.'_delete');
190
191
        return array(
192
            $entityName => $entity,
193
            'edit_form'   => $editForm->createView(),
194
            'delete_form' => $deleteForm->createView(),
195
        );
196
    }
197
198
    /**
199
     * Edits an existing item entity.
200
     *
201
     * @param Object $entity     Entity
202
     * @param Request $request   Request in progress
203
     * @param string $entityName Name of Entity
204
     * @param string $typePath   Path of FormType
205
     * @return array
206
     */
207
    public function abstractUpdateAction($entity, Request $request, $entityName, $typePath)
208
    {
209
        $param = $this->testReturnParam($entity, $entityName);
210
        $editForm = $this->createForm($typePath, $entity, array(
211
            'action' => $this->generateUrl($entityName.'_update', $param),
212
            'method' => 'PUT',
213
        ));
214
        if ($entityName === 'group') {
215
            $this->addRoles($editForm, $entity);
216
        }
217
218
        $deleteForm = $this->createDeleteForm($entity->getId(), $entityName.'_delete');
219
220
        $return = array(
221
            $entityName => $entity,
222
            'edit_form'   => $editForm->createView(),
223
            'delete_form' => $deleteForm->createView(),
224
        );
225
226
        if ($editForm->handleRequest($request)->isValid()) {
227
            $this->getDoctrine()->getManager()->flush();
228
            $this->addFlash('info', 'gestock.edit.ok');
229
230
            $return = $this->redirectToRoute($entityName.'_edit', $param);
231
        }
232
233
        return $return;
234
    }
235
236
    /**
237
     * Deletes an item entity.
238
     *
239
     * @param Object $entity     Entity
240
     * @param Request $request   Request in progress
241
     * @param string $entityName Name of Entity
242
     * @return array
243
     */
244
    public function abstractDeleteAction($entity, Request $request, $entityName)
245
    {
246
        $form = $this->createDeleteForm($entity->getId(), $entityName.'_delete');
247
        if ($form->handleRequest($request)->isValid()) {
248
            $etm = $this->getDoctrine()->getManager();
249
            $etm->remove($entity);
250
            $etm->flush();
251
        }
252
    }
253
254
    private function testReturnParam($entity, $entityName)
255
    {
256
        $entityArray = ['company', 'settings', 'group', 'tva'];
257
        if (in_array($entityName, $entityArray, true)) {
258
            $param = array('id' => $entity->getId());
259
        } else {
260
            $param = array('slug' => $entity->getSlug());
261
        }
262
263
        return $param;
264
    }
265
266
    /**
267
     * SetOrder for the SortAction in views.
268
     *
269
     * @param string $name   session name
270
     * @param string $entity entity name
271
     * @param string $field  field name
272
     * @param string $type   sort type ("ASC"/"DESC")
273
     */
274
    protected function setOrder($name, $entity, $field, $type = 'ASC')
275
    {
276
        $session = new Session();
277
278
        $session->set('sort.'.$name, array('entity' => $entity, 'field' => $field, 'type' => $type));
279
    }
280
281
    /**
282
     * GetOrder for the SortAction in views.
283
     *
284
     * @param string $name session name
285
     *
286
     * @return array
287
     */
288
    protected function getOrder($name)
289
    {
290
        $session = new Session();
291
292
        return $session->has('sort.' . $name) ? $session->get('sort.' . $name) : null;
293
    }
294
295
    /**
296
     * AddQueryBuilderSort for the SortAction in views.
297
     *
298
     * @param QueryBuilder $qbd
299
     * @param string       $name
300
     */
301
    protected function addQueryBuilderSort(QueryBuilder $qbd, $name)
302
    {
303
        $alias = '';
304
        if (is_array($order = $this->getOrder($name))) {
305
            if ($name !== $order['entity']) {
306
                $rootAlias = current($qbd->getDQLPart('from'))->getAlias();
307
                $join = current($qbd->getDQLPart('join'));
308
                foreach ($join as $item) {
309
                    if ($item->getJoin() === $rootAlias.'.'.$order['entity']) {
310
                        $alias = $item->getAlias();
311
                    }
312
                }
313
            } else {
314
                $alias = current($qbd->getDQLPart('from'))->getAlias();
315
            }
316
            $qbd->orderBy($alias . '.' . $order['field'], $order['type']);
317
        }
318
    }
319
320
    /**
321
     * Create Delete form.
322
     *
323
     * @param int    $id
324
     * @param string $route
325
     *
326
     * @return \Symfony\Component\Form\Form
327
     */
328
    protected function createDeleteForm($id, $route)
329
    {
330
        return $this->createFormBuilder(null, array('attr' => array('id' => 'delete')))
331
            ->setAction($this->generateUrl($route, array('id' => $id)))
332
            ->setMethod('DELETE')
333
            ->getForm()
334
        ;
335
    }
336
337
    /**
338
     * Array of file (`pdf`) layout.
339
     *
340
     * @param string $date File date
341
     * @param string $title File title
342
     * @return array<string,integer|string|boolean>
343
     */
344
    protected function getArray($date, $title)
345
    {
346
        $array = array(
347
            'margin-top' => 15,
348
            'header-spacing' => 5,
349
            'header-font-size' => 8,
350
            'header-left' => 'G.L.S.R.',
351
            'header-center' => $title,
352
            'header-right' => $date,
353
            'header-line' => true,
354
            'margin-bottom' => 15,
355
            'footer-spacing' => 5,
356
            'footer-font-size' => 8,
357
            'footer-left' => 'GLSR &copy 2014 and beyond.',
358
            'footer-right' => 'Page [page]/[toPage]',
359
            'footer-line' => true,
360
        );
361
        return $array;
362
    }
363
364
    /**
365
     * Get the existing roles
366
     *
367
     * @return array Array of roles
368
     */
369
    private function getExistingRoles()
370
    {
371
        $roleHierarchy = $this->container->getParameter('security.role_hierarchy.roles');
372
        $roles = array_keys($roleHierarchy);
373
        $theRoles = array();
374
375
        foreach ($roles as $role) {
376
            $theRoles[$role] = $role;
377
        }
378
        return $theRoles;
379
    }
380
381
    /**
382
     * Add roles to form
383
     *
384
     * @param \Symfony\Component\Form\Form  $form  The form in which to insert the roles
385
     * @param \AppBundle\Entity\Group       $group The entity to deal
386
     * @return \Symfony\Component\Form\Form The form
387
     */
388
    private function addRoles($form, $group)
389
    {
390
        $form->add('roles', ChoiceType::class, array(
391
            'choices' => $this->getExistingRoles(),
392
            'choices_as_values' => true,
393
            'data' => $group->getRoles(),
394
            'label' => 'Roles',
395
            'expanded' => true,
396
            'multiple' => true,
397
            'mapped' => true,
398
        ));
399
400
        return $form;
401
    }
402
}
403