Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 101
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
EmailNotificationPreferenceType
0.00% covered (danger)
0.00%
0 / 101
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 buildForm
0.00% covered (danger)
0.00%
0 / 93
0.00% covered (danger)
0.00%
0 / 1
30
 configureOptions
0.00% covered (danger)
0.00%
0 / 8
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\User\Form\Type;
25
26use App\Domain\User\Model\EmailNotificationPreference;
27use Symfony\Component\Form\AbstractType;
28use Symfony\Component\Form\CallbackTransformer;
29use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
30use Symfony\Component\Form\FormBuilderInterface;
31use Symfony\Component\OptionsResolver\OptionsResolver;
32
33class EmailNotificationPreferenceType extends AbstractType
34{
35    /**
36     * Build type form.
37     */
38    public function buildForm(FormBuilderInterface $builder, array $options)
39    {
40        $builder
41        ->add('frequency', ChoiceType::class, [
42            'label'    => false,
43            'required' => true,
44            'choices'  => [
45                'notifications.label.frequency.none'    => EmailNotificationPreference::FREQUENCY_NONE,
46                'notifications.label.frequency.each'    => EmailNotificationPreference::FREQUENCY_EACH,
47                'notifications.label.frequency.hourly'  => EmailNotificationPreference::FREQUENCY_HOUR,
48                'notifications.label.frequency.dayly'   => EmailNotificationPreference::FREQUENCY_DAY,
49                'notifications.label.frequency.weekly'  => EmailNotificationPreference::FREQUENCY_WEEK,
50                'notifications.label.frequency.monthly' => EmailNotificationPreference::FREQUENCY_MONTH,
51            ],
52            'choice_attr' => [
53                'notifications.label.frequency.none'    => ['class' => 'select-frequency'],
54                'notifications.label.frequency.each'    => ['class' => 'select-frequency'],
55                'notifications.label.frequency.hourly'  => ['class' => 'select-frequency'],
56                'notifications.label.frequency.dayly'   => ['class' => 'select-frequency'],
57                'notifications.label.frequency.weekly'  => ['class' => 'select-frequency'],
58                'notifications.label.frequency.monthly' => ['class' => 'select-frequency'],
59            ],
60            'expanded' => true,
61            'multiple' => false,
62        ])
63
64        ;
65        $hours = [];
66        for ($i = 0; $i < 24; ++$i) {
67            $hours[(string) $i] = $i;
68        }
69
70        $modules = [];
71        foreach (EmailNotificationPreference::MODULES as $k => $module) {
72            $modules['notifications.label.modules.' . $k] = $module;
73        }
74
75        $builder
76            ->add('hour', ChoiceType::class, [
77                'label'        => 'heures',
78                'required'     => true,
79                'choices'      => $hours,
80                'expanded'     => false,
81                'multiple'     => false,
82                'block_prefix' => 'wrapped_choice',
83            ])
84            ->add('day', ChoiceType::class, [
85                'label'    => 'à',
86                'required' => true,
87                'choices'  => [
88                    'Lundi'    => 1,
89                    'Mardi'    => 2,
90                    'Mercredi' => 3,
91                    'Jeudi'    => 4,
92                    'Vendredi' => 5,
93                    'Samedi'   => 6,
94                    'Dimanche' => 7,
95                ],
96                'expanded'     => false,
97                'multiple'     => false,
98                'block_prefix' => 'wrapped_choice',
99            ])
100            ->add('week', ChoiceType::class, [
101                'label'    => '',
102                'required' => true,
103                'choices'  => [
104                    'Premier'   => 1,
105                    'Second'    => 2,
106                    'Troisième' => 3,
107                    'Quatrième' => 4,
108                ],
109                'expanded'     => false,
110                'multiple'     => false,
111                'block_prefix' => 'wrapped_choice',
112            ])
113
114            ->add('notificationMask', ChoiceType::class, [
115                'mapped'       => true,
116                'label'        => 'notifications.label.email_modules',
117                'required'     => false,
118                'choices'      => $modules,
119                'expanded'     => true,
120                'multiple'     => true,
121                'block_prefix' => 'wrapped_choice',
122            ])
123        ;
124
125        $builder->get('notificationMask')->addModelTransformer(new CallbackTransformer(
126            function ($mask) {
127                // transform the bitmask to an array
128                $modules = EmailNotificationPreference::MODULES;
129
130                $ret = [];
131                foreach ($modules as $k => $module) {
132                    if ($module & $mask) {
133                        $ret[$k] = $module;
134                    }
135                }
136
137                return $ret;
138            },
139            function ($modules) {
140                // transform the array to a bitmask
141                return array_reduce($modules, function ($car, $el) {
142                    return $car | (int) $el;
143                }, 0);
144            }
145        ));
146    }
147
148    /**
149     * Provide type options.
150     */
151    public function configureOptions(OptionsResolver $resolver)
152    {
153        $resolver
154            ->setDefaults([
155                'data_class'        => EmailNotificationPreference::class,
156                'validation_groups' => [
157                    'default',
158                    'user',
159                ],
160            ]);
161    }
162}