Completed
Push — master ( 655b8f...bb304a )
by
unknown
16:10
created

MediaExtension   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 287
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
lcom 1
cbo 9
dl 0
loc 287
ccs 84
cts 84
cp 1
rs 10

13 Methods

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