Completed
Push — master ( cd8dec...fbf63a )
by Dev
12:23
created

UserAdmin::configureFormFields()   B

Complexity

Conditions 10
Paths 33

Size

Total Lines 100
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 57
c 1
b 0
f 0
nc 33
nop 1
dl 0
loc 100
ccs 0
cts 80
cp 0
crap 110
rs 7.0715

How to fix   Long Method    Complexity   

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\DatagridMapper;
7
use Sonata\AdminBundle\Datagrid\ListMapper;
8
use Sonata\AdminBundle\Form\FormMapper;
9
use Sonata\AdminBundle\Form\Type\ModelType;
10
use Sonata\CoreBundle\Form\Type\DatePickerType;
11
use Sonata\CoreBundle\Form\Type\ImmutableArrayType;
12
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
13
use Symfony\Component\Form\Extension\Core\Type\TextType;
14
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
15
16
class UserAdmin extends AbstractAdmin
17
{
18
    use AdminTrait;
19
20
    protected $datagridValues = [
21
        '_page' => 1,
22
        '_sort_order' => 'DESC',
23
        '_sort_by' => 'createdAt',
24
    ];
25
26
    protected function exists(string $name): bool
27
    {
28
        return method_exists($this->getContainer()->getParameter('app.entity_user'), 'get'.$name);
29
    }
30
31
    protected function configureFormFields(FormMapper $formMapper): void
32
    {
33
        // Forbid edition of other admin account except for super admin
34
        if (($this->getSubject()->hasRole('ROLE_SUPER_ADMIN')
35
            && $this->getUser()->getId() !== $this->getSubject()->getId())) {
36
            throw new AccessDeniedException('u can\'t edit this user'); // TODO : do better
37
        }
38
39
        $now = new \DateTime();
40
41
        $formMapper
42
            ->with('admin.user.label.id', ['class' => 'col-md-4'])
43
                    //->add('username')
44
                    ->add('email', null, [
45
                        'label' => 'admin.user.email.label',
46
                    ])
47
                    ->add('plainPassword', TextType::class, [
48
                        'required' => (!$this->getSubject() || null === $this->getSubject()->getId()),
49
                        'label' => 'admin.user.password.label',
50
                    ])
51
            ->end()
52
        ;
53
54
        $formMapper
55
            ->with('admin.user.label.profile', ['class' => 'col-md-4'])
56
        ;
57
58
        if ($this->exists('DateOfBirth')) {
59
            $formMapper->add('dateOfBirth', DatePickerType::class, [
60
                'years' => range(1900, $now->format('Y')),
61
                'dp_min_date' => '1-1-1900',
62
                'dp_max_date' => $now->format('c'),
63
                'required' => false,
64
                'label' => 'admin.user.dateOfBirth.label',
65
            ]);
66
        }
67
        if ($this->exists('firstname')) {
68
            $formMapper->add('firstname', TextType::class, [
69
                'required' => false,
70
                'label' => 'admin.user.firstname.label',
71
            ]);
72
        }
73
        if ($this->exists('lastname')) {
74
            $formMapper->add('lastname', TextType::class, [
75
                'required' => false,
76
                'label' => 'admin.user.lastname.label',
77
            ]);
78
        }
79
        if ($this->exists('city')) {
80
            $formMapper->add('city', TextType::class, [
81
                'required' => false,
82
                'label' => 'admin.user.city.label',
83
            ]);
84
        }
85
        if ($this->exists('phone')) {
86
            $formMapper->add('phone', TextType::class, [
87
                'required' => false,
88
                'label' => 'admin.user.phone.label',
89
            ]);
90
        }
91
92
        $formMapper->end()
93
94
            ->with('admin.user.label.security', ['class' => 'col-md-4'])
95
                ->add('enabled', null, [
96
                    'required' => false,
97
                    'label' => 'admin.user.enabled.label',
98
                ])
99
100
                /*
101
                ->with('Groups')
102
                    ->add('groups', ModelType::class, [
103
                        'required' => false,
104
                        'expanded' => true,
105
                        'multiple' => true,
106
                    ])
107
                ->end()
108
                */
109
110
                ->add('roles', ImmutableArrayType::class, [
111
                    'label' => false,
112
                    'keys' => [
113
                        ['0', ChoiceType::class, [
114
                            'required' => false,
115
                            'label' => 'admin.user.role.label',
116
                            'choices' => $this->getUser()->isSuperAdmin() ? [
117
                                'admin.user.role.super_admin' => 'ROLE_SUPER_ADMIN',
118
                                'admin.user.role.admin' => 'ROLE_ADMIN',
119
                                'admin.user.role.editor' => 'ROLE_EDITOR',
120
                                'admin.user.role.user' => 'ROLE_USER',
121
                                ] : [
122
                                'admin.user.role.admin' => 'ROLE_ADMIN',
123
                                'admin.user.role.editor' => 'ROLE_EDITOR',
124
                                'admin.user.role.user' => 'ROLE_USER',
125
                                ],
126
                        ]],
127
                    ],
128
                ])
129
130
            ->end()
131
        ;
132
    }
133
134
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
135
    {
136
        $datagridMapper->add('id')
137
            ->add('email')
138
            //->add('groups')
139
        ;
140
    }
141
142
    protected function configureListFields(ListMapper $listMapper)
143
    {
144
        $listMapper
145
            ->add('email', null, [
146
                'label' => 'admin.user.email.label',
147
            ]);
148
        if ($this->exists('firstname')) {
149
            $listMapper->add('firstname', TextType::class, [
150
                'editable' => true,
151
                'label' => 'admin.user.firstname.label',
152
            ]);
153
        }
154
        if ($this->exists('lastname')) {
155
            $listMapper->add('lastname', TextType::class, [
156
                'editable' => true,
157
                'label' => 'admin.user.lastname.label',
158
            ]);
159
        }
160
161
        /**todo
162
        $listMapper->add('roles[0]', null, [
163
                'label' => 'admin.user.role.label',
164
            ]);
165
        /**/
166
        $listMapper
167
            ->add('enabled', null, [
168
                'editable' => true,
169
                'label' => 'admin.user.enabled.label',
170
            ])
171
            ->add('createdAt', null, [
172
                'editable' => true,
173
                'label' => 'admin.user.createdAt.label',
174
            ])
175
            ->add('_action', null, [
176
                'actions' => [
177
                    'edit' => [],
178
                    'delete' => [],
179
                ],
180
                'row_align' => 'right',
181
                'header_class' => 'text-right',
182
                'label' => 'admin.action',
183
            ])
184
        ;
185
    }
186
}
187