Passed
Push — master ( 94b15a...97dd58 )
by Dev
11:43
created

PageHasMedia::configureFormFields()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 18
ccs 0
cts 10
cp 0
crap 20
rs 9.8666
1
<?php
2
3
namespace PiedWeb\CMSBundle\Admin;
4
5
use Sonata\AdminBundle\Admin\AbstractAdmin;
6
use Sonata\AdminBundle\Datagrid\ListMapper;
7
use Sonata\AdminBundle\Form\FormMapper;
8
use Sonata\AdminBundle\Form\Type\ModelListType;
9
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
10
11
class PageHasMedia extends AbstractAdmin
12
{
13
    private $liipImage;
14
15
    public function setLiipImage($liipImage)
16
    {
17
        $this->liipImage = $liipImage;
18
    }
19
20
    protected function getMedialHelp($media)
21
    {
22
        if (!($media && $media->getMedia() && false !== strpos($media->getMimeType(), 'image/'))) {
23
            return null;
24
        }
25
26
        $fullPath = '/'.$media->getRelativeDir().'/'.$media->getMedia();
27
28
        $editUrl = $this->routeGenerator->generate('admin_app_media_edit', ['id' => $media->getId()]);
0 ignored issues
show
Bug introduced by
The method generate() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        /** @scrutinizer ignore-call */ 
29
        $editUrl = $this->routeGenerator->generate('admin_app_media_edit', ['id' => $media->getId()]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
        $thumbUrl = $this->liipImage->getBrowserPath($fullPath, 'thumb');
30
        $defaultUrl = $this->liipImage->getBrowserPath($fullPath, 'default');
31
32
        $help = '<a href="'.$editUrl.'" target=_blank>';
33
        $help .= '<img src="'.$thumbUrl.'" style="width:100%; max-width:300px">';
34
        $help .= '</a>';
35
        $help .= '<pre onclick="copyElementText(this);" class="btn"';
36
        $help .= ' style="font-size:80%;text-overflow:ellipsis;margin-top:10px;max-width:160px;white-space:nowrap;">';
37
        $help .= '!['.str_replace(['[', '"', ']'], ' ', $media->getName()).']('.$defaultUrl.')';
38
        $help .= '</pre>';
39
40
        return $help;
41
    }
42
43
    protected function configureFormFields(FormMapper $formMapper)
44
    {
45
        $media = $this->getSubject() ? $this->getSubject()->getMedia() : null;
46
47
        $formMapper
48
            ->add(
49
                'media',
50
                ModelListType::class,
51
                [
52
                'required' => false,
53
                'btn_delete' => false,
54
                'btn_edit' => false,
55
                'btn_add' => (!$media) ? ' ' : false,
56
                'btn_list' => (!$media) ? ' ' : false,
57
                'sonata_help' => $this->getMedialHelp($media),
58
                ]
59
            )
60
            ->add('position', HiddenType::class);
61
    }
62
63
    protected function configureListFields(ListMapper $listMapper)
64
    {
65
        $listMapper
66
            ->add('media')
67
            ->add('page');
68
    }
69
}
70