Completed
Push — master ( e48220...15f13c )
by
unknown
07:36
created

MediaExtension::getFilters()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 26
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
crap 2
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Twig\Extension;
4
5
use MediaMonks\SonataMediaBundle\Generator\UrlGenerator;
6
use MediaMonks\SonataMediaBundle\Provider\ProviderInterface;
7
use MediaMonks\SonataMediaBundle\Provider\ProviderPool;
8
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
9
10
class MediaExtension extends \Twig_Extension
11
{
12
    /**
13
     * @var ProviderPool
14
     */
15
    private $providerPool;
16
17
    /**
18
     * @var UrlGenerator
19
     */
20
    private $urlGenerator;
21
22
    /**
23
     * @param ProviderPool $providerPool
24
     * @param UrlGenerator $urlGenerator
25
     */
26
    public function __construct(ProviderPool $providerPool, UrlGenerator $urlGenerator)
27
    {
28
        $this->providerPool = $providerPool;
29
        $this->urlGenerator = $urlGenerator;
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public function getName()
36
    {
37
        return 'media';
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function getFilters()
44
    {
45
        return [
46
            new \Twig_SimpleFilter(
47
                'media', [$this, 'media'], [
48
                    'needs_environment' => true,
49
                    'is_safe'           => ['html'],
50
                ]
51
            ),
52
            new \Twig_SimpleFilter(
53
                'media_image', [$this, 'mediaImage'], [
54
                    'needs_environment' => true,
55
                    'is_safe'           => ['html'],
56
                ]
57
            ),
58
            new \Twig_SimpleFilter(
59
                'media_download', [$this, 'mediaDownload'], [
60
                    'needs_environment' => true,
61
                    'is_safe'           => ['html'],
62
                ]
63
            ),
64
            new \Twig_SimpleFilter(
65
                'media_supports', [$this, 'mediaSupports']
66
            ),
67
        ];
68
    }
69
70
    /**
71
     * @param \Twig_Environment $environment
72
     * @param MediaInterface $media
73
     * @param $width
74
     * @param $height
75
     * @param array $parameters
76
     * @return string
77
     */
78
    public function media(
79
        \Twig_Environment $environment,
80
        MediaInterface $media,
81
        $width,
82
        $height,
83
        array $parameters = []
84
    ) {
85
        return $environment->render(
86
            $this->getProviderByMedia($media)->getMediaTemplate(),
87
            [
88
                'media'      => $media,
89
                'width'      => $width,
90
                'height'     => $height,
91
                'parameters' => $parameters,
92
            ]
93
        );
94
    }
95
96
    /**
97
     * @param \Twig_Environment $environment
98
     * @param MediaInterface $media
99
     * @param $width
100
     * @param $height
101
     * @param array $parameters
102
     * @param null $routeName
103
     * @param bool $bustCache
104
     * @return string
105
     */
106
    public function mediaImage(
107
        \Twig_Environment $environment,
108
        MediaInterface $media,
109
        $width,
110
        $height,
111
        array $parameters = [],
112
        $routeName = null,
113
        $bustCache = false
114
    ) {
115
        $parameters += [
116
            'w' => $width,
117
            'h' => $height,
118
        ];
119
120
        $src = $this->urlGenerator->generate($media, $parameters, $routeName);
121
122
        if ($bustCache) {
123
            $src .= '&bc='.time();
124
        }
125
126
        return $environment->render(
127
            'MediaMonksSonataMediaBundle:Image:image.html.twig',
128
            [
129
                'src'    => $src,
130
                'width'  => $width,
131
                'height' => $height,
132
                'title'  => $media->getTitle(),
133
            ]
134
        );
135
    }
136
137
    /**
138
     * @param \Twig_Environment $environment
139
     * @param MediaInterface $media
140
     * @param $width
141
     * @param $height
142
     * @param array $parameters
143
     * @param null $routeName
144
     * @return string
145
     */
146
    public function mediaDownload(
147
        \Twig_Environment $environment,
148
        MediaInterface $media,
149
        $width,
150
        $height,
151
        array $parameters = [],
152
        $routeName = null
153
    ) {
154
        $parameters += [
155
            'w' => $width,
156
            'h' => $height,
157
        ];
158
159
        return $environment->render(
160
            'MediaMonksSonataMediaBundle:Image:file.html.twig',
161
            [
162
                'src'    => $this->urlGenerator->generate($media, $parameters, $routeName),
163
                'width'  => $width,
164
                'height' => $height,
165
                'title'  => $media->getTitle(),
166
            ]
167
        );
168
    }
169
170
    /**
171
     * @param MediaInterface $media
172
     * @param $type
173
     * @return mixed
174
     */
175
    public function mediaSupports(MediaInterface $media, $type)
176
    {
177
        return $this->getProviderByMedia($media)->supports($type);
178
    }
179
180
    /**
181
     * @param MediaInterface $media
182
     * @return ProviderInterface
183
     */
184
    private function getProviderByMedia(MediaInterface $media)
185
    {
186
        return $this->providerPool->getProvider($media->getProvider());
187
    }
188
}
189