PostAdmin::configureFormFields()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 1
dl 0
loc 59
ccs 22
cts 22
cp 1
crap 2
rs 8.8945
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 App\Admin;
4
5
use App\Entity\Tag;
6
use App\Form\DataTransformer\TagTransformer;
7
use FOS\CKEditorBundle\Form\Type\CKEditorType;
8
use Sonata\AdminBundle\Admin\AbstractAdmin;
9
use Sonata\AdminBundle\Datagrid\ListMapper;
10
use Sonata\AdminBundle\Datagrid\DatagridMapper;
11
use Sonata\AdminBundle\Form\FormMapper;
12
use Sonata\AdminBundle\Form\Type\ModelListType;
13
use Sonata\Form\Type\BooleanType;
14
use Sonata\Form\Type\CollectionType;
15
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
16
use Symfony\Component\Form\Extension\Core\Type\TextType;
17
18
class PostAdmin extends AbstractAdmin
19
{
20
    protected $baseRouteName = 'App\Entity\Post';
21
    protected $baseRoutePattern = 'Post';
22
    protected $datagridValues = [
23
        '_sort_order' => 'DESC',
24
        '_sort_by' => 'createdAt',
25
    ];
26
27
    private $default_locale;
28
29
    private $locales;
30
31 23
    public function setParameters($default_locale, $locales)
32
    {
33 23
        $this->default_locale = $default_locale;
34 23
        $this->locales = $locales;
35 23
    }
36
37
    /**
38
     * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
39
     *
40
     * @return void
41
     */
42 2
    protected function configureFormFields(FormMapper $formMapper)
43
    {
44
        $formMapper
45 2
            ->add('title')
46 2
            ->add('shortDescription')
47 2
            ->add('text', CKEditorType::class,
48
                [
49
                    'attr' => [
50 2
                            'class' => 'wysihtml5',
51
                            'style' => 'height:300px',
52
                    ],
53
                ]
54
            )
55 2
            ->add('mainPicture', ModelListType::class,
56
                [
57 2
                    'required' => false,
58
                    'btn_list' => false,
59
                ], [
60
                    'link_parameters' => [
61 2
                        'context' => 'post',
62
                        'provider' => 'sonata.media.provider.image',
63
                    ],
64
                ]
65
            )
66 2
            ->add(
67 2
                $formMapper->create('tags', TextType::class, [
68 2
                    'empty_data' => $this->subject ? $this->subject->getTags() : [],
69
                    'attr' => ['class' => 'posts-tags']]
70
                )
71 2
                ->addModelTransformer(
72 2
                    new TagTransformer(
73 2
                        $this->default_locale,
74 2
                        $this->locales,
75 2
                        $this->modelManager->getEntityManager(new Tag())
76
                    )
77
                )
78
            )
79 2
            ->add('pinned', CheckboxType::class, [
80 2
                'label'    => 'pinned_or_not',
81
                'required' => false,
82
            ])
83 2
            ->add('galleryHasMedia', CollectionType::class,
84
                [
85 2
                    'required' => false,
86
                    'label' => 'Gallery',
87
                ], [
88 2
                    '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'  => 'post',
95
                        'provider' => 'sonata.media.provider.image',
96
                    ],
97
                ]
98
            )
99
        ;
100 2
    }
101
    /**
102
     * @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper
103
     *
104
     * @return void
105
     */
106 2 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...
107
    {
108
        $listMapper
109 2
            ->add('mainPicture', 'string', ['template' => 'bundles/SonataAdmin/thumbnail.html.twig'])
110 2
            ->addIdentifier('title')
111 2
            ->add('createdAt')
112 2
            ->add('_action', 'actions',
113
                [
114
                    'actions' => [
115 2
                        'edit' => [],
116
                        'delete' => [],
117
                    ],
118
                ]
119
            )
120
121
        ;
122 2
    }
123
124
    /**
125
     * @param \Sonata\AdminBundle\Datagrid\DatagridMapper $datagridMapper
126
     *
127
     * @return void
128
     */
129 2
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
130
    {
131
        $datagridMapper
132 2
            ->add('title')
133 2
            ->add('tags', null, [], null, ['expanded' => true, 'multiple' => true]);
134 2
    }
135
}
136