UserRegistrationForm::buildForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 2
dl 0
loc 23
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Donut Social Network - Yet another experimental social network.
5
 * Copyright (C) 2016-2017, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Donut Social Network.
8
 *
9
 * Donut Social Network is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Donut Social Network is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Donut Social Network.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Donut Social Network
23
 * @copyright Copyright (C) 2016-2017, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/donut/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Donut\Users\Form;
29
30
use Doctrine\ORM\EntityManagerInterface;
31
use Angelov\Donut\Core\Form\DataTransformers\NullToEmptyStringDataTransformer;
32
use Angelov\Donut\Core\UuidGenerator\UuidGeneratorInterface;
33
use Angelov\Donut\Places\City;
34
use Angelov\Donut\Users\Commands\StoreUserCommand;
35
use Symfony\Component\Form\AbstractType;
36
use Symfony\Component\Form\DataMapperInterface;
37
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
38
use Symfony\Component\Form\Extension\Core\Type\EmailType;
39
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
40
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
41
use Symfony\Component\Form\Extension\Core\Type\TextType;
42
use Symfony\Component\Form\FormBuilderInterface;
43
use Symfony\Component\OptionsResolver\OptionsResolver;
44
45
// @todo Extract data mapper to separate class
46
47
class UserRegistrationForm extends AbstractType implements DataMapperInterface
48
{
49
    private $entityManager;
50
    private $uuidGenerator;
51
52
    // @todo use repository instead of entity manager
53
    public function __construct(EntityManagerInterface $entityManager, UuidGeneratorInterface $uuidGenerator)
54
    {
55
        $this->entityManager = $entityManager;
56
        $this->uuidGenerator = $uuidGenerator;
57
    }
58
59
    public function buildForm(FormBuilderInterface $builder, array $options) : void
60
    {
61
        $builder->add('name', TextType::class);
62
        $builder->add('email', EmailType::class);
63
        $builder->add('password', RepeatedType::class, [
64
            'type' => PasswordType::class,
65
            'invalid_message' => 'Please confirm your password.'
66
        ]);
67
        $builder->add('city', ChoiceType::class, [
68
            'choices' => $this->entityManager->getRepository(City::class)->findAll(),
69
            'choice_label' => function(City $city) : string {
70
                return $city->getName();
71
            }
72
        ]);
73
74
        $builder->setDataMapper($this);
75
76
        $transformer = new NullToEmptyStringDataTransformer();
77
78
        $builder->get('name')->addModelTransformer($transformer);
79
        $builder->get('email')->addModelTransformer($transformer);
80
        $builder->get('password')->get('first')->addModelTransformer($transformer);
81
        $builder->get('password')->get('second')->addModelTransformer($transformer);
82
    }
83
84
    public function configureOptions(OptionsResolver $resolver) : void
85
    {
86
        $resolver->setDefaults([
87
            'data_class' => StoreUserCommand::class,
88
            'empty_data' => null
89
        ]);
90
    }
91
92
    public function mapDataToForms($data, $forms) : void
93
    {
94
        $forms = iterator_to_array($forms);
95
96
        $forms['name']->setData($data ? $data->getName() : '');
97
        $forms['email']->setData($data ? $data->getEmail() : '');
98
        $forms['password']->get('first')->setData('');
99
        $forms['password']->get('second')->setData('');
100
    }
101
102
    public function mapFormsToData($forms, &$data) : void
103
    {
104
        $forms = iterator_to_array($forms);
105
106
        $data = new StoreUserCommand(
107
            $this->uuidGenerator->generate(),
108
            $forms['name']->getData(),
109
            $forms['email']->getData(),
110
            $forms['password']->get('second')->getData(),
111
            $forms['city']->getData()->getId()
112
        );
113
    }
114
}
115