Completed
Pull Request — master (#124)
by Serhii
13:13
created

PostAdmin::configureFormFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 72
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 46
nc 1
nop 1
dl 0
loc 72
ccs 0
cts 0
cp 0
crap 2
rs 9.102
c 0
b 0
f 0

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 AppBundle\Admin;
4
5
use AppBundle\Entity\Tag;
6
use AppBundle\Form\DataTransformer\TagTransformer;
7
use Sonata\AdminBundle\Admin\Admin;
8
use Sonata\AdminBundle\Datagrid\ListMapper;
9
use Sonata\AdminBundle\Datagrid\DatagridMapper;
10
use Sonata\AdminBundle\Form\FormMapper;
11
12
class PostAdmin extends Admin
13
{
14
    protected $baseRouteName = 'AppBundle\Entity\Post';
15
    protected $baseRoutePattern = 'Post';
16
    protected $datagridValues = [
17
        '_sort_order' => 'DESC',
18
        '_sort_by' => 'createdAt',
19
    ];
20
21
    private $defaultLocale;
22
23
    private $locales;
24
25 10
    public function setParameters($defaultLocale, $locales)
26
    {
27 10
        $this->defaultLocale = $defaultLocale;
28 10
        $this->locales = $locales;
29 10
    }
30
31
    /**
32
     * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
33
     *
34
     * @return void
35
     */
36
    protected function configureFormFields(FormMapper $formMapper)
37
    {
38
        $formMapper
39
            ->add('title')
40
            ->add('shortDescription')
41
            ->add(
42
                'text',
43
                'textarea',
44
                [
45
                    'attr' => [
46
                            'class' => 'wysihtml5',
47
                            'style' => 'height:300px',
48
                    ],
49
                ]
50
            )
51
            ->add(
52
                'mainPicture',
53
                'sonata_type_model_list',
54
                [
55
                    'required' => false,
56
                    'btn_list' => false,
57
                ],
58
                [
59
                    'link_parameters' => [
60
                        'context' => 'post',
61
                        'provider' => 'sonata.media.provider.image',
62
                    ],
63
                ]
64
            )
65
            ->add(
66
                $formMapper->create(
67
                    'tags',
68
                    'text',
69
                    ['empty_data' => $this->subject->getTags(), 'attr' => ['class' => 'posts-tags']]
70
                )
71
                ->addModelTransformer(
72
                    new TagTransformer(
73
                        $this->defaultLocale,
74
                        $this->locales,
75
                        $this->modelManager->getEntityManager(new Tag())
76
                    )
77
                )
78
            )
79
            ->add(
80
                'pinned',
81
                'checkbox',
82
                [
83
                    'label'    => 'pinned_or_not',
84
                    'required' => false,
85
                ]
86
            )
87
            ->add(
88
                'galleryHasMedia',
89
                'sonata_type_collection',
90
                [
91
                    'required' => false,
92
                    'label' => 'Gallery',
93
                ],
94
                [
95
                    'edit' => 'inline',
96
                    'inline' => 'table',
97
                    'sortable'  => 'position',
98
                    'targetEntity' => 'Application\Sonata\MediaBundle\Entity\GalleryHasMedia',
99
                    'admin_code' => 'sonata.media.admin.gallery_has_media',
100
                    'link_parameters' => [
101
                        'context'  => 'post',
102
                        'provider' => 'sonata.media.provider.image',
103
                    ],
104
                ]
105
            )
106
        ;
107
    }
108
    /**
109
     * @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper
110
     *
111
     * @return void
112
     */
113 View Code Duplication
    protected function configureListFields(ListMapper $listMapper)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        $listMapper
116
            ->add('mainPicture', 'string', ['template' => '::SonataAdmin/thumbnail.html.twig'])
117
            ->addIdentifier('title')
118
            ->add('createdAt')
119
            ->add(
120
                '_action',
121
                'actions',
122
                [
123
                    'actions' => [
124
                        'edit' => [],
125
                        'delete' => [],
126
                    ],
127
                ]
128
            )
129
130
        ;
131
    }
132
133
    /**
134
     * @param \Sonata\AdminBundle\Datagrid\DatagridMapper $datagridMapper
135
     *
136
     * @return void
137
     */
138
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
139
    {
140
        $datagridMapper
141
            ->add('title')
142
            ->add('tags', null, [], null, ['expanded' => true, 'multiple' => true]);
143
    }
144
}
145