Passed
Push — master ( ac8071...47eab6 )
by Dev
11:14
created

MediaAdmin::configureFormFields()   C

Complexity

Conditions 12
Paths 3

Size

Total Lines 63
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 12
eloc 46
nc 3
nop 1
dl 0
loc 63
ccs 0
cts 52
cp 0
crap 156
rs 6.9666
c 2
b 1
f 0

How to fix   Long Method    Complexity   

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 PiedWeb\CMSBundle\Admin;
4
5
use Sonata\AdminBundle\Admin\AbstractAdmin;
6
use Sonata\AdminBundle\Datagrid\DatagridMapper;
7
use Sonata\AdminBundle\Datagrid\ListMapper;
8
use Sonata\AdminBundle\Form\FormMapper;
9
use Sonata\BlockBundle\Meta\Metadata;
10
use Symfony\Component\Form\Extension\Core\Type\FileType;
11
use Symfony\Component\Form\Extension\Core\Type\TextType;
12
13
class MediaAdmin extends AbstractAdmin
14
{
15
    use AdminTrait;
16
17
    protected $datagridValues = [
18
        '_page' => 1,
19
        '_sort_order' => 'DESC',
20
        '_sort_by' => 'updatedAt',
21
    ];
22
23
    private $liipImage;
24
    private $relatedPages;
25
26
    public function setLiipImage($liipImage)
27
    {
28
        $this->liipImage = $liipImage;
29
    }
30
31
    protected function configureFormFields(FormMapper $formMapper)
32
    {
33
        $media = $this->getSubject();
34
35
        $formMapper->with('Media', ['class' => 'col-md-6'])
36
37
            ->add('mediaFile', FileType::class, [
38
                'label' => 'admin.media.mediaFile.label',
39
                'required' => $this->getSubject() && $this->getSubject()->getMedia() ? false : true,
40
            ])
41
            ->add('name', TextType::class, [
42
                'required' => $this->getSubject() && $this->getSubject()->getMedia() ? true : false,
43
                'help_html' => true,
44
                'help' => 'admin.media.name.help',
45
                'label' => 'admin.media.name.label',
46
                'attr' => ['ismedia' => 1, 'class' => 'col-md-6'],
47
            ])
48
            ->add('slug', TextType::class, [
49
                'label' => 'admin.page.slug.label',
50
                'help_html' => true,
51
                'required' => false,
52
                'help' => $this->getSubject() && $this->getSubject()->getSlug()
53
                    ? '<span class="btn btn-link" onclick="toggleDisabled()" id="disabledLinkSlug">
54
                        <i class="fa fa-unlock"></i></span>
55
                        <script>function toggleDisabled() {
56
                            $(".slug_disabled").first().removeAttr("disabled");
57
                            $(".slug_disabled").first().focus();
58
                            $("#disabledLinkSlug").first().remove();
59
                        }</script>'
60
                        .'<small>Changer le slug change l\'URL de l\'image et peut créer des erreurs.</small>'
61
                    : 'admin.page.slug.help',
62
                'attr' => [
63
                    'class' => 'slug_disabled',
64
                    ($this->getSubject() ? ($this->getSubject()->getSlug() ? 'disabled' : 't') : 't') => '',
65
                ],
66
            ])
67
            ->end();
68
69
        $formMapper->with('i18n', ['class' => 'col-md-6']);
70
71
72
        $formMapper->add('names', null, [
73
            'required' => false,
74
            'help_html' => true, 'help' => 'admin.media.names.help',
75
            'label' => 'admin.media.names.label',
76
            'attr' => ['ismedia' => 1, 'class' => 'col-md-6'],
77
        ]);
78
79
        $formMapper->end();
80
81
        if ($media && $media->getMedia()) {
82
            $formMapper->with('admin.media.preview.label', [
83
                'class' => 'col-md-12',
84
                'description' => $this->showMediaPreview(),
85
                //'empty_message' => false, // to uncomment when sonataAdmin 3.62 is released
86
            ])->end();
87
88
            if ($this->issetRelatedPages()) {
89
                $formMapper->with('admin.media.related.label', [
90
                    'class' => 'col-md-12',
91
                    'description' => $this->showRelatedPages(),
92
                    //'empty_message' => false, /// to uncomment when sonataAdmin 3.62 is released
93
                ])->end();
94
            }
95
        }
96
    }
97
98
    protected function showMediaPreview(): string
99
    {
100
        $media = $this->getSubject();
101
102
        $template = false !== strpos($media->getMimeType(), 'image/') ?
103
            '@PiedWebCMS/admin/media_show.preview_image.html.twig'
104
            : '@PiedWebCMS/admin/media_show.preview.html.twig';
105
106
        return $this->getContainer()->get('twig')->render(
107
            $template,
108
            [
109
                'media' => $media,
110
            ]
111
        );
112
    }
113
114
    protected function issetRelatedPages(): bool
115
    {
116
        $relatedPages = $this->getRelatedPages();
117
118
        if (!empty($relatedPages['content'])
119
            || $relatedPages['gallery']->count() > 0
120
            || $relatedPages['mainImage']->count() > 0
121
        ) {
122
            return true;
123
        } else {
124
            return false;
125
        }
126
    }
127
128
    protected function getRelatedPages(): ?array
129
    {
130
        if (null !== $this->relatedPages) {
131
            return $this->relatedPages;
132
        }
133
134
        $media = $this->getSubject();
135
136
        $pages = $this->getConfigurationPool()->getContainer()->get('doctrine')
137
            ->getRepository($this->getContainer()->getParameter('pwc.entity_page'))
138
            ->getPagesUsingMedia($this->liipImage->getBrowserPath($media->getFullPath(), 'default'));
139
140
        $this->relatedPages = [
141
            'content' => $pages,
142
            'gallery' => $media->getPageHasMedias(),
143
            'mainImage' => $media->getMainImagePages(),
144
        ];
145
146
        return $this->relatedPages;
147
    }
148
149
    protected function showRelatedPages(): string
150
    {
151
        return $this->getContainer()->get('twig')->render(
152
            '@PiedWebCMS/admin/media_show.relatedPages.html.twig',
153
            $this->getRelatedPages()
154
        );
155
    }
156
157
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
158
    {
159
        $datagridMapper->add('name', null, [
160
            'label' => 'admin.media.name.label',
161
        ]);
162
        $datagridMapper->add('names', null, [
163
            'label' => 'admin.media.names.label',
164
        ]);
165
    }
166
167
    protected function configureListFields(ListMapper $listMapper)
168
    {
169
        $this->setMosaicDefaultListMode();
170
171
        $listMapper->add('name', null, [
172
            'label' => 'admin.media.name.label',
173
        ]);
174
        $listMapper->add('createdAt', null, [
175
            'label' => 'admin.media.createdAt.label',
176
            'format' => 'd/m/y',
177
        ]);
178
        $listMapper->add('mainColor', null, [
179
            'label' => 'admin.media.mainColor.label',
180
        ]);
181
        $listMapper->add('_action', null, [
182
            'actions' => [
183
                'edit' => [],
184
                'delete' => [],
185
            ],
186
        ]);
187
    }
188
189
    public function getObjectMetadata($media)
190
    {
191
        if (false !== strpos($media->getMimeType(), 'image/')) {
192
            $fullPath = '/'.$media->getRelativeDir().'/'.$media->getMedia();
193
            $thumb = $this->liipImage->getBrowserPath($fullPath, 'thumb');
194
        } else {
195
            $thumb = self::$thumb;
196
        }
197
198
        return new Metadata($media->getName(), null, $thumb);
199
    }
200
}
201