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

PostAdmin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 133
Duplicated Lines 12.78 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 17
loc 133
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setParameters() 0 5 1
A configureFormFields() 0 72 1
A configureListFields() 17 19 1
A configureDatagridFilters() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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