PostAdmin   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 118
Duplicated Lines 14.41 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setParameters() 0 5 1
B configureFormFields() 0 59 2
A configureListFields() 17 17 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 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