Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 87
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
NotificationsGenerateCommand
0.00% covered (danger)
0.00%
0 / 87
0.00% covered (danger)
0.00%
0 / 9
1806
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 configure
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
 generateLateActionNotifications
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 generateLateRequestNotification
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 generateLateSurveyNotification
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 generateNoLoginNotifications
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 generateTreatmentNeedsAIPDNotification
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
42
 getCritereValid
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
462
1<?php
2
3namespace App\Domain\Notification\Command;
4
5use App\Domain\Maturity\Repository\Survey as SurveyRepository;
6use App\Domain\Notification\Event\ConformiteTraitementNeedsAIPDEvent;
7use App\Domain\Notification\Event\LateActionEvent;
8use App\Domain\Notification\Event\LateRequestEvent;
9use App\Domain\Notification\Event\LateSurveyEvent;
10use App\Domain\Notification\Event\NoLoginEvent;
11use App\Domain\Registry\Dictionary\MesurementStatusDictionary;
12use App\Domain\Registry\Model\ConformiteTraitement\ConformiteTraitement;
13use App\Domain\Registry\Model\Mesurement;
14use App\Domain\Registry\Model\TreatmentDataCategory;
15use App\Domain\User\Repository\User as UserRepository;
16use App\Infrastructure\ORM\Registry\Repository\ConformiteTraitement\ConformiteTraitement as ConformiteTraitementRepository;
17use App\Infrastructure\ORM\Registry\Repository\Mesurement as MesurementRepository;
18use App\Infrastructure\ORM\Registry\Repository\Request as RequestRepository;
19use Symfony\Component\Console\Command\Command;
20use Symfony\Component\Console\Input\InputInterface;
21use Symfony\Component\Console\Output\OutputInterface;
22use Symfony\Component\Console\Style\SymfonyStyle;
23use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24
25class NotificationsGenerateCommand extends Command
26{
27    protected static $defaultName        = 'notifications:generate';
28    protected static $defaultDescription = 'Generate notifications that depend on elapsed time for the application';
29
30    private EventDispatcherInterface $dispatcher;
31    private MesurementRepository $mesurementRepository;
32    private RequestRepository $requestRepository;
33    private UserRepository $userRepository;
34    private SurveyRepository $surveyRepository;
35    private ConformiteTraitementRepository $conformiteTraitementRepository;
36
37    private SymfonyStyle $io;
38
39    private bool $activeNotifications;
40
41    public function __construct(
42        EventDispatcherInterface $dispatcher,
43        MesurementRepository $mesurementRepository,
44        RequestRepository $requestRepository,
45        UserRepository $userRepository,
46        SurveyRepository $surveyRepository,
47        ConformiteTraitementRepository $conformiteTraitementRepository,
48        bool $activeNotifications,
49    ) {
50        $this->dispatcher                     = $dispatcher;
51        $this->mesurementRepository           = $mesurementRepository;
52        $this->requestRepository              = $requestRepository;
53        $this->userRepository                 = $userRepository;
54        $this->surveyRepository               = $surveyRepository;
55        $this->conformiteTraitementRepository = $conformiteTraitementRepository;
56        $this->activeNotifications            = $activeNotifications;
57
58        parent::__construct();
59    }
60
61    protected function configure(): void
62    {
63        $this
64            ->setDescription(self::$defaultDescription)
65        ;
66    }
67
68    protected function execute(InputInterface $input, OutputInterface $output): int
69    {
70        $this->io = new SymfonyStyle($input, $output);
71
72        if (!$this->activeNotifications) {
73            $this->io->warning('Notifications are not active. Exiting now');
74            exit(0);
75        }
76        $cnt = $this->generateNoLoginNotifications();
77        $this->io->success($cnt . ' inactive users notifications generated');
78
79        $cnt = $this->generateLateActionNotifications();
80        $this->io->success($cnt . ' late actions notifications generated');
81
82        $cnt = $this->generateLateRequestNotification();
83        $this->io->success($cnt . ' late requests notifications generated');
84
85        $cnt = $this->generateLateSurveyNotification();
86        $this->io->success($cnt . ' late survey notifications generated');
87
88        $cnt = $this->generateTreatmentNeedsAIPDNotification();
89        $this->io->success($cnt . ' treatment needs AIPD notifications generated');
90
91        return 0;
92    }
93
94    protected function generateLateActionNotifications(): int
95    {
96        $actions = $this->mesurementRepository->findAll();
97        $now     = new \DateTime();
98        $cnt     = 0;
99        foreach ($actions as $action) {
100            /**
101             * @var Mesurement $action
102             */
103            if ($action->getPlanificationDate() && $action->getPlanificationDate() < $now && MesurementStatusDictionary::STATUS_NOT_APPLIED === $action->getStatus()) {
104                $this->dispatcher->dispatch(new LateActionEvent($action));
105                ++$cnt;
106            }
107        }
108
109        return $cnt;
110    }
111
112    protected function generateLateRequestNotification(): int
113    {
114        $cnt      = 0;
115        $requests = $this->requestRepository->findAllLate();
116        foreach ($requests as $request) {
117            $this->dispatcher->dispatch(new LateRequestEvent($request));
118            ++$cnt;
119            //            $this->io->writeln('late request count so far: ' . $cnt);
120        }
121
122        return $cnt;
123    }
124
125    protected function generateLateSurveyNotification(): int
126    {
127        $cnt = 0;
128        // Find all late surveys
129        $surveys = $this->surveyRepository->findAllLate();
130
131        foreach ($surveys as $survey) {
132            $this->dispatcher->dispatch(new LateSurveyEvent($survey));
133            ++$cnt;
134            //            $this->io->writeln('late survey count so far: ' . $cnt);
135        }
136
137        return $cnt;
138    }
139
140    protected function generateNoLoginNotifications(): int
141    {
142        // Get users that have last login null and created_at more than 6 months ago
143        $users = $this->userRepository->findAllNoLogin();
144        $cnt   = 0;
145        foreach ($users as $user) {
146            ++$cnt;
147            $this->dispatcher->dispatch(new NoLoginEvent($user));
148            //            $this->io->writeln('No login count so far: ' . $cnt);
149        }
150
151        return $cnt;
152    }
153
154    protected function generateTreatmentNeedsAIPDNotification(): int
155    {
156        // Get all conformité traitements that should have an AIPD
157        $cfs = $this->conformiteTraitementRepository->findAll();
158        $cnt = 0;
159        foreach ($cfs as $conformite) {
160            /** @var ConformiteTraitement $conformite */
161            if ($conformite->getTraitement() && !$conformite->getTraitement()->getExemptAIPD()
162                && null === $conformite->getLastAnalyseImpact() && $this->getCritereValid($conformite)) {
163                ++$cnt;
164                $this->dispatcher->dispatch(new ConformiteTraitementNeedsAIPDEvent($conformite));
165                //                $this->io->writeln('treatment needs AIPD count so far: ' . $cnt);
166            }
167        }
168
169        return $cnt;
170    }
171
172    private function getCritereValid(ConformiteTraitement $conformite)
173    {
174        $counter  = 0;
175        $sensible = false;
176
177        $t = $conformite->getTraitement();
178
179        if ($t && $t->isLargeScaleCollection()) {
180            ++$counter;
181        }
182        if ($t && $t->isDataCrossing()) {
183            ++$counter;
184        }
185        if ($t && $t->isAutomatedDecisionsWithLegalEffect()) {
186            ++$counter;
187        }
188        if ($t && $t->isEvaluationOrRating()) {
189            ++$counter;
190        }
191        if ($t && $t->isAutomaticExclusionService()) {
192            ++$counter;
193        }
194        if ($t && $t->isVulnerablePeople()) {
195            ++$counter;
196        }
197        if ($t && $t->isSystematicMonitoring()) {
198            ++$counter;
199        }
200        if ($t && $t->isInnovativeUse()) {
201            ++$counter;
202        }
203
204        if (1 === $counter) {
205            foreach ($t->getDataCategories() as $category) {
206                /** @var TreatmentDataCategory $category */
207                if ($category->isSensible()) {
208                    $sensible = true;
209                }
210            }
211
212            return $sensible;
213        }
214
215        if ($counter > 1) {
216            return true;
217        }
218
219        return false;
220    }
221}