MenuApplicazioneController::deleteAction()   A
last analyzed

Complexity

Conditions 3
Paths 9

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3.1056

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 32
ccs 17
cts 22
cp 0.7727
rs 9.568
c 0
b 0
f 0
cc 3
nc 9
nop 1
crap 3.1056
1
<?php
2
3
namespace Fi\CoreBundle\Controller;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
8
9
/**
10
 * MenuApplicazione controller.
11
 */
12
class MenuApplicazioneController extends FiCoreController
13
{
14
    /**
15
     * Displays a form to create a new table entity.
16
     */
17 1
    public function newAction(Request $request)
18
    {
19 1
        $this->setup($request);
20 1
        $namespace = $this->getNamespace();
21 1
        $bundle = $this->getBundle();
22 1
        $controller = $this->getController();
23
24 1
        if (!self::$canCreate) {
25
            throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto");
26
        }
27
28 1
        $nomebundle = $namespace . $bundle . 'Bundle';
29 1
        $classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller;
30 1
        $formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller;
31 1
        $formType = $formbundle . 'Type';
32
33 1
        $entity = new $classbundle();
34 1
        $em = $this->getDoctrine()->getManager();
35 1
        $form = $this->createForm(
36 1
            $formType,
37 1
            $entity,
38 1
            array('entity_manager' => $em, 'attr' => array(
39 1
                'id' => 'formdati' . $controller,
40
                ),
41 1
                'action' => $this->generateUrl($controller . '_create'),
42
                )
43
        );
44
45 1
        return $this->render(
46 1
            $nomebundle . ':' . $controller . ':new.html.twig',
47
            array(
48 1
                    'nomecontroller' => $controller,
49 1
                    'entity' => $entity,
50 1
                    'form' => $form->createView(),
51
                        )
52
        );
53
    }
54
    /**
55
     * Displays a form to edit an existing table entity.
56
     */
57 1
    public function editAction(Request $request, $id)
58
    {
59
        /* @var $em \Doctrine\ORM\EntityManager */
60 1
        $this->setup($request);
61 1
        $namespace = $this->getNamespace();
62 1
        $bundle = $this->getBundle();
63 1
        $controller = $this->getController();
64
65 1
        if (!self::$canUpdate) {
66
            throw new AccessDeniedException("Non si hanno i permessi per modificare questo contenuto");
67
        }
68
69 1
        $nomebundle = $namespace . $bundle . 'Bundle';
70 1
        $formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller;
71 1
        $formType = $formbundle . 'Type';
72
73 1
        $elencomodifiche = $this->elencoModifiche($controller, $id);
74
75 1
        $em = $this->getDoctrine()->getManager();
76
77 1
        $entity = $em->getRepository($nomebundle . ':' . $controller)->find($id);
78
79 1
        if (!$entity) {
80
            throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.');
81
        }
82
83 1
        $editForm = $this->createForm(
84 1
            $formType,
85 1
            $entity,
86 1
            array('entity_manager' => $em, 'attr' => array(
87 1
                'id' => 'formdati' . $controller,
88
                ),
89 1
                'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())),
90
                )
91
        );
92
93 1
        $deleteForm = $this->createDeleteForm($id);
94
95 1
        return $this->render(
96 1
            $nomebundle . ':' . $controller . ':edit.html.twig',
97
            array(
98 1
                    'entity' => $entity,
99 1
                    'nomecontroller' => $controller,
100 1
                    'edit_form' => $editForm->createView(),
101 1
                    'delete_form' => $deleteForm->createView(),
102 1
                    'elencomodifiche' => $elencomodifiche,
103
                        )
104
        );
105
    }
106
    /**
107
     * Creates a new table entity.
108
     */
109 1
    public function createAction(Request $request)
110
    {
111 1
        $this->setup($request);
112 1
        $namespace = $this->getNamespace();
113 1
        $bundle = $this->getBundle();
114 1
        $controller = $this->getController();
115
116 1
        if (!self::$canCreate) {
117
            throw new AccessDeniedException("Non si hanno i permessi per creare questo contenuto");
118
        }
119
120 1
        $nomebundle = $namespace . $bundle . 'Bundle';
121 1
        $classbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Entity\\' . $controller;
122 1
        $formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller;
123
124 1
        $entity = new $classbundle();
125 1
        $formType = $formbundle . 'Type';
126 1
        $em = $this->getDoctrine()->getManager();
127 1
        $form = $this->createForm(
128 1
            $formType,
129 1
            $entity,
130 1
            array('entity_manager' => $em, 'attr' => array(
131 1
                'id' => 'formdati' . $controller,
132
                ),
133 1
                'action' => $this->generateUrl($controller . '_create'),
134
                )
135
        );
136
137 1
        $form->submit($request->request->get($form->getName()));
138
139 1
        if ($form->isValid()) {
140 1
            $em = $this->getDoctrine()->getManager();
141 1
            $em->persist($entity);
142 1
            $em->flush();
143 1
            $em->getConfiguration()->getResultCacheImpl()->delete($controller);
0 ignored issues
show
Bug introduced by
The method getConfiguration() does not exist on Doctrine\Persistence\ObjectManager. It seems like you code against a sub-type of Doctrine\Persistence\ObjectManager such as Doctrine\ORM\EntityManagerInterface or Doctrine\ORM\Decorator\EntityManagerDecorator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
            $em->/** @scrutinizer ignore-call */ 
144
                 getConfiguration()->getResultCacheImpl()->delete($controller);
Loading history...
144
145 1
            $continua = (int) $request->get('continua');
146 1
            if ($continua === 0) {
147 1
                return new Response('OK');
148
            } else {
149
                return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $entity->getId())));
150
            }
151
        }
