Completed
Push — master ( 7db90d...b62044 )
by
unknown
05:58
created

MediaExtension::mediaDownload()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 18
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 25
nc 2
nop 8
crap 6

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 3
    public function __construct(
37
        ProviderPool $providerPool,
38
        ImageUrlGenerator $imageUrlGenerator,
39
        DownloadUrlGenerator $downloadUrlGenerator
40
    ) {
41 3
        $this->providerPool = $providerPool;
42 3
        $this->imageUrlGenerator = $imageUrlGenerator;
43 3
        $this->downloadUrlGenerator = $downloadUrlGenerator;
44 3
    }
45
46
    /**
47
     * @return string
48
     */
49 3
    public function getName()
50
    {
51 3
        return 'media';
52
    }
53
54
    /**
55
     * @return array
56
     */
57 1
    public function getFilters()
58
    {
59
        return [
60 1
            new \Twig_SimpleFilter(
61 1
                'media', [$this, 'media'], [
62 1
                    'needs_environment' => true,
63 1
                    'is_safe'           => ['html'],
64
                ]
65 1
            ),
66 1
            new \Twig_SimpleFilter(
67 1
                'media_embed', [$this, 'mediaEmbed'], [
68 1
                    'needs_environment' => true,
69 1
                    'is_safe'           => ['html'],
70
                ]
71 1
            ),
72 1
            new \Twig_SimpleFilter(
73 1
                'media_image', [$this, 'mediaImage'], [
74 1
                    'needs_environment' => true,
75 1
                    'is_safe'           => ['html'],
76
                ]
77 1
            ),
78 1
            new \Twig_SimpleFilter(
79 1
                'media_download', [$this, 'mediaDownload'], [
80 1
                    'needs_environment' => true,
81 1
                    'is_safe'           => ['html'],
82
                ]
83 1
            ),
84 1
            new \Twig_SimpleFilter(
85 1
                'media_image_url', [$this, 'mediaImageUrl']
86 1
            ),
87 1
            new \Twig_SimpleFilter(
88 1
                'media_download_url', [$this, 'mediaDownloadUrl']
89 1
            )
90 1
        ];
91
    }
92
93
    /**
94
     * @return array
95
     */
96 1
    public function getTests()
97
    {
98
        return [
99 1
            new \Twig_SimpleTest('media_downloadable', [$this, 'isDownloadable']),
100 1
            new \Twig_SimpleTest('media_embeddable', [$this, 'isEmbeddable'])
101 1
        ];
102
    }
103
104
    /**
105
     * @param MediaInterface $media
106
     * @return bool
107
     */
108
    public function isDownloadable(MediaInterface $media)
109
    {
110
        return $this->getProviderByMedia($media) instanceof DownloadableProviderInterface;
111
    }
112
113
    /**
114
     * @param MediaInterface $media
115
     * @return bool
116
     */
117
    public function isEmbeddable(MediaInterface $media)
118
    {
119
        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
    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
        if ($this->isEmbeddable($media)) {
142
            return $this->mediaEmbed($environment, $media, $width, $height, $extra);
143
        }
144
145
        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
    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
        if (!$this->isEmbeddable($media)) {
168
            return $this->mediaImage($environment, $media, $width, $height, $extra, $routeName, $bustCache);
169
        }
170
171
        return $environment->render(
172
            $this->getProviderByMedia($media)->getEmbedTemplate(),
173
            [
174
                'media'      => $media,
175
                'width'      => $width,
176
                'height'     => $height,
177
                '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
    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
        if ($bustCache) {
203
            $extra['bc'] = time();
204
        }
205
206
        $src = $this->imageUrlGenerator->generate($media, new ImageParameterBag($width, $height, $extra), $routeName);
207
208
        return $environment->render(
209
            'MediaMonksSonataMediaBundle:Media:image.html.twig',
210
            [
211
                'media'  => $media,
212
                'src'    => $src,
213
                'width'  => $width,
214
                'height' => $height,
215
                '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
    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
        if (!$this->isDownloadable($media)) {
242
            return '';
243
        }
244
245
        return $environment->render(
246
            'MediaMonksSonataMediaBundle:Media:file.html.twig',
247
            [
248
                'media'       => $media,
249
                'downloadSrc' => $this->downloadUrlGenerator->generateDownloadUrl(
250
                    $media,
251
                    $extraDownload,
252
                    $routeNameDownload
253
                ),
254
                'src'         => $this->imageUrlGenerator->generateImageUrl(
255
                    $media,
256
                    $width, $height, $extraImage,
257
                    $routeNameImage
258
                ),
259
                'width'       => $width,
260
                'height'      => $height,
261
                '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
    public function mediaImageUrl(MediaInterface $media, $width, $height, array $extra = [], $routeName = null)
275
    {
276
        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
    public function mediaDownloadUrl(MediaInterface $media, array $extra = [], $routeName = null)
286
    {
287
        return $this->downloadUrlGenerator->generateDownloadUrl($media, $extra, $routeName);
288
    }
289
290
    /**
291
     * @param MediaInterface $media
292
     * @return ProviderInterface
293
     */
294
    private function getProviderByMedia(MediaInterface $media)
295
    {
296
        return $this->providerPool->getProvider($media->getProvider());
297
    }
298
}
299