Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.38% covered (warning)
75.38%
49 / 65
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AdminMetric
75.38% covered (warning)
75.38%
49 / 65
66.67% covered (warning)
66.67%
2 / 3
14.15
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getData
72.41% covered (warning)
72.41%
42 / 58
0.00% covered (danger)
0.00%
0 / 1
12.10
 getTemplateViewName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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\Reporting\Metrics;
25
26use App\Domain\Maturity;
27use App\Domain\Maturity\Model\Referentiel;
28use App\Domain\Registry;
29use App\Domain\User;
30use App\Domain\User\Dictionary\CollectivityTypeDictionary;
31use App\Domain\User\Dictionary\UserRoleDictionary;
32use Symfony\Component\Security\Core\Security;
33
34class AdminMetric implements MetricInterface
35{
36    /**
37     * @var User\Repository\Collectivity
38     */
39    private $collectivityRepository;
40
41    /**
42     * @var Registry\Repository\Mesurement
43     */
44    private $mesurementRepository;
45
46    /**
47     * @var Registry\Repository\Proof
48     */
49    private $proofRepository;
50
51    /**
52     * @var Maturity\Repository\Survey
53     */
54    private $surveyRepository;
55
56    /**
57     * @var Registry\Repository\Treatment
58     */
59    private $treatmentRepository;
60
61    /**
62     * @var Security
63     */
64    private $security;
65
66    public function __construct(
67        User\Repository\Collectivity $collectivityRepository,
68        Registry\Repository\Mesurement $mesurementRepository,
69        Registry\Repository\Proof $proofRepository,
70        Maturity\Repository\Survey $surveyRepository,
71        Registry\Repository\Treatment $treatmentRepository,
72        Security $security,
73    ) {
74        $this->collectivityRepository = $collectivityRepository;
75        $this->mesurementRepository   = $mesurementRepository;
76        $this->proofRepository        = $proofRepository;
77        $this->surveyRepository       = $surveyRepository;
78        $this->treatmentRepository    = $treatmentRepository;
79        $this->security               = $security;
80    }
81
82    public function getData(?Referentiel $referentiel = null): array
83    {
84        if (!$this->security->isGranted(UserRoleDictionary::ROLE_ADMIN)) {
85            $collectivities = $this->collectivityRepository->findByUserReferent($this->security->getUser());
86        } else {
87            $collectivities = $this->collectivityRepository->findAllActive();
88        }
89
90        $collectivityByType = [];
91        foreach (CollectivityTypeDictionary::getTypesKeys() as $type) {
92            $collectivityByType[$type] = 0;
93        }
94
95        $averageMesurement       = floatval($this->mesurementRepository->planifiedAverageOnAllCollectivity($collectivities));
96        $averageProof            = floatval($this->proofRepository->averageProofFiled($collectivities));
97        $averageBalanceSheetPoof = floatval($this->proofRepository->averageBalanceSheetProof($collectivities));
98        $averageSurveyLastYer    = floatval($this->surveyRepository->averageSurveyDuringLastYear($collectivities));
99
100        $totalCollectivity = count($collectivities);
101
102        $data = [
103            'collectivityByType' => [
104                'value' => [
105                    'all'  => $totalCollectivity,
106                    'type' => $collectivityByType,
107                ],
108            ],
109            'collectivityByAddressInsee' => [
110                'value' => [
111                    'all'          => $totalCollectivity,
112                    'addressInsee' => [],
113                    'dpoPercent'   => 0,
114                ],
115            ],
116            'mesurementByCollectivity' => [
117                'average' => $averageMesurement,
118            ],
119            'proofByCollectivity' => [
120                'average' => $averageProof,
121            ],
122            'balanceSheetProofByCollectivity' => [
123                'average' => $averageBalanceSheetPoof * 100,
124            ],
125            'surveyLastYear' => [
126                'average' => $averageSurveyLastYer * 100,
127            ],
128        ];
129
130        $nbIsDifferentDpo = 0;
131        $inseeValidType   = [CollectivityTypeDictionary::TYPE_COMMUNE, CollectivityTypeDictionary::TYPE_CCAS, CollectivityTypeDictionary::TYPE_CIAS, CollectivityTypeDictionary::TYPE_MEDICAL_INSTITUTION, CollectivityTypeDictionary::TYPE_OTHER];
132        foreach ($collectivities as $collectivity) {
133            if (!\is_null($collectivity->getAddress())
134                && !\is_null($collectivity->getAddress()->getInsee())
135                && \in_array($collectivity->getType(), $inseeValidType)
136            ) {
137                $collectivityData = [
138                    'name'                => $collectivity->getShortName(),
139                    'nbTraitementActifs'  => intval($this->treatmentRepository->countAllActiveByCollectivity($collectivity)),
140                    'nbActionsProtection' => intval($this->mesurementRepository->countAppliedByCollectivity($collectivity)),
141                ];
142                $collectivityInsee                                                                 = $collectivity->getAddress()->getInsee();
143                $data['collectivityByAddressInsee']['value']['addressInsee'][$collectivityInsee][] = $collectivityData;
144            }
145
146            if (false === $collectivity->isDifferentDpo()) {
147                ++$nbIsDifferentDpo;
148            }
149
150            if (isset($data['collectivityByType']['value']['type'][$collectivity->getType()])) {
151                ++$data['collectivityByType']['value']['type'][$collectivity->getType()];
152            }
153        }
154
155        if ($totalCollectivity > 0) {
156            $data['collectivityByAddressInsee']['value']['dpoPercent'] = floor(($nbIsDifferentDpo * 100) / $totalCollectivity);
157        }
158
159        return $data;
160    }
161
162    public function getTemplateViewName(): string
163    {
164        return 'Reporting/Dashboard/index_admin.html.twig';
165    }
166}