152
153
        return $this->render(
154
            $nomebundle . ':' . $controller . ':new.html.twig',
155
            array(
156
                    'nomecontroller' => $controller,
157
                    'entity' => $entity,
158
                    'form' => $form->createView(),
159
                        )
160
        );
161
    }
162
    /**
163
     * Edits an existing table entity.
164
     */
165 1
    public function updateAction(Request $request, $id)
166
    {
167
        /* @var $em \Doctrine\ORM\EntityManager */
168 1
        $this->setup($request);
169 1
        $namespace = $this->getNamespace();
170 1
        $bundle = $this->getBundle();
171 1
        $controller = $this->getController();
172
173 1
        if (!self::$canUpdate) {
174
            throw new AccessDeniedException("Non si hanno i permessi per aggiornare questo contenuto");
175
        }
176
177 1
        $nomebundle = $namespace . $bundle . 'Bundle';
178 1
        $formbundle = $namespace . '\\' . $bundle . 'Bundle' . '\\Form\\' . $controller;
179 1
        $formType = $formbundle . 'Type';
180
181 1
        $repoStorico = $this->container->get('Storicomodifiche_repository');
182
183 1
        $em = $this->getDoctrine()->getManager();
184
185 1
        $entity = $em->getRepository($nomebundle . ':' . $controller)->find($id);
186
187 1
        if (!$entity) {
188
            throw $this->createNotFoundException('Unable to find ' . $controller . ' entity.');
189
        }
190
191 1
        $deleteForm = $this->createDeleteForm($id);
192
193 1
        $editForm = $this->createForm(
194 1
            $formType,
195 1
            $entity,
196 1
            array('entity_manager' => $em, 'attr' => array(
197 1
                'id' => 'formdati' . $controller,
198
                ),
199 1
                'action' => $this->generateUrl($controller . '_update', array('id' => $entity->getId())),
200
                )
201
        );
202
203 1
        $editForm->submit($request->request->get($editForm->getName()));
204
205 1
        if ($editForm->isValid()) {
206 1
            $originalData = $em->getUnitOfWork()->getOriginalEntityData($entity);
0 ignored issues
show
Bug introduced by
The method getUnitOfWork() does not exist on Doctrine\Persistence\ObjectManager. It seems like you code against a sub-type of Doctrine\Persistence\ObjectManager such as Doctrine\ORM\EntityManagerInterface or Doctrine\ORM\Decorator\EntityManagerDecorator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
            $originalData = $em->/** @scrutinizer ignore-call */ getUnitOfWork()->getOriginalEntityData($entity);
Loading history...
207
208 1
            $em->persist($entity);
209 1
            $em->flush();
210 1
            $em->getConfiguration()->getResultCacheImpl()->delete($controller);
211
212 1
            $newData = $em->getUnitOfWork()->getOriginalEntityData($entity);
213 1
            $changes = $repoStorico->isRecordChanged($nomebundle, $controller, $originalData, $newData);
214
215 1
            if ($changes) {
216
                $repoStorico->saveHistory($controller, $changes, $id, $this->getUser());
217
            }
218
219 1
            $continua = (int) $request->get('continua');
220 1
            if ($continua === 0) {
221 1
                return new Response('OK');
222
            } else {
223
                return $this->redirect($this->generateUrl($controller . '_edit', array('id' => $id)));
224
            }
225
        }
226
227
        return $this->render(
228
            $nomebundle . ':' . $controller . ':edit.html.twig',
229
            array(
230
                    'entity' => $entity,
231
                    'edit_form' => $editForm->createView(),
232
                    'delete_form' => $deleteForm->createView(),
233
                    'nomecontroller' => $controller,
234
                        )
235
        );
236
    }
237
    /**
238
     * Deletes a table entity.
239
     */
240 1
    public function deleteAction(Request $request)
241
    {
242
        /* @var $em \Doctrine\ORM\EntityManager */
243 1
        $this->setup($request);
244 1
        if (!self::$canDelete) {
245
            throw new AccessDeniedException("Non si hanno i permessi per aggiornare questo contenuto");
246
        }
247 1
        $namespace = $this->getNamespace();
248 1
        $bundle = $this->getBundle();
249 1
        $controller = $this->getController();
250
251 1
        $nomebundle = $namespace . $bundle . 'Bundle';
252
253
        try {
254 1
            $em = $this->getDoctrine()->getManager();
255 1
            $qb = $em->createQueryBuilder();
0 ignored issues
show
Bug introduced by
The method createQueryBuilder() does not exist on Doctrine\Persistence\ObjectManager. It seems like you code against a sub-type of Doctrine\Persistence\ObjectManager such as Doctrine\ORM\EntityManagerInterface or Doctrine\ORM\Decorator\EntityManagerDecorator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

255
            /** @scrutinizer ignore-call */ 
256
            $qb = $em->createQueryBuilder();
Loading history...
256 1
            $ids = explode(', ', $request->get('id'));
0 ignored issues
show
Bug introduced by
It seems like $request->get('id') can also be of type null; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

256
            $ids = explode(', ', /** @scrutinizer ignore-type */ $request->get('id'));
Loading history...
257 1
            $qb->delete($nomebundle . ':' . $controller, 'u')
258 1
                    ->andWhere('u.id IN (:ids)')
259 1
                    ->setParameter('ids', $ids);
260
261 1
            $query = $qb->getQuery();
262 1
            $query->execute();
263 1
            $em->getConfiguration()->getResultCacheImpl()->delete($controller);
264
        } catch (\Exception $e) {
265
            $response = new Response();
266
            $response->setStatusCode('200');
267
268
            return new Response('404');
269
        }
270
271 1
        return new Response('OK');
272
    }
273
}
274