Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
ConformiteTraitementCompletionSubscriber
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
7 / 7
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSubscribedEvents
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 prePersist
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 preUpdate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 postUpdate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 processConformiteTraitement
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 processReponseOrMesurment
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
6
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\Registry\Symfony\EventSubscriber\Doctrine;
25
26use App\Domain\Registry\Calculator\Completion\ConformiteTraitementCompletion;
27use App\Domain\Registry\Model\ConformiteTraitement\ConformiteTraitement;
28use App\Domain\Registry\Model\ConformiteTraitement\Reponse;
29use App\Domain\Registry\Model\Mesurement;
30use Doctrine\Common\EventSubscriber;
31use Doctrine\ORM\Event\PostUpdateEventArgs;
32use Doctrine\ORM\Event\PrePersistEventArgs;
33use Doctrine\ORM\Event\PreUpdateEventArgs;
34
35class ConformiteTraitementCompletionSubscriber implements EventSubscriber
36{
37    /**
38     * @var ConformiteTraitementCompletion
39     */
40    private $conformiteTraitementCompletion;
41
42    public function __construct(ConformiteTraitementCompletion $conformiteTraitementCompletion)
43    {
44        $this->conformiteTraitementCompletion = $conformiteTraitementCompletion;
45    }
46
47    public function getSubscribedEvents(): array
48    {
49        return [
50            'prePersist',
51            'preUpdate',
52            'postUpdate',
53        ];
54    }
55
56    public function prePersist(PrePersistEventArgs $args)
57    {
58        $this->processConformiteTraitement($args);
59    }
60
61    public function preUpdate(PreUpdateEventArgs $args)
62    {
63        $this->processConformiteTraitement($args);
64    }
65
66    public function postUpdate(PostUpdateEventArgs $args)
67    {
68        $this->processReponseOrMesurment($args);
69    }
70
71    private function processConformiteTraitement(PreUpdateEventArgs|PrePersistEventArgs $args)
72    {
73        $object = $args->getObject();
74
75        if (!$object instanceof ConformiteTraitement) {
76            return;
77        }
78
79        $this->conformiteTraitementCompletion->setCalculsConformite($object);
80    }
81
82    private function processReponseOrMesurment(PostUpdateEventArgs $args): void
83    {
84        $object = $args->getObject();
85
86        if (!$object instanceof Reponse && !$object instanceof Mesurement) {
87            return;
88        }
89
90        $args->getObjectManager()->refresh($object);
91
92        switch (true) {
93            case $object instanceof Reponse:
94                $this->conformiteTraitementCompletion->setCalculsConformite($object->getConformiteTraitement());
95                $args->getObjectManager()->persist($object->getConformiteTraitement());
96                break;
97            case $object instanceof Mesurement:
98                foreach ($object->getConformiteTraitementReponses() as $reponse) {
99                    $this->conformiteTraitementCompletion->setCalculsConformite($reponse->getConformiteTraitement());
100                    $args->getObjectManager()->persist($reponse->getConformiteTraitement());
101                }
102                break;
103        }
104
105        $args->getObjectManager()->flush();
106    }
107}