Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
43.14% |
44 / 102 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
NotificationExtension | |
43.14% |
44 / 102 |
|
40.00% |
2 / 5 |
509.65 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
getFilters | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getSentence | |
46.15% |
24 / 52 |
|
0.00% |
0 / 1 |
105.59 | |||
getObjectLink | |
43.75% |
7 / 16 |
|
0.00% |
0 / 1 |
37.63 | |||
getRouteForModule | |
8.70% |
2 / 23 |
|
0.00% |
0 / 1 |
141.64 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace App\Domain\Notification\Twig\Extension; |
6 | |
7 | use App\Domain\AIPD\Dictionary\StatutAnalyseImpactDictionary; |
8 | use App\Domain\Documentation\Model\Document; |
9 | use App\Domain\Notification\Model\Notification; |
10 | use App\Domain\Notification\Model\Notification as NotificationModel; |
11 | use App\Domain\Registry\Dictionary\ProofTypeDictionary; |
12 | use App\Domain\Registry\Dictionary\ViolationNatureDictionary; |
13 | use App\Domain\Registry\Model\Proof; |
14 | use App\Domain\Registry\Model\Violation; |
15 | use App\Infrastructure\ORM\AIPD\Repository\AnalyseImpact; |
16 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
17 | use Symfony\Component\Routing\RouterInterface; |
18 | use Symfony\Component\Security\Core\Security; |
19 | use Symfony\Contracts\Translation\TranslatorInterface; |
20 | use Twig\Extension\AbstractExtension; |
21 | use Twig\TwigFilter; |
22 | |
23 | class NotificationExtension extends AbstractExtension |
24 | { |
25 | protected TranslatorInterface $translator; |
26 | protected RouterInterface $router; |
27 | protected \App\Domain\Notification\Repository\Notification $repository; |
28 | protected AnalyseImpact $aipdRepository; |
29 | protected Security $security; |
30 | |
31 | protected string $requestDays; |
32 | protected string $surveyDays; |
33 | |
34 | public function __construct(TranslatorInterface $translator, RouterInterface $router, Security $security, \App\Domain\Notification\Repository\Notification $repository, AnalyseImpact $aipdRepository, string $requestDays, string $surveyDays) |
35 | { |
36 | $this->translator = $translator; |
37 | $this->router = $router; |
38 | $this->security = $security; |
39 | $this->repository = $repository; |
40 | $this->requestDays = $requestDays; |
41 | $this->aipdRepository = $aipdRepository; |
42 | $this->surveyDays = $surveyDays; |
43 | } |
44 | |
45 | public function getFilters() |
46 | { |
47 | return [ |
48 | new TwigFilter('sentence', [$this, 'getSentence']), |
49 | new TwigFilter('link', [$this, 'getObjectLink']), |
50 | ]; |
51 | } |
52 | |
53 | public function getSentence(Notification $notification): string |
54 | { |
55 | $sentence = '<strong>[' . $this->translator->trans($notification->getModule()) . ']</strong> '; |
56 | |
57 | if ($notification->getModule() === 'notification.modules.' . NotificationModel::MODULES[Document::class]) { |
58 | $sentence .= $this->translator->trans('notifications.sentence.new_documentation'); |
59 | } |
60 | |
61 | switch ($notification->getAction()) { |
62 | case 'notifications.actions.late_request': |
63 | case 'notification.actions.late_request': |
64 | $link = $this->getObjectLink($notification); |
65 | $sentence .= $this->translator->trans('notifications.sentence.late_request', [ |
66 | '%name%' => '<a href="' . $link . '">' . $notification->getName() . '</a> ', |
67 | '%days%' => $this->requestDays, |
68 | ]) . ' '; |
69 | break; |
70 | case 'notifications.actions.late_survey': |
71 | case 'notification.actions.late_survey': |
72 | $sentence .= ' ' . $this->translator->trans('notifications.sentence.late_survey', [ |
73 | '%days%' => $this->surveyDays, |
74 | ]) . ' '; |
75 | break; |
76 | case 'notifications.actions.delete': |
77 | case 'notification.actions.delete': |
78 | $sentence .= $this->translator->trans($notification->getAction()) . ' '; |
79 | $sentence .= ' : ' . |
80 | '<span>' . $notification->getName() . '</span> ' |
81 | ; |
82 | break; |
83 | default: |
84 | if ($notification->getModule() !== 'notification.modules.' . NotificationModel::MODULES[Document::class]) { |
85 | $sentence .= $this->translator->trans($notification->getAction()) . ' '; |
86 | } |
87 | if ('notification.modules.action_plan' !== $notification->getModule()) { |
88 | $sentence .= ' : '; |
89 | } |
90 | $link = $this->getObjectLink($notification); |
91 | if ($this->repository->objectExists($notification)) { |
92 | $sentence .= '<a href="' . $link . '">' . $notification->getName() . '</a> ' |
93 | ; |
94 | } else { |
95 | $sentence .= '<span>' . $notification->getName() . '</span> ' |
96 | ; |
97 | } |
98 | } |
99 | |
100 | if ('notification.modules.aipd' === $notification->getModule() && ('notification.actions.state_change' === $notification->getAction() || 'notification.actions.validated' === $notification->getAction())) { |
101 | $sentence .= '<strong>(' . StatutAnalyseImpactDictionary::getStatuts()[$notification->getObject()->statut] . ') </strong>'; |
102 | } |
103 | |
104 | if ($notification->getModule() === 'notification.modules.' . NotificationModel::MODULES[Violation::class]) { |
105 | $natures = []; |
106 | |
107 | if ($notification->getObject()->violationNatures) { |
108 | $raw = $notification->getObject()->violationNatures; |
109 | if (is_string($raw)) { |
110 | $raw = explode(',', $raw); |
111 | } |
112 | $natures = array_map(function ($n) {return ViolationNatureDictionary::getNatures()[trim($n)]; }, (array) $raw); |
113 | } |
114 | $sentence .= '<strong>(' . join(', ', $natures) . ')</strong> '; |
115 | } |
116 | if ($notification->getModule() === 'notification.modules.' . NotificationModel::MODULES[Proof::class]) { |
117 | $sentence .= '<strong>(' . ProofTypeDictionary::getTypes()[$notification->getObject()->type] . ')</strong> '; |
118 | } |
119 | if ('notification.modules.action_plan' === $notification->getModule()) { |
120 | $sentence .= ' pour le <strong>' . (new \DateTime($notification->getObject()->planificationDate))->format('d/m/Y') . '</strong> est en retard '; |
121 | } |
122 | |
123 | if ($notification->getCollectivity() && $this->security->getToken() && $this->security->isGranted('ROLE_REFERENT')) { |
124 | $sentence .= $this->translator->trans('notifications.label.par') . ' <strong>' . $notification->getCollectivity()->getName() . '</strong>'; |
125 | } |
126 | |
127 | return $sentence; |
128 | } |
129 | |
130 | public function getObjectLink(Notification $notification): string |
131 | { |
132 | try { |
133 | if ('notification.modules.aipd' === $notification->getModule() && 'notification.actions.validated' === $notification->getAction()) { |
134 | /** @var \App\Domain\AIPD\Model\AnalyseImpact $aipd */ |
135 | $aipd = $this->aipdRepository->findOneById($notification->getObject()->id); |
136 | if ($aipd) { |
137 | return $this->router->generate('registry_treatment_show', ['id' => $aipd->getConformiteTraitement()->getTraitement()->getId()->toString()], UrlGeneratorInterface::ABSOLUTE_URL); |
138 | } |
139 | |
140 | return ''; |
141 | } |
142 | if ('notification.modules.aipd' === $notification->getModule() && 'notifications.actions.treatment_needs_aipd' === $notification->getAction()) { |
143 | return $this->router->generate('registry_conformite_traitement_start_aipd', ['id' => $notification->getObject()->id], UrlGeneratorInterface::ABSOLUTE_URL); |
144 | } |
145 | if ('notification.modules.aipd' === $notification->getModule() && 'notification.actions.validation' === $notification->getAction()) { |
146 | return $this->router->generate('aipd_analyse_impact_validation', ['id' => $notification->getObject()->id], UrlGeneratorInterface::ABSOLUTE_URL); |
147 | } |
148 | if ('notification.modules.document' === $notification->getModule() && 'notification.actions.delete' !== $notification->getAction()) { |
149 | return $notification->getObject()->url; |
150 | } |
151 | if ('notification.modules.action_plan' === $notification->getModule()) { |
152 | return $this->router->generate('registry_mesurement_show', ['id' => $notification->getObject()->id], UrlGeneratorInterface::ABSOLUTE_URL); |
153 | } |
154 | |
155 | return $this->router->generate($this->getRouteForModule($notification->getModule()), ['id' => $notification->getObject()->id], UrlGeneratorInterface::ABSOLUTE_URL); |
156 | } catch (\Exception $e) { |
157 | return ''; |
158 | } |
159 | } |
160 | |
161 | private function getRouteForModule($module): string |
162 | { |
163 | switch ($module) { |
164 | case 'notification.modules.treatment': |
165 | return 'registry_treatment_show'; |
166 | case 'notification.modules.subcontractor': |
167 | case 'notification.modules.contractor': |
168 | return 'registry_contractor_show'; |
169 | case 'notification.modules.violation': |
170 | return 'registry_violation_show'; |
171 | case 'notification.modules.proof': |
172 | return 'registry_proof_edit'; |
173 | case 'notification.modules.protect_action': |
174 | return 'registry_mesurement_show'; |
175 | case 'notification.modules.aipd': |
176 | return 'aipd_analyse_impact_edit'; |
177 | case 'notification.modules.request': |
178 | return 'registry_request_show'; |
179 | case 'notification.modules.user': |
180 | return 'user_user_edit'; |
181 | case 'notification.modules.documentation': |
182 | case 'notification.modules.document': |
183 | return 'documentation_document_edit'; |
184 | case 'notification.modules.maturity': |
185 | return 'maturity_survey_edit'; |
186 | } |
187 | |
188 | return ''; |
189 | } |
190 | } |