Failed Conditions
Branch issue#666 (91903a)
by Guilherme
08:25
created

DynamicFormBuilder::addRequiredPersonField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\DynamicFormBundle\Form;
12
13
use libphonenumber\PhoneNumberFormat;
14
use LoginCidadao\CoreBundle\Entity\PersonAddress;
15
use LoginCidadao\CoreBundle\Model\IdCardInterface;
16
use LoginCidadao\CoreBundle\Model\LocationSelectData;
17
use LoginCidadao\DynamicFormBundle\Model\DynamicFormData;
18
use LoginCidadao\ValidationControlBundle\Handler\ValidationHandler;
19
use Symfony\Component\Form\FormInterface;
20
use Symfony\Component\Validator\Constraints;
21
22
class DynamicFormBuilder
23
{
24
    /** @var ValidationHandler */
25
    private $validationHandler;
26
27
    /**
28
     * DynamicFormBuilder constructor.
29
     * @param ValidationHandler $validationHandler
30
     */
31 3
    public function __construct(ValidationHandler $validationHandler)
32
    {
33 3
        $this->validationHandler = $validationHandler;
34 3
    }
35
36 3
    public function addFieldFromScope(FormInterface $form, $scope, DynamicFormData $data)
37
    {
38
        switch ($scope) {
39 3
            case 'name':
40 3
            case 'surname':
41 3
            case 'full_name':
42 1
                $this->addRequiredPersonField($form, 'firstname');
43 1
                $this->addRequiredPersonField($form, 'surname');
44 1
                break;
45 3
            case 'cpf':
46 1
                $this->addRequiredPersonField($form, 'cpf', 'form-control cpf');
47 1
                break;
48 3
            case 'email':
49 1
                $this->addRequiredPersonField($form, 'email');
50 1
                break;
51 3
            case 'id_cards':
52 3
                $this->addIdCard($form, $data);
53 3
                break;
54 1
            case 'phone_number':
55 1
            case 'mobile':
56 1
                $this->addPhoneField($form);
57 1
                break;
58 1
            case 'birthdate':
59 1
                $this->addBirthdayField($form);
60 1
                break;
61 1
            case 'city':
62 1
            case 'state':
63 1
            case 'country':
64 1
                $this->addPlaceOfBirth($form, $scope);
65 1
                break;
66 1
            case 'addresses':
67 1
                $this->addAddresses($form, $data);
68 1
                break;
69
            default:
70 1
                break;
71
        }
72 3
    }
73
74 1
    private function getPersonForm(FormInterface $form)
75
    {
76 1
        if ($form->has('person') === false) {
77 1
            $form->add('person', new DynamicPersonType(), ['label' => false]);
0 ignored issues
show
Bug introduced by
new LoginCidadao\Dynamic...orm\DynamicPersonType() of type LoginCidadao\DynamicForm...\Form\DynamicPersonType is incompatible with the type null|string expected by parameter $type of Symfony\Component\Form\FormInterface::add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            $form->add('person', /** @scrutinizer ignore-type */ new DynamicPersonType(), ['label' => false]);
Loading history...
78
        }
79
80 1
        return $form->get('person');
81
    }
82
83 1
    private function addRequiredPersonField(FormInterface $form, $field, $cssClass = null)
84
    {
85 1
        $options = ['required' => true];
86
87 1
        if ($cssClass) {
88 1
            $options['attr'] = ['class' => $cssClass];
89
        }
90
91 1
        $this->getPersonForm($form)->add($field, null, $options);
92 1
    }
93
94 1
    private function addPhoneField(FormInterface $form)
95
    {
96 1
        $this->getPersonForm($form)->add(
97 1
            'mobile',
98 1
            'Misd\PhoneNumberBundle\Form\Type\PhoneNumberType',
99
            [
100 1
                'required' => true,
101 1
                'label' => 'person.form.mobile.label',
102
                'attr' => ['class' => 'form-control intl-tel', 'placeholder' => 'person.form.mobile.placeholder'],
103
                'label_attr' => ['class' => 'intl-tel-label'],
104 1
                'format' => PhoneNumberFormat::E164,
105
            ]
106
        );
107 1
    }
108
109 1
    private function addBirthdayField(FormInterface $form)
110
    {
111 1
        $this->getPersonForm($form)->add(
112 1
            'birthdate',
113 1
            'birthday',
114
            [
115 1
                'required' => true,
116
                'format' => 'dd/MM/yyyy',
117
                'widget' => 'single_text',
118
                'label' => 'form.birthdate',
119
                'translation_domain' => 'FOSUserBundle',
120
                'attr' => ['pattern' => '[0-9/]*', 'class' => 'form-control birthdate'],
121
            ]
122
        );
123 1
    }
124
125 1
    private function addPlaceOfBirth(FormInterface $form, $level)
126
    {
127 1
        $form->add(
128 1
            'placeOfBirth',
129 1
            'LoginCidadao\CoreBundle\Form\Type\CitySelectorComboType',
130
            [
131 1
                'level' => $level,
132 1
                'city_label' => 'Place of birth - City',
133 1
                'state_label' => 'Place of birth - State',
134 1
                'country_label' => 'Place of birth - Country',
135 1
                'constraints' => new Constraints\Valid(),
136
            ]
137
        );
138
139 1
        return;
140
    }
141
142 1
    private function addAddresses(FormInterface $form, DynamicFormData $data)
143
    {
144 1
        $address = new PersonAddress();
145 1
        $address->setLocation(new LocationSelectData());
146 1
        $data->setAddress($address);
147 1
        $form->add(
148 1
            'address',
149 1
            'LoginCidadao\CoreBundle\Form\Type\PersonAddressFormType',
150 1
            ['label' => false, 'constraints' => new Constraints\Valid()]
151
        );
152 1
    }
153
154 3
    private function addIdCard(FormInterface $form, DynamicFormData $data)
155
    {
156 3
        $person = $data->getPerson();
157 3
        $state = $data->getIdCardState();
158 3
        if (!$state) {
0 ignored issues
show
introduced by
The condition ! $state can never be false.
Loading history...
159 1
            return;
160
        }
161 2
        foreach ($person->getIdCards() as $idCard) {
162 1
            if ($idCard->getState()->getId() === $state->getId()) {
163 1
                $data->setIdCard($idCard);
164 1
                break;
165
            }
166
        }
167
168 2
        if (!($data->getIdCard() instanceof IdCardInterface)) {
169 1
            $idCard = $this->validationHandler->instantiateIdCard($state);
170 1
            $idCard->setPerson($person);
171 1
            $data->setIdCard($idCard);
172
        }
173
174 2
        $form->add('idcard', 'lc_idcard_form', ['label' => false, 'constraints' => new Constraints\Valid()]);
175 2
    }
176
}
177