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\LifecycleEventArgs;
32
33class ConformiteTraitementCompletionSubscriber implements EventSubscriber
34{
35    /**
36     * @var ConformiteTraitementCompletion
37     */
38    private $conformiteTraitementCompletion;
39
40    public function __construct(ConformiteTraitementCompletion $conformiteTraitementCompletion)
41    {
42        $this->conformiteTraitementCompletion = $conformiteTraitementCompletion;
43    }
44
45    public function getSubscribedEvents(): array
46    {
47        return [
48            'prePersist',
49            'preUpdate',
50            'postUpdate',
51        ];
52    }
53
54    public function prePersist(LifecycleEventArgs $args)
55    {
56        $this->processConformiteTraitement($args);
57    }
58
59    public function preUpdate(LifecycleEventArgs $args)
60    {
61        $this->processConformiteTraitement($args);
62    }
63
64    public function postUpdate(LifecycleEventArgs $args)
65    {
66        $this->processReponseOrMesurment($args);
67    }
68
69    private function processConformiteTraitement(LifecycleEventArgs $args)
70    {
71        $object = $args->getObject();
72
73        if (!$object instanceof ConformiteTraitement) {
74            return;
75        }
76
77        $this->conformiteTraitementCompletion->setCalculsConformite($object);
78    }
79
80    private function processReponseOrMesurment(LifecycleEventArgs $args): void
81    {
82        $object = $args->getObject();
83
84        if (!$object instanceof Reponse && !$object instanceof Mesurement) {
85            return;
86        }
87
88        $args->getObjectManager()->refresh($object);
89
90        switch (true) {
91            case $object instanceof Reponse:
92                $this->conformiteTraitementCompletion->setCalculsConformite($object->getConformiteTraitement());
93                $args->getObjectManager()->persist($object->getConformiteTraitement());
94                break;
95            case $object instanceof Mesurement:
96                foreach ($object->getConformiteTraitementReponses() as $reponse) {
97                    $this->conformiteTraitementCompletion->setCalculsConformite($reponse->getConformiteTraitement());
98                    $args->getObjectManager()->persist($reponse->getConformiteTraitement());
99                }
100                break;
101        }
102
103        $args->getObjectManager()->flush();
104    }
105}