Completed
Push — master ( 4e0960...0d8f75 )
by Benjamin
13:37
created

MediaPreviewExtension::previewIconFilter()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 25
rs 8.8571
cc 3
eloc 16
nc 3
nop 2
1
<?php
2
3
namespace Alpixel\Bundle\MediaBundle\Twig\Extension;
4
5
use Doctrine\ORM\EntityManager;
6
use Symfony\Component\HttpFoundation\RequestStack;
7
8
class MediaPreviewExtension extends \Twig_Extension
9
{
10
    protected $entityManager;
11
    protected $request;
12
    protected $previewIcons;
13
14
    public function __construct(RequestStack $requestStack, EntityManager $entityManager, $previewIcons)
15
    {
16
        $this->request = $requestStack->getCurrentRequest();
17
        $this->entityManager = $entityManager;
18
        $this->previewIcon = $previewIcons;
0 ignored issues
show
Bug introduced by
The property previewIcon does not seem to exist. Did you mean previewIcons?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
19
    }
20
21
    public function getFilters()
22
    {
23
        return [
24
            new \Twig_SimpleFilter('previewIcon', [$this, 'previewIconFilter'], [
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
25
                'is_safe'           => ['html'],
26
                'needs_environment' => true,
27
                ]
28
            ),
29
        ];
30
    }
31
32
    public function previewIconFilter(\Twig_Environment $twig, $secretKey = '')
33
    {
34
        if ($secretKey == '') {
35
            return '';
36
        }
37
38
        $mimeType = $this->getMimeType($secretKey);
39
        $icon = '';
40
41
        $media = $this->entityManager->getRepository("AlpixelMediaBundle:Media")->findOneBySecretKey($secretKey);
42
43
        if (preg_match('/^image/', $mimeType) === 0) {
44
            $icon = $this->getIcon($mimeType);
45
            $link = $this->generatePath(true, $icon);
46
        } else {
47
            $link = $this->generatePath(false, $secretKey);
48
        }
49
50
        return $twig->render('AlpixelMediaBundle:Form:blocks/show_icon.html.twig', [
51
            'link'      => $link,
52
            'icon'      => $icon,
53
            'label'     => $media->getName(),
54
            'secretKey' => $secretKey,
55
        ]);
56
    }
57
58
    protected function getIcon($mimeType)
59
    {
60
        $explMime = explode('/', $mimeType);
61
        $mime = (isset($explMime[1])) ? $explMime[1] : '';
62
63
        return (array_key_exists($mime, $this->previewIcon)) ? $this->previewIcon[$mime] : $this->previewIcon['unknown'];
0 ignored issues
show
Bug introduced by
The property previewIcon does not seem to exist. Did you mean previewIcons?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
64
    }
65
66
    protected function generatePath($isIcon, $str)
67
    {
68
        if ($isIcon === true) {
69
            return $this->request->getSchemeAndHttpHost().$this->request->getBasePath().'/bundles/alpixelmedia/images/'.$str;
70
        }
71
72
        return $this->request->getSchemeAndHttpHost().$this->request->getBaseUrl().'/media/'.$str.'/admin';
73
    }
74
75
    public function generatePathFromSecretKey($secretKey)
76
    {
77
        if ($secretKey == '') {
78
            return '';
79
        }
80
81
        $mimeType = $this->getMimeType($secretKey);
82
83
        if (preg_match('/^image/', $mimeType) === 0) {
84
            $icon = $this->getIcon($mimeType);
85
86
            return $this->generatePath(true, $icon);
87
        }
88
89
        return $this->generatePath(false, $secretKey);
90
    }
91
92
    protected function getMimeType($secretKey)
93
    {
94
        $mediaObject = $this->entityManager->getRepository('AlpixelMediaBundle:Media')->findOneBySecretKey($secretKey);
95
96
        return ($mediaObject !== null) ? $mediaObject->getMime() : null;
97
    }
98
99
    public function getName()
100
    {
101
        return 'alpixel_media_twig_media_preview_extension';
102
    }
103
}
104