MediaExtension::getFilters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 17
cts 17
cp 1
rs 9.36
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Twig\Extension;
4
5
use MediaMonks\SonataMediaBundle\Generator\DownloadUrlGenerator;
6
use MediaMonks\SonataMediaBundle\Generator\ImageUrlGenerator;
7
use MediaMonks\SonataMediaBundle\ParameterBag\ImageParameterBag;
8
use MediaMonks\SonataMediaBundle\Provider\DownloadableProviderInterface;
9
use MediaMonks\SonataMediaBundle\Provider\EmbeddableProviderInterface;
10
use MediaMonks\SonataMediaBundle\Provider\ProviderInterface;
11
use MediaMonks\SonataMediaBundle\Provider\ProviderPool;
12
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
13
14
class MediaExtension extends \Twig_Extension
15
{
16
    /**
17
     * @var ProviderPool
18
     */
19
    private $providerPool;
20
21
    /**
22
     * @var ImageUrlGenerator
23
     */
24
    private $imageUrlGenerator;
25
26
    /**
27
     * @var DownloadUrlGenerator
28
     */
29
    private $downloadUrlGenerator;
30
31
    /**
32
     * @param ProviderPool $providerPool
33
     * @param ImageUrlGenerator $imageUrlGenerator
34
     * @param DownloadUrlGenerator $downloadUrlGenerator
35
     */
36 13
    public function __construct(
37
        ProviderPool $providerPool,
38
        ImageUrlGenerator $imageUrlGenerator,
39
        DownloadUrlGenerator $downloadUrlGenerator
40
    ) {
41 13
        $this->providerPool = $providerPool;
42 13
        $this->imageUrlGenerator = $imageUrlGenerator;
43 13
        $this->downloadUrlGenerator = $downloadUrlGenerator;
44 13
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getName()
50
    {
51
        return 'media';
52
    }
53
54
    /**
55
     * @return array
56
     */
57 12
    public function getFilters()
58
    {
59
        return [
60 12
            new \Twig_SimpleFilter(
61 12
                'media', [$this, 'media'], [
62 12
                    'needs_environment' => true,
63
                    'is_safe'           => ['html'],
64
                ]
65
            ),
66 12
            new \Twig_SimpleFilter(
67 12
                'media_embed', [$this, 'mediaEmbed'], [
68 12
                    'needs_environment' => true,
69
                    'is_safe'           => ['html'],
70
                ]
71
            ),
72 12
            new \Twig_SimpleFilter(
73 12
                'media_image', [$this, 'mediaImage'], [
74 12
                    'needs_environment' => true,
75
                    'is_safe'           => ['html'],
76
                ]
77
            ),
78 12
            new \Twig_SimpleFilter(
79 12
                'media_download', [$this, 'mediaDownload'], [
80 12
                    'needs_environment' => true,
81
                    'is_safe'           => ['html'],
82
                ]
83
            ),
84 12
            new \Twig_SimpleFilter(
85 12
                'media_image_url', [$this, 'mediaImageUrl']
86
            ),
87 12
            new \Twig_SimpleFilter(
88 12
                'media_download_url', [$this, 'mediaDownloadUrl']
89
            )
90
        ];
91
    }
92
93
    /**
94
     * @return array
95
     */
96 12
    public function getTests()
97
    {
98
        return [
99 12
            new \Twig_SimpleTest('media_downloadable', [$this, 'isDownloadable']),
100 12
            new \Twig_SimpleTest('media_embeddable', [$this, 'isEmbeddable'])
101
        ];
102
    }
103
104
    /**
105
     * @param MediaInterface $media
106
     * @return bool
107
     */
108 7
    public function isDownloadable(MediaInterface $media)
109
    {
110 7
        return $this->getProviderByMedia($media) instanceof DownloadableProviderInterface;
111
    }
112
113
    /**
114
     * @param MediaInterface $media
115
     * @return bool
116
     */
117 7
    public function isEmbeddable(MediaInterface $media)
118
    {
119 7
        return $this->getProviderByMedia($media) instanceof EmbeddableProviderInterface;
120
    }
121
122
    /**
123
     * @param \Twig_Environment $environment
124
     * @param MediaInterface $media
125
     * @param $width
126
     * @param $height
127
     * @param array $extra
128
     * @param null $routeName
129
     * @param bool $bustCache
130
     * @return string
131
     */
132 2
    public function media(
133
        \Twig_Environment $environment,
134
        MediaInterface $media,
135
        $width,
136
        $height,
137
        array $extra = [],
138
        $routeName = null,
139
        $bustCache = false
140
    ) {
141 2
        if ($this->isEmbeddable($media)) {
142 1
            return $this->mediaEmbed($environment, $media, $width, $height, $extra);
143
        }
144
145 1
        return $this->mediaImage($environment, $media, $width, $height, $extra, $routeName, $bustCache);
146
    }
147
148
    /**
149
     * @param \Twig_Environment $environment
150
     * @param MediaInterface $media
151
     * @param $width
152
     * @param $height
153
     * @param array $extra
154
     * @param null $routeName
155
     * @param bool $bustCache
156
     * @return string
157
     */
158 4
    public function mediaEmbed(
159
        \Twig_Environment $environment,
160
        MediaInterface $media,
161
        $width,
162
        $height,
163
        array $extra = [],
164
        $routeName = null,
165
        $bustCache = false
166
    ) {
167 4
        if (!$this->isEmbeddable($media)) {
168 1
            return $this->mediaImage($environment, $media, $width, $height, $extra, $routeName, $bustCache);
169
        }
170
171 3
        return $environment->render(
172 3
            $this->getProviderByMedia($media)->getEmbedTemplate(),
173
            [
174 3
                'media'      => $media,
175 3
                'width'      => $width,
176 3
                'height'     => $height,
177 3
                'parameters' => $extra,
178
            ]
179
        );
180
    }
181
182
    /**
183
     * @param \Twig_Environment $environment
184
     * @param MediaInterface $media
185
     * @param $width
186
     * @param $height
187
     * @param array $extra
188
     * @param null $routeName
189
     * @param bool $bustCache
190
     * @return string
191
     */
192 7
    public function mediaImage(
193
        \Twig_Environment $environment,
194
        MediaInterface $media,
195
        $width,
196
        $height,
197
        array $extra = [],
198
        $routeName = null,
199
        $bustCache = false
200
    ) {
201
202 7
        if ($bustCache) {
203 7
            $extra['bc'] = time();
204
        }
205
206 7
        $src = $this->imageUrlGenerator->generate($media, new ImageParameterBag($width, $height, $extra), $routeName);
207
208 7
        return $environment->render(
209 7
            '@MediaMonksSonataMedia/Media/image.html.twig',
210
            [
211 7
                'media'  => $media,
212 7
                'src'    => $src,
213 7
                'width'  => $width,
214 7
                'height' => $height,
215 7
                'title'  => $media->getTitle(),
216
            ]
217
        );
218
    }
219
220
    /**
221
     * @param \Twig_Environment $environment
222
     * @param MediaInterface $media
223
     * @param $width
224
     * @param $height
225
     * @param array $extraImage
226
     * @param array $extraDownload
227
     * @param null $routeNameImage
228
     * @param null $routeNameDownload
229
     * @return string
230
     */
231 1
    public function mediaDownload(
232
        \Twig_Environment $environment,
233
        MediaInterface $media,
234
        $width,
235
        $height,
236
        array $extraImage = [],
237
        array $extraDownload = [],
238
        $routeNameImage = null,
239
        $routeNameDownload = null
240
    ) {
241 1
        if (!$this->isDownloadable($media)) {
242
            return '';
243
        }
244
245 1
        return $environment->render(
246 1
            '@MediaMonksSonataMedia/Media/file.html.twig',
247
            [
248 1
                'media'       => $media,
249 1
                'downloadSrc' => $this->downloadUrlGenerator->generateDownloadUrl(
250 1
                    $media,
251 1
                    $extraDownload,
252 1
                    $routeNameDownload
253
                ),
254 1
                'src'         => $this->imageUrlGenerator->generateImageUrl(
255 1
                    $media,
256 1
                    $width, $height, $extraImage,
257 1
                    $routeNameImage
258
                ),
259 1
                'width'       => $width,
260 1
                'height'      => $height,
261 1
                'title'       => $media->getTitle(),
262
            ]
263
        );
264
    }
265
266
    /**
267
     * @param MediaInterface $media
268
     * @param int $width
269
     * @param int $height
270
     * @param array $extra
271
     * @param null $routeName
272
     * @return string
273
     */
274 2
    public function mediaImageUrl(MediaInterface $media, $width, $height, array $extra = [], $routeName = null)
275
    {
276 2
        return $this->imageUrlGenerator->generateImageUrl($media, $width, $height, $extra, $routeName);
277
    }
278
279
    /**
280
     * @param MediaInterface $media
281
     * @param array $extra
282
     * @param null $routeName
283
     * @return string
284
     */
285 5
    public function mediaDownloadUrl(MediaInterface $media, array $extra = [], $routeName = null)
286
    {
287 5
        return $this->downloadUrlGenerator->generateDownloadUrl($media, $extra, $routeName);
288
    }
289
290
    /**
291
     * @param MediaInterface $media
292
     * @return ProviderInterface
293
     */
294 7
    private function getProviderByMedia(MediaInterface $media)
295
    {
296 7
        return $this->providerPool->getProvider($media->getProvider());
297
    }
298
}
299