Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
18.18% covered (danger)
18.18%
6 / 33
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConformiteTraitementExtension
18.18% covered (danger)
18.18%
6 / 33
20.00% covered (danger)
20.00%
1 / 5
105.56
0.00% covered (danger)
0.00%
0 / 1
 getFunctions
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 orderReponseByQuestionPositionAsc
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 getPlanifiedMesurements
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 getConformiteLevelWeight
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getConformiteTraitementLabel
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
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\Twig\Extension;
25
26use App\Domain\Registry\Calculator\Completion\ConformiteTraitementCompletion;
27use App\Domain\Registry\Dictionary\ConformiteTraitementLevelDictionary;
28use App\Domain\Registry\Model\ConformiteTraitement\ConformiteTraitement;
29use App\Domain\Registry\Model\ConformiteTraitement\Reponse;
30use Symfony\Component\Form\FormView;
31use Twig\Extension\AbstractExtension;
32use Twig\TwigFunction;
33
34class ConformiteTraitementExtension extends AbstractExtension
35{
36    /**
37     * @return array|TwigFunction[]
38     */
39    public function getFunctions()
40    {
41        return [
42            new TwigFunction('orderReponseByQuestionPositionAsc', [$this, 'orderReponseByQuestionPositionAsc']),
43            new TwigFunction('getPlanifiedMesurements', [$this, 'getPlanifiedMesurements']),
44            new TwigFunction('getConformiteLevelWeight', [$this, 'getConformiteLevelWeight']),
45            new TwigFunction('getConformiteTraitementLabel', [$this, 'getConformiteTraitementLabel']),
46        ];
47    }
48
49    public function orderReponseByQuestionPositionAsc(FormView $formView): array
50    {
51        $ordered = [];
52        foreach ($formView->children as $formViewReponse) {
53            $reponse = $formViewReponse->vars['value'];
54            if (!$reponse instanceof Reponse) {
55                continue;
56            }
57
58            $ordered[$reponse->getQuestion()->getPosition()] = $formViewReponse;
59        }
60
61        \ksort($ordered);
62
63        return $ordered;
64    }
65
66    public function getPlanifiedMesurements(ConformiteTraitement $conformiteTraitement): array
67    {
68        $planifiedMesurementsToBeNotified = [];
69        foreach ($conformiteTraitement->getReponses() as $reponse) {
70            $mesurements = \iterable_to_array($reponse->getActionProtectionsPlanifiedNotSeens());
71            foreach ($mesurements as $mesurement) {
72                if (!\in_array($mesurement, $planifiedMesurementsToBeNotified)) {
73                    \array_push($planifiedMesurementsToBeNotified, $mesurement);
74                }
75            }
76        }
77
78        return $planifiedMesurementsToBeNotified;
79    }
80
81    public function getConformiteLevelWeight(ConformiteTraitement $conformiteTraitement): int
82    {
83        $level = ConformiteTraitementCompletion::getConformiteTraitementLevel($conformiteTraitement);
84
85        return ConformiteTraitementLevelDictionary::getConformitesWeight()[$level];
86    }
87
88    public function getConformiteTraitementLabel(ConformiteTraitement $conformiteTraitement): string
89    {
90        $level = $this->getConformiteLevelWeight($conformiteTraitement);
91        $label = ConformiteTraitementLevelDictionary::getConformites()[array_flip(ConformiteTraitementLevelDictionary::getConformitesWeight())[$level]];
92        switch ($level) {
93            case 1:
94                $color = 'label-success';
95                break;
96            case 2:
97                $color = 'label-warning';
98                break;
99            default:
100                $color = 'label-danger';
101        }
102
103        return '<span class="label ' . $color . '" style="display: inline-block;">' . $label . '</span>';
104    }
105}