UserType   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 3
c 2
b 0
f 1
lcom 0
cbo 5
dl 0
loc 62
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B buildForm() 0 36 2
A configureOptions() 0 9 1
1
<?php
2
3
namespace Xdaysaysay\AdminBundle\Form\Type;
4
5
use Symfony\Component\Form\AbstractType;
6
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
7
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
8
use Symfony\Component\Form\FormBuilderInterface;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
use Symfony\Component\Validator\Constraints as Assert;
11
12
/**
13
 * Class UserType
14
 * @package Xdaysaysay\AdminBundle\Form\Type
15
 */
16
class UserType extends AbstractType
17
{
18
    /**
19
     * @param FormBuilderInterface $builder
20
     * @param array $options
21
     *
22
     * @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException
23
     * @throws \Symfony\Component\Validator\Exception\InvalidOptionsException
24
     * @throws \Symfony\Component\Validator\Exception\MissingOptionsException
25
     */
26
    public function buildForm(FormBuilderInterface $builder, array $options)
27
    {
28
        $builder->add('username', null, [
29
            'label'       => 'admin.user.form.label.username',
30
            'constraints' => [
31
                new Assert\NotBlank(),
32
            ],
33
        ]);
34
        $builder->add('email', null, [
35
            'label'       => 'admin.user.form.label.email',
36
            'constraints' => [
37
                new Assert\NotBlank(),
38
                new Assert\Email(['checkMX' => true]),
39
            ],
40
        ]);
41
        $passwordConstraints = [];
42
        if ($options['new_user']) {
43
            $passwordConstraints = [
44
                new Assert\NotBlank(),
45
            ];
46
        }
47
        $builder->add('password', RepeatedType::class, [
48
            'first_name'     => 'password',
49
            'second_name'    => 'password_repeat',
50
            'first_options'  => [
51
                'label' => 'admin.user.form.label.password',
52
            ],
53
            'second_options' => [
54
                'label' => 'admin.user.form.label.password_repeat',
55
            ],
56
            'type'           => PasswordType::class,
57
            'mapped'         => false,
58
            'required'       => $options['new_user'],
59
            'constraints'    => $passwordConstraints,
60
        ]);
61
    }
62
63
    /**
64
     * @param OptionsResolver $resolver
65
     *
66
     * @throws \Symfony\Component\OptionsResolver\Exception\AccessException
67
     */
68
    public function configureOptions(OptionsResolver $resolver)
69
    {
70
        $resolver->setDefined('new_user');
71
        $resolver->setDefaults([
72
            'new_user'           => false,
73
            'data_class'         => 'Xdaysaysay\UserBundle\Entity\User',
74
            'translation_domain' => 'admin',
75
        ]);
76
    }
77
}
78