Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
43.40% covered (danger)
43.40%
23 / 53
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ReponseType
43.40% covered (danger)
43.40%
23 / 53
66.67% covered (warning)
66.67%
2 / 3
6.90
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 buildForm
31.82% covered (danger)
31.82%
14 / 44
0.00% covered (danger)
0.00%
0 / 1
3.27
 configureOptions
100.00% covered (success)
100.00%
8 / 8
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\Registry\Form\Type\ConformiteTraitement;
25
26use App\Domain\Registry\Dictionary\MesurementStatusDictionary;
27use App\Domain\Registry\Model\ConformiteTraitement\Reponse;
28use App\Domain\Registry\Model\Mesurement;
29use App\Domain\User\Model\User;
30use Doctrine\ORM\EntityRepository;
31use Symfony\Bridge\Doctrine\Form\Type\EntityType;
32use Symfony\Component\Form\AbstractType;
33use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
34use Symfony\Component\Form\FormBuilderInterface;
35use Symfony\Component\Form\FormEvent;
36use Symfony\Component\Form\FormEvents;
37use Symfony\Component\OptionsResolver\OptionsResolver;
38use Symfony\Component\Security\Core\Security;
39
40class ReponseType extends AbstractType
41{
42    /**
43     * @var Security
44     */
45    private $security;
46
47    public function __construct(Security $security)
48    {
49        $this->security = $security;
50    }
51
52    /**
53     * Build type form.
54     */
55    public function buildForm(FormBuilderInterface $builder, array $options)
56    {
57        $builder
58            ->add('conforme', ChoiceType::class, [
59                'label'   => false,
60                'choices' => [
61                    'Non conforme' => false,
62                    'Conforme'     => true,
63                ],
64                'attr' => [
65                    'class' => 'conformite-select',
66                ],
67            ])
68        ;
69
70        $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
71            $parentForm   = $event->getForm()->getParent()->getParent();
72            $collectivity = $parentForm->getData()->getTraitement()->getCollectivity();
73            $event->getForm()->add('actionProtections', EntityType::class, [
74                'required'      => false,
75                'label'         => false,
76                'class'         => Mesurement::class,
77                'query_builder' => function (EntityRepository $er) use ($collectivity) {
78                    /** @var User $user */
79                    $user = $this->security->getUser();
80
81                    return $er->createQueryBuilder('m')
82                        ->andWhere('m.collectivity = :collectivity')
83                        ->setParameter('collectivity', $collectivity)
84                        ->andWhere('m.status = :nonApplied')
85                        ->setParameter('nonApplied', MesurementStatusDictionary::STATUS_NOT_APPLIED)
86                        ->orderBy('m.name', 'ASC');
87                },
88                'choice_label' => 'name',
89                'expanded'     => false,
90                'multiple'     => true,
91                'attr'         => [
92                    'class'            => 'selectpicker',
93                    'title'            => 'global.placeholder.multiple_select',
94                    'data-live-search' => true,
95                ],
96                'choice_attr' => function (Mesurement $choice) {
97                    $name = $choice->getName();
98                    if (\mb_strlen($name) > 85) {
99                        $name = \mb_substr($name, 0, 85) . '...';
100                    }
101
102                    return ['data-content' => $name];
103                },
104            ]);
105        });
106    }
107
108    /**
109     * Provide type options.
110     */
111    public function configureOptions(OptionsResolver $resolver)
112    {
113        $resolver
114            ->setDefaults([
115                'data_class'        => Reponse::class,
116                'validation_groups' => [
117                    'default',
118                    'reponse',
119                ],
120            ]);
121    }
122}