Completed
Pull Request — master (#191)
by Serhii
02:32
created

EmployeeAdmin::configureFormFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 17
cts 17
cp 1
rs 9.0472
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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 App\Admin;
4
5
use FOS\CKEditorBundle\Form\Type\CKEditorType;
6
use Sonata\AdminBundle\Admin\AbstractAdmin;
7
use Sonata\AdminBundle\Datagrid\ListMapper;
8
use Sonata\AdminBundle\Datagrid\DatagridMapper;
9
use Sonata\AdminBundle\Form\FormMapper;
10
use Sonata\AdminBundle\Form\Type\ModelListType;
11
use Sonata\AdminBundle\Show\ShowMapper;
12
use App\Entity\Employee;
13
use Sonata\Form\Type\CollectionType;
14
use Sonata\Form\Type\DateTimePickerType;
15
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
16
17
class EmployeeAdmin extends AbstractAdmin
18
{
19
    protected $baseRouteName = 'App\Entity\Employee';
20
    protected $baseRoutePattern = 'Employee';
21
    protected $datagridValues = [
22
        '_sort_order' => 'ASC',
23
        '_sort_by'    => 'name',
24
    ];
25
26
    /**
27
     * @param \Sonata\AdminBundle\Show\ShowMapper $showMapper
28
     *
29
     * @return void
30
     */
31
    protected function configureShowFields(ShowMapper $showMapper)
32
    {
33
        $showMapper
34
            ->add('firstName')
35
            ->add('lastName')
36
            ->add('dob', 'date')
37
            ->add('position')
38
            ->add('staff')
39
            ->add('roles')
40
        ;
41
    }
42
43
    /**
44
     * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
45
     *
46
     * @return void
47 2
     */
48
    protected function configureFormFields(FormMapper $formMapper)
49
    {
50 2
        $formMapper
51 2
            ->add('firstName')
52 2
            ->add('lastName')
53 2
            ->add('avatar', ModelListType::class, [
54
                'required' => false,
55
                'btn_list' => false,
56
            ], [
57 2
                'link_parameters' => [
58
                    'context'  => 'employee',
59
                    'provider' => 'sonata.media.provider.image',
60
                ],
61 2
            ])
62
            ->add('dob', DateTimePickerType::class,
63 2
                [
64
                    'dp_side_by_side'       => false,
65
                    'dp_use_current'        => false,
66
                    'dp_use_seconds'        => false,
67
                    'dp_use_minutes'        => false,
68
                    'format' => "dd/MM/yyyy",
69
                ]
70 2
            )
71 2
            ->add('position', ChoiceType::class, [
72 2
                'label' => 'employee.position',
73 2
                'choices' => employee::getPositions(),
74
                'translation_domain' => 'messages',
75
                ]
76 2
            )
77
            ->add('staff', null, ['required' => true])
78
            ->add('biography', CKEditorType::class,
79
                [
80 2
//                    'format' => 'richhtml',
81
//                    'ckeditor_context' => 'default',
82 2
                ]
83 2
            )
84
            ->add('galleryHasMedia', CollectionType::class, [
85
                'required' => false,
86 2
                'label' => 'Gallery',
87
                ], [
88
                'edit' => 'inline',
89
                'inline' => 'table',
90
                'sortable'  => 'position',
91
                'targetEntity' => 'App\Entity\GalleryHasMedia',
92
                'admin_code' => 'sonata.media.admin.gallery_has_media',
93
                'link_parameters' => [
94
                    'context'  => 'employee',
95
                    'provider' => 'sonata.media.provider.image',
96
                ],
97 2
            ])
98
        ;
99
    }
100
101
    /**
102
     * @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper
103
     *
104 2
     * @return void
105
     */
106
    protected function configureListFields(ListMapper $listMapper)
107 2
    {
108 2
        $listMapper
109 2
            ->add('avatar', 'string', ['template' => 'bundles/SonataAdmin/thumbnail.html.twig'])
110 2
            ->addIdentifier('firstName')
111 2
            ->add('lastName')
112 2
            ->add('dob', 'date')
113 2
            ->add('position', 'choice', [
114
                    'choices' => employee::getPositions(),
115
                    'catalogue' => 'messages',
116 2
                ]
117
            )
118 2
            ->add('staff', 'string', ['template' => 'bundles/SonataAdmin/staff.html.twig'])
119
            ->add('roles')
120
        ;
121
    }
122
123
    /**
124
     * @param \Sonata\AdminBundle\Datagrid\DatagridMapper $datagridMapper
125 2
     *
126
     * @return void
127
     */
128 2
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
129 2
    {
130 2
        $datagridMapper
131 2
            ->add('firstName')
132
            ->add('lastName')
133 2
            ->add('dob')
134
            ->add('roles')
135
        ;
136
    }
137
}
138