Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
71.00% covered (warning)
71.00%
71 / 100
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DuplicationType
71.00% covered (warning)
71.00%
71 / 100
66.67% covered (warning)
66.67%
2 / 3
8.20
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 buildForm
67.05% covered (warning)
67.05%
59 / 88
0.00% covered (danger)
0.00%
0 / 1
5.89
 configureOptions
100.00% covered (success)
100.00%
7 / 7
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\Admin\Form\Type;
25
26use App\Domain\Admin\Dictionary\DuplicationTargetOptionDictionary;
27use App\Domain\Admin\DTO\DuplicationFormDTO;
28use App\Domain\Registry\Repository\Contractor;
29use App\Domain\Registry\Repository\Mesurement;
30use App\Domain\Registry\Repository\Tool;
31use App\Domain\Registry\Repository\Treatment;
32use App\Domain\User\Model as UserModel;
33use App\Domain\User\Repository\Collectivity;
34use Doctrine\ORM\EntityRepository;
35use Knp\DictionaryBundle\Form\Type\DictionaryType;
36use Symfony\Bridge\Doctrine\Form\Type\EntityType;
37use Symfony\Component\Form\AbstractType;
38use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
39use Symfony\Component\Form\FormBuilderInterface;
40use Symfony\Component\Form\FormEvent;
41use Symfony\Component\Form\FormEvents;
42use Symfony\Component\OptionsResolver\OptionsResolver;
43
44class DuplicationType extends AbstractType
45{
46    private Treatment $treatmentRepository;
47    private Contractor $contractorRepository;
48    private Mesurement $mesurementRepository;
49    private Tool $toolRepository;
50    private Collectivity $collectivityRepository;
51
52    public function __construct(
53        Treatment $treatmentRepository,
54        Contractor $contractorRepository,
55        Mesurement $mesurementRepository,
56        Tool $toolRepository,
57        Collectivity $collectivityRepository,
58    ) {
59        $this->treatmentRepository    = $treatmentRepository;
60        $this->contractorRepository   = $contractorRepository;
61        $this->mesurementRepository   = $mesurementRepository;
62        $this->toolRepository         = $toolRepository;
63        $this->collectivityRepository = $collectivityRepository;
64    }
65
66    /**
67     * Build type form.
68     */
69    public function buildForm(FormBuilderInterface $builder, array $options)
70    {
71        $builder
72            ->add('type', DictionaryType::class, [
73                'name'     => 'admin_duplication_type',
74                'label'    => 'admin.duplication.label.type',
75                'required' => true,
76                'multiple' => false,
77                'expanded' => true,
78            ])
79            ->add('sourceCollectivity', EntityType::class, [
80                'class'         => UserModel\Collectivity::class,
81                'label'         => 'admin.duplication.label.source_organization',
82                'query_builder' => function (EntityRepository $er) {
83                    return $er->createQueryBuilder('c')
84                        ->orderBy('c.name', 'ASC');
85                },
86                'required' => true,
87                'multiple' => false,
88                'expanded' => false,
89            ])
90            ->add('data', ChoiceType::class, [
91                'label'    => 'admin.duplication.label.data',
92                'required' => true,
93                'multiple' => true,
94                'expanded' => false,
95                'choices'  => [],
96                'attr'     => [
97                    'size' => 15,
98                ],
99            ])
100            ->add('targetOption', DictionaryType::class, [
101                'name'     => DuplicationTargetOptionDictionary::NAME,
102                'label'    => false,
103                'required' => true,
104                'multiple' => false,
105                'expanded' => true,
106            ])
107            ->add('targetCollectivityTypes', DictionaryType::class, [
108                'name'     => 'user_collectivity_type',
109                'label'    => 'global.label.organization_type',
110                'required' => false,
111                'multiple' => true,
112                'expanded' => false,
113                'attr'     => [
114                    'size' => 6,
115                ],
116            ])
117            ->add('targetCollectivities', EntityType::class, [
118                'class'         => UserModel\Collectivity::class,
119                'label'         => 'global.label.organization_list',
120                'query_builder' => function (EntityRepository $er) {
121                    return $er->createQueryBuilder('c')
122                        ->orderBy('c.name', 'ASC');
123                },
124                'required' => false,
125                'multiple' => true,
126                'expanded' => false,
127                'attr'     => [
128                    'size' => 18,
129                ],
130            ])
131        ;
132
133        // Reset view transformer to disable mapping between choices values & given values
134        // Since we send "random" values which are not defined in Form, no need to validate sended values with transformer
135        // This data initial view transformer is \Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer
136        // $builder->get('data')->resetViewTransformers();
137
138        $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
139            $data = $event->getData();
140            $form = $event->getForm();
141
142            $choices = [];
143
144            $collectivity = $this->collectivityRepository->findOneById($data['sourceCollectivity']);
145
146            if ('treatment' === $data['type']) {
147                $choices = $this->treatmentRepository->findAllByCollectivity($collectivity);
148            } elseif ('contractor' === $data['type']) {
149                $choices = $this->contractorRepository->findAllByCollectivity($collectivity);
150            } elseif ('mesurement' === $data['type']) {
151                $choices = $this->mesurementRepository->findAllByCollectivity($collectivity);
152            } elseif ('tool' === $data['type']) {
153                $choices = $this->toolRepository->findAllByCollectivity($collectivity);
154            }
155
156            $choices = array_map(function ($object) {
157                return $object->getId()->__toString();
158            }, $choices);
159
160            $form->add('data', ChoiceType::class, [
161                'label'    => 'admin.duplication.label.data',
162                'required' => true,
163                'multiple' => true,
164                'expanded' => false,
165                'choices'  => $choices,
166                'attr'     => [
167                    'size' => 15,
168                ],
169            ]);
170        });
171    }
172
173    /**
174     * Provide type options.
175     */
176    public function configureOptions(OptionsResolver $resolver)
177    {
178        $resolver
179            ->setDefaults([
180                'data_class'        => DuplicationFormDTO::class,
181                'validation_groups' => [
182                    'default',
183                ],
184            ]);
185    }
186}