Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
17.50% covered (danger)
17.50%
7 / 40
50.00% covered (danger)
50.00%
5 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
CategoryController
17.50% covered (danger)
17.50%
7 / 40
50.00% covered (danger)
50.00%
5 / 10
268.63
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getDomain
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getModel
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getModelClass
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFormType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getListData
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 formPrePersistData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 editAction
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 deleteAction
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 deleteConfirmationAction
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3/**
4 * This file is part of the MADIS - RGPD Management application.
5 *
6 * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22declare(strict_types=1);
23
24namespace App\Domain\Documentation\Controller;
25
26use App\Application\Controller\CRUDController;
27use App\Application\Symfony\Security\UserProvider;
28use App\Domain\Documentation\Form\Type\CategoryType;
29use App\Domain\Documentation\Model;
30use App\Domain\Documentation\Repository;
31use Doctrine\ORM\EntityManagerInterface;
32use Knp\Snappy\Pdf;
33use Symfony\Component\HttpFoundation\Request;
34use Symfony\Component\HttpFoundation\Response;
35use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
36use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
37use Symfony\Contracts\Translation\TranslatorInterface;
38use Symfony\Polyfill\Intl\Icu\Exception\MethodNotImplementedException;
39
40/**
41 * @property Repository\Category $repository
42 */
43class CategoryController extends CRUDController
44{
45    /**
46     * @var AuthorizationCheckerInterface
47     */
48    protected $authorizationChecker;
49
50    /**
51     * @var UserProvider
52     */
53    protected $userProvider;
54
55    public function __construct(
56        EntityManagerInterface $entityManager,
57        TranslatorInterface $translator,
58        Repository\Category $repository,
59        AuthorizationCheckerInterface $authorizationChecker,
60        UserProvider $userProvider,
61        Pdf $pdf,
62    ) {
63        parent::__construct($entityManager, $translator, $repository, $pdf, $userProvider, $authorizationChecker);
64        $this->authorizationChecker = $authorizationChecker;
65        $this->userProvider         = $userProvider;
66    }
67
68    protected function getDomain(): string
69    {
70        return 'documentation';
71    }
72
73    protected function getModel(): string
74    {
75        return 'category';
76    }
77
78    protected function getModelClass(): string
79    {
80        return Model\Category::class;
81    }
82
83    protected function getFormType(): string
84    {
85        return CategoryType::class;
86    }
87
88    protected function getListData()
89    {
90        $order = [
91            'createdAt' => 'DESC',
92        ];
93
94        return $this->repository->findAll($order);
95    }
96
97    /**
98     * {@inheritdoc}
99     * Here, we wanna compute maturity score.
100     *
101     * @param Model\Category $object
102     */
103    public function formPrePersistData($object, $form = null)
104    {
105        $object->setSysteme(false);
106    }
107
108    public function editAction(Request $request, string $id): Response
109    {
110        $object = $this->repository->findOneById($id);
111        if (!$object || !$this->authorizationChecker->isGranted('ROLE_ADMIN') || $object->getSysteme()) {
112            throw new NotFoundHttpException("No object found with ID '{$id}'");
113        }
114
115        return parent::editAction($request, $id);
116    }
117
118    /**
119     * The delete action view
120     * Display a confirmation message to confirm data deletion.
121     *
122     * @Override
123     */
124    public function deleteAction(string $id): Response
125    {
126        $object = $this->repository->findOneById($id);
127        if (!$object) {
128            throw new NotFoundHttpException("No object found with ID '{$id}'");
129        }
130
131        if (!$this->authorizationChecker->isGranted('ROLE_ADMIN') || $object->getSysteme()) {
132            $this->addFlash('error', $this->getFlashbagMessage('error', 'delete', $object));
133
134            return $this->redirectToRoute('documentation_document_list');
135        }
136
137        return $this->render($this->getTemplatingBasePath('delete'), [
138            'object' => $object,
139        ]);
140    }
141
142    /**
143     * The deletion action
144     * Delete the data.
145     *
146     * @throws \Exception
147     */
148    public function deleteConfirmationAction(string $id): Response
149    {
150        $object = $this->repository->findOneById($id);
151        if (!$object) {
152            throw new NotFoundHttpException("No object found with ID '{$id}'");
153        }
154
155        if (!$this->authorizationChecker->isGranted('ROLE_ADMIN') || $object->getSysteme()) {
156            $this->addFlash('error', $this->getFlashbagMessage('error', 'delete', $object));
157
158            return $this->redirectToRoute('documentation_document_list');
159        }
160
161        if ($this->isSoftDelete()) {
162            if (!\method_exists($object, 'setDeletedAt')) {
163                throw new MethodNotImplementedException('setDeletedAt');
164            }
165            $object->setDeletedAt(new \DateTimeImmutable());
166            $this->repository->update($object);
167        } else {
168            $this->entityManager->remove($object);
169            $this->entityManager->flush();
170        }
171
172        $this->addFlash('success', $this->getFlashbagMessage('success', 'delete', $object));
173
174        return $this->redirectToRoute($this->getRouteName('list'));
175    }
176}