Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 135
0.00% covered (danger)
0.00%
0 / 18
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContractorGenerator
0.00% covered (danger)
0.00%
0 / 135
0.00% covered (danger)
0.00%
0 / 18
2162
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 initializeExtract
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
12
 collectivityHeaders
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 initializeCollectivity
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 contractorGeneralInformationsHeaders
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 initializeContractorGeneralInformations
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 contractorDpoHeaders
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 initializeContractorDpo
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
240
 treatmentRecipientsHeaders
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 initializeTreatmentRecipients
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 contractorHistoricHeaders
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 initializeContractorHistoric
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 contractorCoordinatesHeaders
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 initializeContractorCoordinates
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
90
 contractorProofHeaders
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 initializeProof
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 contractorTreatmentHeaders
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 initializeTreatment
0.00% covered (danger)
0.00%
0 / 3
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\Generator\Csv;
25
26use App\Domain\User\Dictionary\ContactCivilityDictionary;
27use App\Domain\User\Repository\Collectivity;
28use App\Infrastructure\ORM\Registry\Repository\Contractor;
29use Symfony\Component\Security\Core\Security;
30use Symfony\Contracts\Translation\TranslatorInterface;
31
32class ContractorGenerator extends AbstractGenerator
33{
34    /**
35     * @var TranslatorInterface
36     */
37    private $translator;
38
39    /**
40     * @var Collectivity
41     */
42    private $collectivityRepository;
43
44    /**
45     * @var Contractor
46     */
47    private $contractorRepository;
48
49    /**
50     * @var Security
51     */
52    private $security;
53
54    /**
55     * @var string
56     */
57    private $yes;
58
59    /**
60     * @var string
61     */
62    private $no;
63
64    public function __construct(
65        TranslatorInterface $translatorInterface,
66        Collectivity $collectivityRepository,
67        Contractor $contractorRepository,
68        Security $security,
69    ) {
70        $this->translator             = $translatorInterface;
71        $this->collectivityRepository = $collectivityRepository;
72        $this->contractorRepository   = $contractorRepository;
73        $this->security               = $security;
74    }
75
76    public function initializeExtract(): array
77    {
78        $this->yes = $this->translator->trans('global.label.yes');
79        $this->no  = $this->translator->trans('global.label.no');
80
81        $headers = array_merge(
82            ['Nom'],
83            $this->collectivityHeaders(),
84            $this->contractorGeneralInformationsHeaders(),
85            $this->contractorDpoHeaders(),
86            $this->contractorHistoricHeaders(),
87            $this->contractorCoordinatesHeaders(),
88            $this->contractorProofHeaders(),
89            $this->contractorTreatmentHeaders(),
90        );
91        $data = [$headers];
92
93        $user = null;
94        if (!$this->security->isGranted('ROLE_ADMIN')) {
95            $user = $this->security->getUser();
96        }
97
98        /** @var \App\Domain\Registry\Model\Contractor $contractor */
99        foreach ($this->contractorRepository->findAllByActiveCollectivity(true, $user) as $contractor) {
100            $extract = array_merge(
101                [$contractor->getName()],
102                $this->initializeCollectivity($contractor->getCollectivity()),
103                $this->initializeContractorGeneralInformations($contractor),
104                $this->initializeContractorDpo($contractor),
105                $this->initializeContractorHistoric($contractor),
106                $this->initializeContractorCoordinates($contractor),
107                $this->initializeProof($contractor),
108                $this->initializeTreatment($contractor),
109            );
110            array_push($data, $extract);
111        }
112
113        return $data;
114    }
115
116    private function collectivityHeaders(): array
117    {
118        $collectivityTrans = $this->translator->trans('global.label.organization');
119
120        return [
121            $collectivityTrans . ' - ' . $this->translator->trans('user.organization.label.name'),
122            $collectivityTrans . ' - ' . $this->translator->trans('user.organization.label.siren'),
123            $collectivityTrans . ' - ' . $this->translator->trans('global.label.address.insee'),
124        ];
125    }
126
127    private function initializeCollectivity(\App\Domain\User\Model\Collectivity $collectivity)
128    {
129        return [
130            $collectivity->getName(),
131            $collectivity->getSiren(),
132            $collectivity->getAddress()->getInsee(),
133        ];
134    }
135
136    private function contractorGeneralInformationsHeaders()
137    {
138        return [
139            $this->translator->trans('registry.contractor.label.referent'),
140            $this->translator->trans('registry.contractor.label.contractual_clauses_verified'),
141            $this->translator->trans('registry.contractor.label.adopted_security_features'),
142            $this->translator->trans('registry.contractor.label.maintains_treatment_register'),
143            $this->translator->trans('registry.contractor.label.sending_data_outside_eu'),
144            $this->translator->trans('registry.contractor.label.other_informations'),
145        ];
146    }
147
148    private function initializeContractorGeneralInformations(\App\Domain\Registry\Model\Contractor $contractor): array
149    {
150        return [
151            $contractor->getReferent(),
152            $contractor->isContractualClausesVerified() ? $this->yes : $this->no,
153            $contractor->isAdoptedSecurityFeatures() ? $this->yes : $this->no,
154            $contractor->isMaintainsTreatmentRegister() ? $this->yes : $this->no,
155            $contractor->isSendingDataOutsideEu() ? $this->yes : $this->no,
156            $contractor->getOtherInformations(),
157        ];
158    }
159
160    private function contractorDpoHeaders()
161    {
162        $dpo = $this->translator->trans('registry.contractor.tab.dpo');
163
164        return [
165            $dpo . ' - ' . $this->translator->trans('registry.contractor.label.has_dpo'),
166            $dpo . ' - ' . $this->translator->trans('global.label.contact.civility'),
167            $dpo . ' - ' . $this->translator->trans('global.label.contact.first_name'),
168            $dpo . ' - ' . $this->translator->trans('global.label.contact.last_name'),
169            $dpo . ' - ' . $this->translator->trans('global.label.contact.job'),
170            $dpo . ' - ' . $this->translator->trans('global.label.contact.email'),
171            $dpo . ' - ' . $this->translator->trans('global.label.contact.phone_number'),
172        ];
173    }
174
175    private function initializeContractorDpo(\App\Domain\Registry\Model\Contractor $contractor): array
176    {
177        $dpo = $contractor->getDpo();
178
179        return [
180            $contractor->isHasDpo() ? $this->yes : $this->no,
181            $contractor->isHasDpo() && !\is_null($dpo) && !\is_null($dpo->getCivility()) ? ContactCivilityDictionary::getCivilities()[$dpo->getCivility()] : null,
182            $contractor->isHasDpo() && !\is_null($dpo) ? $dpo->getFirstName() : null,
183            $contractor->isHasDpo() && !\is_null($dpo) ? $dpo->getLastName() : null,
184            $contractor->isHasDpo() && !\is_null($dpo) ? $dpo->getJob() : null,
185            $contractor->isHasDpo() && !\is_null($dpo) ? $dpo->getMail() : null,
186            $contractor->isHasDpo() && !\is_null($dpo) ? $dpo->getPhoneNumber() : null,
187        ];
188    }
189
190    private function treatmentRecipientsHeaders()
191    {
192        $recipientsTrans = $this->translator->trans('registry.treatment.tab.recipients');
193
194        return [
195            $recipientsTrans . ' - ' . $this->translator->trans('registry.treatment.label.recipient_category'),
196            $recipientsTrans . ' - ' . $this->translator->trans('global.label.linked_contractor'),
197        ];
198    }
199
200    private function initializeTreatmentRecipients(\App\Domain\Registry\Model\Treatment $treatment): array
201    {
202        return [
203            $treatment->getRecipientCategory(),
204            implode(' - ', \iterable_to_array($treatment->getContractors())),
205        ];
206    }
207
208    private function contractorHistoricHeaders()
209    {
210        $historicTrans = $this->translator->trans('global.tab.history');
211
212        return [
213            $historicTrans . ' - ' . $this->translator->trans('global.label.created_by'),
214            $historicTrans . ' - ' . $this->translator->trans('global.label.created_at'),
215            $historicTrans . ' - ' . $this->translator->trans('global.label.updated_at'),
216        ];
217    }
218
219    private function initializeContractorHistoric(\App\Domain\Registry\Model\Contractor $contractor): array
220    {
221        return [
222            strval($contractor->getCreator()),
223            $this->getDate($contractor->getCreatedAt()),
224            $this->getDate($contractor->getUpdatedAt()),
225        ];
226    }
227
228    private function contractorCoordinatesHeaders()
229    {
230        $coordinates = $this->translator->trans('registry.contractor.tab.coordinates');
231
232        return [
233            $coordinates . ' - ' . $this->translator->trans('global.label.contact.first_name'),
234            $coordinates . ' - ' . $this->translator->trans('global.label.contact.last_name'),
235            $coordinates . ' - ' . $this->translator->trans('global.label.address.line_one'),
236            $coordinates . ' - ' . $this->translator->trans('global.label.address.line_two'),
237            $coordinates . ' - ' . $this->translator->trans('global.label.address.zip_code'),
238            $coordinates . ' - ' . $this->translator->trans('global.label.address.city'),
239            $coordinates . ' - ' . $this->translator->trans('global.label.contact.email'),
240            $coordinates . ' - ' . $this->translator->trans('global.label.contact.phone_number'),
241        ];
242    }
243
244    private function initializeContractorCoordinates(\App\Domain\Registry\Model\Contractor $contractor): array
245    {
246        $legalManager = $contractor->getLegalManager();
247        $address      = $contractor->getAddress();
248
249        return [
250            !\is_null($legalManager) ? $legalManager->getFirstName() : null,
251            !\is_null($legalManager) ? $legalManager->getLastName() : null,
252            !\is_null($address) ? $address->getLineOne() : null,
253            !\is_null($address) ? $address->getLineTwo() : null,
254            !\is_null($address) ? $address->getZipCode() : null,
255            !\is_null($address) ? $address->getCity() : null,
256            !\is_null($address) ? $address->getMail() : null,
257            !\is_null($address) ? $address->getPhoneNumber() : null,
258        ];
259    }
260
261    private function contractorProofHeaders()
262    {
263        return [
264            $this->translator->trans('global.label.linked_proof'),
265        ];
266    }
267
268    private function initializeProof(\App\Domain\Registry\Model\Contractor $contractor): array
269    {
270        return [
271            implode(' - ', \iterable_to_array($contractor->getProofs())),
272        ];
273    }
274
275    private function contractorTreatmentHeaders()
276    {
277        return [
278            $this->translator->trans('global.label.linked_treatment'),
279        ];
280    }
281
282    private function initializeTreatment(\App\Domain\Registry\Model\Contractor $contractor): array
283    {
284        return [
285            implode(' - ', \iterable_to_array($contractor->getTreatments())),
286        ];
287    }
288}