Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.47% covered (warning)
83.47%
101 / 121
61.54% covered (warning)
61.54%
8 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
WordHandler
83.47% covered (warning)
83.47%
101 / 121
61.54% covered (warning)
61.54%
8 / 13
19.46
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 setDocument
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setCollectivity
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 generateOverviewReport
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
4
 generateRegistryContractorReport
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 generateMaturitySurveyReport
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 generateRegistryMesurementReport
76.92% covered (warning)
76.92%
10 / 13
0.00% covered (danger)
0.00%
0 / 1
3.11
 generateRegistryRequestReport
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 generateRegistryTreatmentReport
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 generateRegistryToolReport
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 generateRegistryViolationReport
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 generateRegistryConformiteTraitementReport
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 generateRegistryConformiteOrganisationReport
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
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\Handler;
25
26use App\Application\Symfony\Security\UserProvider;
27use App\Domain\Registry\Model\ConformiteOrganisation\Evaluation;
28use App\Domain\Reporting\Generator\Word\ConformiteOrganisationGenerator;
29use App\Domain\Reporting\Generator\Word\ConformiteTraitementGenerator;
30use App\Domain\Reporting\Generator\Word\ContractorGenerator;
31use App\Domain\Reporting\Generator\Word\MaturityGenerator;
32use App\Domain\Reporting\Generator\Word\MesurementGenerator;
33use App\Domain\Reporting\Generator\Word\OverviewGenerator;
34use App\Domain\Reporting\Generator\Word\RequestGenerator;
35use App\Domain\Reporting\Generator\Word\ToolGenerator;
36use App\Domain\Reporting\Generator\Word\TreatmentGenerator;
37use App\Domain\Reporting\Generator\Word\ViolationGenerator;
38use App\Domain\User\Model\Collectivity;
39use PhpOffice\PhpWord\PhpWord;
40use Symfony\Component\HttpFoundation\BinaryFileResponse;
41use Symfony\Component\HttpFoundation\Response;
42use Symfony\Component\String\Slugger\AsciiSlugger;
43
44class WordHandler
45{
46    /**
47     * @var PhpWord
48     */
49    private $document;
50
51    /**
52     * @var ContractorGenerator
53     */
54    private $contractorGenerator;
55
56    /**
57     * @var OverviewGenerator
58     */
59    private $overviewGenerator;
60
61    /**
62     * @var MaturityGenerator
63     */
64    private $maturityGenerator;
65
66    /**
67     * @var MesurementGenerator
68     */
69    private $mesurementGenerator;
70
71    /**
72     * @var RequestGenerator
73     */
74    private $requestGenerator;
75
76    /**
77     * @var TreatmentGenerator
78     */
79    private $treatmentGenerator;
80
81    /**
82     * @var ToolGenerator
83     */
84    private $toolGenerator;
85
86    /**
87     * @var ViolationGenerator
88     */
89    private $violationGenerator;
90
91    /**
92     * @var ConformiteTraitementGenerator
93     */
94    private $conformiteTraitementGenerator;
95
96    /**
97     * @var ConformiteOrganisationGenerator
98     */
99    private $conformiteOrganisationGenerator;
100
101    /**
102     * @var Collectivity|null
103     */
104    protected $collectivity;
105
106    /**
107     * @var UserProvider
108     */
109    protected $userProvider;
110
111    public function __construct(
112        PhpWord $document,
113        ContractorGenerator $contractorGenerator,
114        OverviewGenerator $overviewGenerator,
115        MaturityGenerator $maturityGenerator,
116        MesurementGenerator $mesurementGenerator,
117        RequestGenerator $requestGenerator,
118        TreatmentGenerator $treatmentGenerator,
119        ViolationGenerator $violationGenerator,
120        ToolGenerator $toolGenerator,
121        ConformiteTraitementGenerator $conformiteTraitementGenerator,
122        ConformiteOrganisationGenerator $conformiteOrganisationGenerator,
123        UserProvider $userProvider,
124    ) {
125        $this->document                        = $document;
126        $this->contractorGenerator             = $contractorGenerator;
127        $this->overviewGenerator               = $overviewGenerator;
128        $this->maturityGenerator               = $maturityGenerator;
129        $this->mesurementGenerator             = $mesurementGenerator;
130        $this->requestGenerator                = $requestGenerator;
131        $this->treatmentGenerator              = $treatmentGenerator;
132        $this->violationGenerator              = $violationGenerator;
133        $this->toolGenerator                   = $toolGenerator;
134        $this->conformiteTraitementGenerator   = $conformiteTraitementGenerator;
135        $this->conformiteOrganisationGenerator = $conformiteOrganisationGenerator;
136        $this->userProvider                    = $userProvider;
137    }
138
139    public function setDocument(PhpWord $document)
140    {
141        $this->document = $document;
142    }
143
144    public function setCollectivity(Collectivity $collectivity)
145    {
146        $this->collectivity = $collectivity;
147        $this->contractorGenerator->setCollectivity($this->collectivity);
148        $this->overviewGenerator->setCollectivity($this->collectivity);
149        $this->maturityGenerator->setCollectivity($this->collectivity);
150        $this->mesurementGenerator->setCollectivity($this->collectivity);
151        $this->requestGenerator->setCollectivity($this->collectivity);
152        $this->treatmentGenerator->setCollectivity($this->collectivity);
153        $this->violationGenerator->setCollectivity($this->collectivity);
154        $this->toolGenerator->setCollectivity($this->collectivity);
155        $this->conformiteTraitementGenerator->setCollectivity($this->collectivity);
156        $this->conformiteOrganisationGenerator->setCollectivity($this->collectivity);
157    }
158
159    /**
160     * Generate overview report.
161     *
162     * @param array $treatments  Treatments used for overview report generation
163     * @param array $contractors Contractors used for overview report generation
164     * @param array $mesurements Mesurements used for overview report generation
165     * @param array $maturity    Surveys maturity used for overview report generation
166     * @param array $requests    Requests used for overview report generation
167     * @param array $violations  Violations used for overview report generation
168     * @param array $tools       Tools used for overview report generation
169     *
170     * @throws \PhpOffice\PhpWord\Exception\Exception
171     *
172     * @return BinaryFileResponse The generated Word document
173     */
174    public function generateOverviewReport(
175        array $treatments = [],
176        array $contractors = [],
177        array $mesurements = [],
178        array $maturity = [],
179        array $requests = [],
180        array $violations = [],
181        array $tools = [],
182        ?Evaluation $evaluation = null,
183    ): BinaryFileResponse {
184        if ($this->collectivity) {
185            $collectivity = $this->collectivity;
186        } else {
187            $collectivity = $this->userProvider->getAuthenticatedUser()->getCollectivity();
188        }
189
190        $title = 'Bilan de gestion des données Ã  caractère personnel';
191        if ($collectivity->getReviewData() && $collectivity->getReviewData()->getDocumentName()) {
192            $title = $collectivity->getReviewData()->getDocumentName();
193        }
194
195        $this->overviewGenerator->setCollectivity($collectivity);
196        // Initialize document
197        $this->overviewGenerator->initializeDocument($this->document);
198
199        // Begin generation
200        $this->overviewGenerator->addHomepage($this->document, $title);
201
202        // Section which will get whole content
203        $contentSection = $this->overviewGenerator->createContentSection($this->document, $title);
204
205        // Table of content
206        $this->overviewGenerator->addTableOfContent($contentSection, 9);
207
208        // Content
209        $this->overviewGenerator->generateObjectPart($contentSection);
210        $this->overviewGenerator->generateOrganismIntroductionPart($contentSection);
211        $this->overviewGenerator->generateRegistries($contentSection, $treatments, $contractors, $requests, $violations, $tools);
212        $this->overviewGenerator->generateManagementSystemAndCompliance($contentSection, $maturity, $treatments, $mesurements, $evaluation);
213        $this->overviewGenerator->generateContinuousImprovements($contentSection);
214        $this->overviewGenerator->generateAnnexeMention($this->document, $contentSection, $treatments, $violations, $mesurements);
215
216        $slugger = new AsciiSlugger();
217        $cname   = 'bilan-' . $slugger->slug($collectivity->getShortName())->lower()->toString();
218
219        return $this->overviewGenerator->generateResponse($this->document, $cname);
220    }
221
222    /**
223     * Generate contractor report.
224     *
225     * @param array $contractors contractors to use for generation
226     *
227     * @throws \PhpOffice\PhpWord\Exception\Exception
228     * @throws \Exception
229     *
230     * @return Response The generated Word file
231     */
232    public function generateRegistryContractorReport(array $contractors = []): Response
233    {
234        $title = 'Registre des sous-traitants';
235        // Initialize document
236        $this->contractorGenerator->initializeDocument($this->document);
237
238        // Begin generation
239        $this->contractorGenerator->addHomepage($this->document, $title);
240
241        // Section which will get whole content
242        $contentSection = $this->contractorGenerator->createContentSection($this->document, $title);
243
244        // Table of content
245        $this->contractorGenerator->addTableOfContent($contentSection, 1);
246
247        // Content
248        $this->contractorGenerator->addSyntheticView($contentSection, $contractors);
249        $this->contractorGenerator->addDetailedView($contentSection, $contractors);
250
251        return $this->contractorGenerator->generateResponse($this->document, 'sous_traitants');
252    }
253
254    /**
255     * Generate maturity report.
256     *
257     * @param array $maturityList list of maturity to use for generation. First (old) if exists and second (new)
258     *
259     * @throws \PhpOffice\PhpWord\Exception\Exception
260     * @throws \Exception
261     *
262     * @return Response The generated Word file
263     */
264    public function generateMaturitySurveyReport(array $maturityList = []): Response
265    {
266        $title = 'Indice de maturité';
267        // Initialize document
268        $this->maturityGenerator->initializeDocument($this->document);
269
270        // Begin generation
271        $this->maturityGenerator->addHomepage($this->document, $title);
272
273        // Section which will get whole content
274        $contentSection = $this->maturityGenerator->createContentSection($this->document, $title);
275
276        // Table of content
277        $this->maturityGenerator->addTableOfContent($contentSection, 1);
278
279        // Content
280        $this->maturityGenerator->addContextView($contentSection, $maturityList);
281        $this->maturityGenerator->addSyntheticView($contentSection, $maturityList);
282        $this->maturityGenerator->addDetailedView($contentSection, $maturityList);
283
284        return $this->maturityGenerator->generateResponse($this->document, 'indice_de_maturite');
285    }
286
287    /**
288     * Generate mesurement report.
289     *
290     * @param array $mesurements mesurements to use for generation
291     *
292     * @throws \PhpOffice\PhpWord\Exception\Exception
293     * @throws \Exception
294     *
295     * @return Response The generated Word file
296     */
297    public function generateRegistryMesurementReport(array $mesurements = [], $actionPlan = false): Response
298    {
299        if ($actionPlan) {
300            $title = 'Registre des actions de protection planifiées';
301        } else {
302            $title = 'Registre des actions de protection';
303        }
304
305        // Initialize document
306        $this->mesurementGenerator->initializeDocument($this->document);
307
308        // Begin generation
309        $this->mesurementGenerator->addHomepage($this->document, $title);
310
311        // Section which will get whole content
312        $contentSection = $this->mesurementGenerator->createContentSection($this->document, $title);
313
314        // Table of content
315        $this->mesurementGenerator->addTableOfContent($contentSection, 1);
316
317        // Content
318        if ($actionPlan) {
319            $this->mesurementGenerator->addActionPlanSyntheticView($contentSection, $mesurements);
320            $this->mesurementGenerator->addDetailedView($contentSection, $mesurements, true);
321        } else {
322            $this->mesurementGenerator->addSyntheticView($contentSection, $mesurements);
323            $this->mesurementGenerator->addDetailedView($contentSection, $mesurements);
324        }
325
326        return $this->mesurementGenerator->generateResponse($this->document, 'actions_de_protection');
327    }
328
329    /**
330     * Generate request report.
331     *
332     * @param array $requests requests to use for generation
333     *
334     * @throws \PhpOffice\PhpWord\Exception\Exception
335     * @throws \Exception
336     *
337     * @return Response The generated Word file
338     */
339    public function generateRegistryRequestReport(array $requests = []): Response
340    {
341        $title = 'Registre des demandes des personnes concernées';
342        // Initialize document
343        $this->requestGenerator->initializeDocument($this->document);
344
345        // Begin generation
346        $this->requestGenerator->addHomepage($this->document, $title);
347
348        // Section which will get whole content
349        $contentSection = $this->requestGenerator->createContentSection($this->document, $title);
350
351        // Table of content
352        $this->requestGenerator->addTableOfContent($contentSection, 1);
353
354        // Content
355        $this->requestGenerator->addSyntheticView($contentSection, $requests);
356        $this->requestGenerator->addDetailedView($contentSection, $requests);
357
358        return $this->requestGenerator->generateResponse($this->document, 'demandes_des_personnes_concernees');
359    }
360
361    /**
362     * Generate treatment report.
363     *
364     * @param array $treatments treatments to use for generation
365     *
366     * @throws \PhpOffice\PhpWord\Exception\Exception
367     * @throws \Exception
368     *
369     * @return Response The generated Word file
370     */
371    public function generateRegistryTreatmentReport(array $treatments = [])
372    {
373        $title = 'Registre des traitements';
374
375        // Initialize document
376        $this->treatmentGenerator->initializeDocument($this->document);
377
378        // Begin generation
379        $this->treatmentGenerator->addHomepage($this->document, $title);
380
381        // Section which will get whole content
382        $contentSection = $this->treatmentGenerator->createContentSection($this->document, $title);
383
384        // Table of content
385        $this->treatmentGenerator->addTableOfContent($contentSection, 1);
386
387        // Content
388        $this->treatmentGenerator->addSyntheticView($contentSection, $treatments);
389        $this->treatmentGenerator->addDetailedView($contentSection, $treatments);
390
391        return $this->treatmentGenerator->generateResponse($this->document, 'traitements');
392    }
393
394    /**
395     * Generate tool report.
396     *
397     * @param array $tools treatments to use for generation
398     *
399     * @throws \PhpOffice\PhpWord\Exception\Exception
400     * @throws \Exception
401     *
402     * @return Response The generated Word file
403     */
404    public function generateRegistryToolReport(array $tools = [], ?Collectivity $collectivity = null)
405    {
406        $title = 'Registre des Logiciels et supports';
407
408        // Initialize document
409        $this->toolGenerator->initializeDocument($this->document);
410
411        // Begin generation
412        $this->toolGenerator->addHomepage($this->document, $title);
413
414        // Section which will get whole content
415        $contentSection = $this->toolGenerator->createContentSection($this->document, $title);
416        // Table of content
417        $this->toolGenerator->addTableOfContent($contentSection, 1);
418
419        // Content
420        $this->toolGenerator->addSyntheticView($contentSection, $tools, false, false, $collectivity);
421        $this->toolGenerator->addDetailedView($contentSection, $tools);
422
423        return $this->toolGenerator->generateResponse($this->document, 'logiciels-et-supports');
424    }
425
426    /**
427     * Generate violation report.
428     *
429     * @param array $treatments treatments to use for generation
430     *
431     * @throws \PhpOffice\PhpWord\Exception\Exception
432     * @throws \Exception
433     *
434     * @return Response The generated Word file
435     */
436    public function generateRegistryViolationReport(array $treatments = [])
437    {
438        $title = 'Registre des violations';
439
440        // Initialize document
441        $this->violationGenerator->initializeDocument($this->document);
442
443        // Begin generation
444        $this->violationGenerator->addHomepage($this->document, $title);
445
446        // Section which will get whole content
447        $contentSection = $this->violationGenerator->createContentSection($this->document, $title);
448
449        // Table of content
450        $this->violationGenerator->addTableOfContent($contentSection, 1);
451
452        // Content
453        $this->violationGenerator->addSyntheticView($contentSection, $treatments);
454        $this->violationGenerator->addDetailedView($contentSection, $treatments);
455
456        return $this->violationGenerator->generateResponse($this->document, 'violations');
457    }
458
459    /**
460     * Generate conformiteTraitement report.
461     *
462     * @param array $treatments treatments to use for generation conformite traitement
463     *
464     * @throws \Exception
465     * @throws \PhpOffice\PhpWord\Exception\Exception
466     *
467     * @return Response The generated Word file
468     */
469    public function generateRegistryConformiteTraitementReport(array $treatments = []): Response
470    {
471        $title = 'Diagnostic de la conformité des traitements';
472        // Initialize document
473        $this->conformiteTraitementGenerator->initializeDocument($this->document);
474
475        // Begin generation
476        $this->conformiteTraitementGenerator->addHomepage($this->document, $title);
477
478        // Section which will get whole content
479        $contentSection = $this->conformiteTraitementGenerator->createContentSection($this->document, $title);
480
481        // Table of content
482        $this->conformiteTraitementGenerator->addTableOfContent($contentSection, 1);
483
484        // Content
485        $this->conformiteTraitementGenerator->addSyntheticView($contentSection, $treatments);
486        $this->conformiteTraitementGenerator->addDetailedView($contentSection, $treatments);
487
488        return $this->conformiteTraitementGenerator->generateResponse($this->document, 'conformite_des_traitements');
489    }
490
491    public function generateRegistryConformiteOrganisationReport(Evaluation $evaluation, bool $withAllActions): Response
492    {
493        $title = 'Diagnostic de la conformite de la structure';
494
495        $this->conformiteOrganisationGenerator->initializeDocument($this->document);
496
497        /* Basic generation */
498        $this->conformiteOrganisationGenerator->addHomepage($this->document, $title);
499
500        $contentSection = $this->conformiteOrganisationGenerator->createContentSection($this->document, $title);
501
502        /* Table of content */
503        $this->conformiteOrganisationGenerator->addTableOfContent($contentSection, 1);
504
505        /* Content */
506        $this->conformiteOrganisationGenerator->addDetailedView($contentSection, [$evaluation, $withAllActions]);
507
508        return $this->conformiteOrganisationGenerator->generateResponse($this->document, 'conformite_des_organisations');
509    }
510}