Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 150
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
MaturityGenerator
0.00% covered (danger)
0.00%
0 / 150
0.00% covered (danger)
0.00%
0 / 3
1122
0.00% covered (danger)
0.00%
0 / 1
 addContextView
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 1
90
 addSyntheticView
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 1
210
 addDetailedView
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
110
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\Generator\Word;
25
26use App\Domain\Maturity\Model\Answer;
27use App\Domain\Maturity\Model\OptionalAnswer;
28use App\Domain\Maturity\Model\Survey;
29use App\Domain\Registry\Model\Mesurement;
30use PhpOffice\PhpWord\Element\Section;
31use PhpOffice\PhpWord\Shared\Converter;
32use PhpOffice\PhpWord\Style\Table;
33
34class MaturityGenerator extends AbstractGenerator implements ImpressionGeneratorInterface
35{
36    public function addContextView(Section $section, array $data): void
37    {
38        if (isset($data['bilanReport']) && $data['bilanReport']) {
39            $section->addTitle('Évaluation de la mise en conformité', 2);
40            if (!isset($data['new'])) {
41                $section->addText('Aucun indice de maturité à l\'heure actuelle.');
42
43                return;
44            }
45
46            $section->addTitle($data['new']->getReferentiel()->getName(), 3);
47
48            if (null !== $data['new']->getReferentiel()->getDescription()) {
49                $section->addText($data['new']->getReferentiel()->getDescription());
50            }
51
52            $section->addText('Score de l\'indice de maturité : ' . round($data['new']->getScore() / 10, 1));
53
54            if (isset($data['old'])) {
55                $section->addText('Score de l\'indice de maturité précédent : ' . round($data['old']->getScore() / 10, 1));
56            }
57        } else {
58            if (!isset($data['new'])) {
59                $section->addText('Aucun indice de maturité à l\'heure actuelle.');
60
61                return;
62            }
63
64            $section->addTitle('Contexte', 1);
65
66            $table = $section->addTable([
67                'borderColor' => '006699',
68                'borderSize'  => 6,
69                'cellMargin'  => 100,
70                'layout'      => Table::LAYOUT_FIXED,
71            ]);
72
73            $cellStyle = ['bgColor' => '3c8dbc'];
74            $textStyle = ['bold' => true, 'color' => 'ffffff'];
75            $table->addRow();
76            $cell = $table->addCell(3000, $cellStyle);
77            $cell->addText('Référentiel', $textStyle);
78            $cell = $table->addCell(7000);
79            $cell->addText($data['new']->getReferentiel()->getName());
80            $table->addRow();
81            $cell = $table->addCell(3000, $cellStyle);
82            $cell->addText('Description', $textStyle);
83            $cell = $table->addCell(7000);
84            $cell->addText($data['new']->getReferentiel()->getDescription());
85            $table->addRow();
86            $cell = $table->addCell(3000, $cellStyle);
87            $cell->addText('Date de l\'indice de maturité', $textStyle);
88            $cell = $table->addCell(7000);
89            $cell->addText($data['new']->getCreatedAt()->format('d/m/Y'));
90            $table->addRow();
91            $cell = $table->addCell(3000, $cellStyle);
92            $cell->addText('Score de l\'indice de maturité', $textStyle);
93            $cell = $table->addCell(7000);
94            $cell->addText(round($data['new']->getScore() / 10, 1));
95
96            if (isset($data['old'])) {
97                $table->addRow();
98                $cell = $table->addCell(3000, $cellStyle);
99                $cell->addText('Date de l\'indice de maturité précédent', $textStyle);
100                $cell = $table->addCell(7000);
101                $cell->addText(isset($data['old']) ? $data['old']->getCreatedAt()->format('d/m/Y') : 'Aucun');
102                $table->addRow();
103                $cell = $table->addCell(3000, $cellStyle);
104                $cell->addText('Score de l\'indice de maturité précédent', $textStyle);
105                $cell = $table->addCell(7000);
106                $cell->addText(round($data['old']->getScore() / 10, 1));
107            }
108        }
109    }
110
111    public function addSyntheticView(Section $section, array $data): void
112    {
113        if (!isset($data['new'])) {
114            return;
115        }
116
117        $maturityList = [];
118        $domainsName  = [];
119        if (isset($data['old'])) {
120            foreach ($data['old']->getMaturity() as $maturity) {
121                $maturityList[$maturity->getDomain()->getPosition()]['old'] = $maturity->getScore();
122                if (!isset($domainsName[$maturity->getDomain()->getPosition()])) {
123                    $domainsName[$maturity->getDomain()->getPosition()] = $maturity->getDomain()->getName();
124                }
125            }
126        }
127        foreach ($data['new']->getMaturity() as $maturity) {
128            $maturityList[$maturity->getDomain()->getPosition()]['new'] = $maturity->getScore();
129            if (!isset($domainsName[$maturity->getDomain()->getPosition()])) {
130                $domainsName[$maturity->getDomain()->getPosition()] = $maturity->getDomain()->getName();
131            }
132        }
133        \ksort($maturityList);
134        \ksort($domainsName);
135
136        $serie1 = [];
137        $serie2 = [];
138
139        // Table header
140        $tableData = [
141            [
142                '',
143            ],
144        ];
145        if (isset($data['old'])) {
146            $tableData[0][] = $this->getDate($data['old']->getCreatedAt(), 'd/m/Y');
147        }
148        $tableData[0][] = $this->getDate($data['new']->getCreatedAt(), 'd/m/Y');
149        // Table data + radar data
150        foreach ($maturityList as $position => $score) {
151            $row   = [];
152            $row[] = $domainsName[$position];
153            if (isset($score['old'])) {
154                $row[]    = $score['old'] / 10; // Display comma with 1 digit precision
155                $serie2[] = $score['old'] / 10;
156            }
157            if (isset($score['new'])) {
158                $row[]       = $score['new'] / 10; // Display comma with 1 digit precision
159                $serie1[]    = $score['new'] / 10;
160                $tableData[] = $row;
161            }
162        }
163        // Display
164        if (!(isset($data['bilanReport']) && $data['bilanReport'])) {
165            $section->addTitle('Vue d\'ensemble', 1);
166        }
167
168        $this->addTable($section, $tableData, true, self::TABLE_ORIENTATION_HORIZONTAL);
169
170        $section->addTextBreak(2);
171
172        $chart = $section->addChart(
173            'radar',
174            $domainsName,
175            $serie1,
176            [
177                'height'            => Converter::cmToEmu(11),
178                'width'             => Converter::cmToEmu(15),
179                'showLegend'        => true,
180                'legendPosition'    => 'tr',
181                'showAxisLabels'    => true,
182                'categoryAxisTitle' => 'tt',
183                'minX'              => 0,
184                'minY'              => 0,
185                'maxY'              => 5,
186                'dataLabelOptions'  => [
187                    'showVal'     => false,
188                    'showCatName' => false,
189                ],
190            ],
191            $this->getDate($data['new']->getCreatedAt(), 'd/m/Y')
192        );
193
194        if (!empty($serie2)) {
195            $chart->addSeries(\array_keys($maturityList), $serie2, $this->getDate($data['old']->getCreatedAt(), 'd/m/Y'));
196        }
197
198        $section->addPageBreak();
199    }
200
201    public function addDetailedView(Section $section, array $data): void
202    {
203        $section->addTitle('Détail', 1);
204
205        // Order data
206        $ordered      = [];
207        $descriptions = [];
208        /**
209         * @var Survey $survey
210         */
211        $survey = $data['new'];
212        foreach ($survey->getAnswerSurveys() as $answerSurvey) {
213            $answer                                                                                     = $answerSurvey->getAnswer();
214            $ordered[$answer->getQuestion()->getDomain()->getName()][$answer->getQuestion()->getName()] = $answer;
215            $descriptions[$answer->getQuestion()->getDomain()->getName()]                               = $answer->getQuestion()->getDomain()->getDescription();
216        }
217        foreach ($survey->getOptionalAnswers() as $optionalAnswer) {
218            $ordered[$optionalAnswer->getQuestion()->getDomain()->getName()][$optionalAnswer->getQuestion()->getName()] = $optionalAnswer;
219            $descriptions[$optionalAnswer->getQuestion()->getDomain()->getName()]                                       = $optionalAnswer->getQuestion()->getDomain()->getDescription();
220        }
221
222        \ksort($ordered);
223        // Table Body
224        foreach ($ordered as $domainName => $questions) {
225            $section->addTitle($domainName, 3);
226            $section->addText($descriptions[$domainName]);
227            $parsedData = [['', 'Réponse', 'Préconisation', 'Actions de protection']];
228            \ksort($questions);
229            foreach ($questions as $questionName => $answer) {
230                $table   = [];
231                $table[] = $questionName;
232                /*
233                 * @var string                $newOld
234                 * @var Answer|OptionalAnswer $answer
235                 */
236                $table[1]    = is_a($answer, Answer::class) ? $answer->getName() : $answer->getQuestion()->getOptionReason();
237                $table[2]    = is_a($answer, Answer::class) ? $answer->getRecommendation() : '';
238                $mesurements = '';
239                if ($survey->getAnswerSurveys()) {
240                    foreach ($survey->getAnswerSurveys() as $answerSurvey) {
241                        if ($answerSurvey->getAnswer()->getId() === $answer->getId()) {
242                            $mesurements = join(', ', $answerSurvey->getMesurements()->map(function (Mesurement $mesurement) {
243                                return $mesurement->getName();
244                            })->toArray());
245                        }
246                    }
247                }
248                $table[3] = $mesurements;
249
250                \ksort($table);
251                $parsedData[] = $table;
252            }
253
254            $this->addTable($section, $parsedData, true, self::TABLE_ORIENTATION_VERTICAL);
255        }
256    }
257}