Passed
Push — master ( af608e...293c09 )
by Dev
11:05
created

UserAdmin::configureFormFields()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 90
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 90
rs 8.1793
c 0
b 0
f 0
cc 7
nc 32
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PiedWeb\CMSBundle\Admin;
4
5
use Sonata\AdminBundle\Admin\AbstractAdmin;
6
use Sonata\AdminBundle\Datagrid\ListMapper;
7
use Sonata\AdminBundle\Datagrid\DatagridMapper;
8
use Sonata\AdminBundle\Form\FormMapper;
9
use Sonata\AdminBundle\Form\Type\ModelType;
10
use Sonata\CoreBundle\Form\Type\ImmutableArrayType;
11
use Sonata\CoreBundle\Form\Type\DatePickerType;
12
use Symfony\Component\Form\Extension\Core\Type\TextType;
13
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
14
use Sonata\UserBundle\Form\Type\SecurityRolesType;
0 ignored issues
show
Bug introduced by
The type Sonata\UserBundle\Form\Type\SecurityRolesType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
class UserAdmin extends AbstractAdmin
17
{
18
    protected $datagridValues = [
19
        '_page' => 1,
20
        '_sort_order' => 'DESC',
21
        '_sort_by' => 'createdAt',
22
    ];
23
24
    protected function configureFormFields(FormMapper $formMapper): void
25
    {
26
27
        $now = new \DateTime();
28
29
        $formMapper
30
            ->with('admin.user.label.id', ['class' => 'col-md-4'])
31
                    //->add('username')
32
                    ->add('email', null, [
33
                        'label' => 'admin.user.email.label',
34
                    ])
35
                    ->add('plainPassword', TextType::class, [
36
                        'required' => (!$this->getSubject() || null === $this->getSubject()->getId()),
37
                        'label' => 'admin.user.password.label',
38
                    ])
39
            ->end()
40
        ;
41
42
        $formMapper
43
            ->with('admin.user.label.profile', ['class' => 'col-md-4'])
44
        ;
45
46
        if (method_exists($this->getConfigurationPool()->getContainer()->getParameter('app.entity_user'), 'getDateOfBirth')) {
47
            $formMapper->add('dateOfBirth', DatePickerType::class, [
48
                'years' => range(1900, $now->format('Y')),
49
                'dp_min_date' => '1-1-1900',
50
                'dp_max_date' => $now->format('c'),
51
                'required' => false,
52
                'label' => 'admin.user.dateOfBirth.label',
53
            ]);
54
        }
55
        if (method_exists($this->getConfigurationPool()->getContainer()->getParameter('app.entity_user'), 'getfirstname')) {
56
            $formMapper->add('firstname', TextType::class, [
57
                'required' => false,
58
                'label' => 'admin.user.firstname.label',
59
            ]);
60
        }
61
        if (method_exists($this->getConfigurationPool()->getContainer()->getParameter('app.entity_user'), 'getlastname')) {
62
            $formMapper->add('lastname', TextType::class, [
63
                'required' => false,
64
                'label' => 'admin.user.lastname.label',
65
            ]);
66
        }
67
        if (method_exists($this->getConfigurationPool()->getContainer()->getParameter('app.entity_user'), 'getcity')) {
68
            $formMapper->add('city', TextType::class, [
69
                'required' => false,
70
                'label' => 'admin.user.city.label',
71
            ]);
72
        }
73
        if (method_exists($this->getConfigurationPool()->getContainer()->getParameter('app.entity_user'), 'getphone')) {
74
              $formMapper->add('phone', TextType::class, [
75
                'required' => false,
76
                'label' => 'admin.user.phone.label',
77
            ]);
78
        }
79
80
        $formMapper->end()
81
82
            ->with('admin.user.label.security', ['class' => 'col-md-4'])
83
                ->add('enabled', null, [
84
                    'required' => false,
85
                    'label' => 'admin.user.enabled.label',
86
                ])
87
88
                /*
89
                ->with('Groups')
90
                    ->add('groups', ModelType::class, [
91
                        'required' => false,
92
                        'expanded' => true,
93
                        'multiple' => true,
94
                    ])
95
                ->end()
96
                */
97
98
                ->add('roles', ImmutableArrayType::class, [
99
                    'label' => false,
100
                    'keys' => [
101
                        ['0', ChoiceType::class, [
102
                            'required' => false,
103
                            'label' => 'admin.user.role.label',
104
                            'choices' => [
105
                                'admin.user.role.admin' => 'ROLE_SUPER_ADMIN',
106
                                'admin.user.role.user' => 'ROLE_USER',
107
                            ],
108
                        ]],
109
                    ],
110
                ])
111
112
113
            ->end()
114
        ;
115
    }
116
117
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
118
    {
119
        $datagridMapper->add('id')
120
            ->add('email')
121
            //->add('groups')
122
        ;
123
    }
124
125
    protected function configureListFields(ListMapper $listMapper)
126
    {
127
        $listMapper
128
            ->add('email', null, [
129
                'label' => 'admin.user.email.label',
130
            ]);
131
        if (method_exists($this->getConfigurationPool()->getContainer()->getParameter('app.entity_user'), 'getfirstname')) {
132
            $listMapper->add('firstname', TextType::class, [
133
                'editable' => true,
134
                'label' => 'admin.user.firstname.label',
135
            ]);
136
        }
137
        if (method_exists($this->getConfigurationPool()->getContainer()->getParameter('app.entity_user'), 'getlastname')) {
138
            $listMapper->add('lastname', TextType::class, [
139
                'editable' => true,
140
                'label' => 'admin.user.lastname.label',
141
            ]);
142
        }
143
144
        /**todo
145
        $listMapper->add('roles[0]', null, [
146
                'label' => 'admin.user.role.label',
147
            ]);
148
        /**/
149
        $listMapper
150
            ->add('enabled', null, [
151
                'editable' => true,
152
                'label' => 'admin.user.enabled.label',
153
            ])
154
            ->add('createdAt', null, [
155
                'editable' => true,
156
                'label' => 'admin.user.createdAt.label',
157
            ])
158
            ->add('_action', null, [
159
                'actions' => [
160
                    'edit' => [],
161
                    'delete' => [],
162
                ],
163
                'row_align' => 'right',
164
                'header_class' => 'text-right',
165
                'label' => 'admin.action',
166
            ])
167
        ;
168
    }
169
}
170