Completed
Push — master ( ce130b...97424e )
by
unknown
14:29
created

MediaExtension   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 285
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 98.94%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 8
dl 0
loc 285
ccs 93
cts 94
cp 0.9894
rs 10
c 0
b 0
f 0

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 34 2
A mediaImageUrl() 0 4 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\UrlGeneratorInterface;
6
use MediaMonks\SonataMediaBundle\ParameterBag\ImageParameterBag;
7
use MediaMonks\SonataMediaBundle\Provider\DownloadableProviderInterface;
8
use MediaMonks\SonataMediaBundle\Provider\EmbeddableProviderInterface;
9
use MediaMonks\SonataMediaBundle\Provider\ProviderInterface;
10
use MediaMonks\SonataMediaBundle\Provider\ProviderPool;
11
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
12
13
class MediaExtension extends \Twig_Extension
14
{
15
    /**
16
     * @var ProviderPool
17
     */
18
    private $providerPool;
19
20
    /**
21
     * @var UrlGeneratorInterface
22
     */
23
    private $imageUrlGenerator;
24
25
    /**
26
     * @var UrlGeneratorInterface
27
     */
28
    private $downloadUrlGenerator;
29
30
    /**
31
     * @param ProviderPool $providerPool
32
     * @param UrlGeneratorInterface $imageUrlGenerator
33
     * @param UrlGeneratorInterface $downloadUrlGenerator
34
     */
35 13
    public function __construct(
36
        ProviderPool $providerPool,
37
        UrlGeneratorInterface $imageUrlGenerator,
38
        UrlGeneratorInterface $downloadUrlGenerator
39
    ) {
40 13
        $this->providerPool = $providerPool;
41 13
        $this->imageUrlGenerator = $imageUrlGenerator;
42 13
        $this->downloadUrlGenerator = $downloadUrlGenerator;
43 13
    }
44
45
    /**
46
     * @return string
47
     */
48 13
    public function getName()
49
    {
50 13
        return 'media';
51
    }
52
53
    /**
54
     * @return array
55
     */
56 12
    public function getFilters()
57
    {
58
        return [
59 12
            new \Twig_SimpleFilter(
60 12
                'media', [$this, 'media'], [
61 12
                    'needs_environment' => true,
62 12
                    'is_safe'           => ['html'],
63
                ]
64 12
            ),
65 12
            new \Twig_SimpleFilter(
66 12
                'media_embed', [$this, 'mediaEmbed'], [
67 12
                    'needs_environment' => true,
68 12
                    'is_safe'           => ['html'],
69
                ]
70 12
            ),
71 12
            new \Twig_SimpleFilter(
72 12
                'media_image', [$this, 'mediaImage'], [
73 12
                    'needs_environment' => true,
74 12
                    'is_safe'           => ['html'],
75
                ]
76 12
            ),
77 12
            new \Twig_SimpleFilter(
78 12
                'media_download', [$this, 'mediaDownload'], [
79 12
                    'needs_environment' => true,
80 12
                    'is_safe'           => ['html'],
81
                ]
82 12
            ),
83 12
            new \Twig_SimpleFilter(
84 12
                'media_image_url', [$this, 'mediaImageUrl']
85 12
            ),
86 12
            new \Twig_SimpleFilter(
87 12
                'media_download_url', [$this, 'mediaDownloadUrl']
88 12
            )
89 12
        ];
90
    }
91
92
    /**
93
     * @return array
94
     */
95 12
    public function getTests()
96
    {
97
        return [
98 12
            new \Twig_SimpleTest('media_downloadable', [$this, 'isDownloadable']),
99 12
            new \Twig_SimpleTest('media_embeddable', [$this, 'isEmbeddable'])
100 12
        ];
101
    }
102
103
    /**
104
     * @param MediaInterface $media
105
     * @return bool
106
     */
107 7
    public function isDownloadable(MediaInterface $media)
108
    {
109 7
        return $this->getProviderByMedia($media) instanceof DownloadableProviderInterface;
110
    }
111
112
    /**
113
     * @param MediaInterface $media
114
     * @return bool
115
     */
116 7
    public function isEmbeddable(MediaInterface $media)
117
    {
118 7
        return $this->getProviderByMedia($media) instanceof EmbeddableProviderInterface;
119
    }
120
121
    /**
122
     * @param \Twig_Environment $environment
123
     * @param MediaInterface $media
124
     * @param $width
125
     * @param $height
126
     * @param array $extra
127
     * @param null $routeName
128
     * @param bool $bustCache
129
     * @return string
130
     */
131 2
    public function media(
132
        \Twig_Environment $environment,
133
        MediaInterface $media,
134
        $width,
135
        $height,
136
        array $extra = [],
137
        $routeName = null,
138
        $bustCache = false
139
    ) {
140 2
        if ($this->isEmbeddable($media)) {
141 1
            return $this->mediaEmbed($environment, $media, $width, $height, $extra);
142
        }
143
144 1
        return $this->mediaImage($environment, $media, $width, $height, $extra, $routeName, $bustCache);
145
    }
146
147
    /**
148
     * @param \Twig_Environment $environment
149
     * @param MediaInterface $media
150
     * @param $width
151
     * @param $height
152
     * @param array $extra
153
     * @param null $routeName
154
     * @param bool $bustCache
155
     * @return string
156
     */
157 4
    public function mediaEmbed(
158
        \Twig_Environment $environment,
159
        MediaInterface $media,
160
        $width,
161
        $height,
162
        array $extra = [],
163
        $routeName = null,
164
        $bustCache = false
165
    ) {
166 4
        if (!$this->isEmbeddable($media)) {
167 1
            return $this->mediaImage($environment, $media, $width, $height, $extra, $routeName, $bustCache);
168
        }
169
170 3
        return $environment->render(
171 3
            $this->getProviderByMedia($media)->getEmbedTemplate(),
172
            [
173 3
                'media'      => $media,
174 3
                'width'      => $width,
175 3
                'height'     => $height,
176 3
                'parameters' => $extra,
177
            ]
178 3
        );
179
    }
180
181
    /**
182
     * @param \Twig_Environment $environment
183
     * @param MediaInterface $media
184
     * @param $width
185
     * @param $height
186
     * @param array $extra
187
     * @param null $routeName
188
     * @param bool $bustCache
189
     * @return string
190
     */
191 7
    public function mediaImage(
192
        \Twig_Environment $environment,
193
        MediaInterface $media,
194
        $width,
195
        $height,
196
        array $extra = [],
197
        $routeName = null,
198
        $bustCache = false
199
    ) {
200
201 7
        if ($bustCache) {
202 7
            $extra['bc'] = time();
203 7
        }
204
205 7
        $src = $this->imageUrlGenerator->generate($media, new ImageParameterBag($width, $height, $extra), $routeName);
0 ignored issues
show
Bug introduced by
The call to generate() misses a required argument $referenceType.

This check looks for function calls that miss required arguments.

Loading history...
206
207 7
        return $environment->render(
208 7
            'MediaMonksSonataMediaBundle:Media:image.html.twig',
209
            [
210 7
                'media'  => $media,
211 7
                'src'    => $src,
212 7
                'width'  => $width,
213 7
                'height' => $height,
214 7
                'title'  => $media->getTitle(),
215
            ]
216 7
        );
217
    }
218
219
    /**
220
     * @param \Twig_Environment $environment
221
     * @param MediaInterface $media
222
     * @param $width
223
     * @param $height
224
     * @param array $extraImage
225
     * @param array $extraDownload
226
     * @param null $routeNameImage
227
     * @param null $routeNameDownload
228
     * @return string
229
     */
230 1
    public function mediaDownload(
231
        \Twig_Environment $environment,
232
        MediaInterface $media,
233
        $width,
234
        $height,
235
        array $extraImage = [],
236
        array $extraDownload = [],
237
        $routeNameImage = null,
238
        $routeNameDownload = null
239
    ) {
240 1
        if (!$this->isDownloadable($media)) {
241
            return '';
242
        }
243
244 1
        return $environment->render(
245 1
            'MediaMonksSonataMediaBundle:Media:file.html.twig',
246
            [
247 1
                'media'       => $media,
248 1
                'downloadSrc' => $this->downloadUrlGenerator->generateDownloadUrl(
0 ignored issues
show
Bug introduced by
The method generateDownloadUrl() does not exist on MediaMonks\SonataMediaBu...r\UrlGeneratorInterface. Did you maybe mean generate()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
249 1
                    $media,
250 1
                    $extraDownload,
251
                    $routeNameDownload
252 1
                ),
253 1
                'src'         => $this->imageUrlGenerator->generateImageUrl(
0 ignored issues
show
Bug introduced by
The method generateImageUrl() does not exist on MediaMonks\SonataMediaBu...r\UrlGeneratorInterface. Did you maybe mean generate()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
254 1
                    $media,
255 1
                    $width, $height, $extraImage,
256
                    $routeNameImage
257 1
                ),
258 1
                'width'       => $width,
259 1
                'height'      => $height,
260 1
                'title'       => $media->getTitle(),
261
            ]
262 1
        );
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 2
    public function mediaImageUrl(MediaInterface $media, $width, $height, array $extra = [], $routeName = null)
274
    {
275 2
        return $this->imageUrlGenerator->generateImageUrl($media, $width, $height, $extra, $routeName);
0 ignored issues
show
Bug introduced by
The method generateImageUrl() does not exist on MediaMonks\SonataMediaBu...r\UrlGeneratorInterface. Did you maybe mean generate()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
276
    }
277
278
    /**
279
     * @param MediaInterface $media
280
     * @param array $extra
281
     * @param null $routeName
282
     * @return string
283
     */
284 5
    public function mediaDownloadUrl(MediaInterface $media, array $extra = [], $routeName = null)
285
    {
286 5
        return $this->downloadUrlGenerator->generateDownloadUrl($media, $extra, $routeName);
0 ignored issues
show
Bug introduced by
The method generateDownloadUrl() does not exist on MediaMonks\SonataMediaBu...r\UrlGeneratorInterface. Did you maybe mean generate()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
287
    }
288
289
    /**
290
     * @param MediaInterface $media
291
     * @return ProviderInterface
292
     */
293 7
    private function getProviderByMedia(MediaInterface $media)
294
    {
295 7
        return $this->providerPool->getProvider($media->getProvider());
296
    }
297
}
298