Completed
Push — master ( ea4395...f11038 )
by
unknown
15:56
created

MediaExtension::mediaImage()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 13
cts 13
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 7
crap 2
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\ProviderInterface;
9
use MediaMonks\SonataMediaBundle\Provider\ProviderPool;
10
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
11
12
class MediaExtension extends \Twig_Extension
13
{
14
    /**
15
     * @var ProviderPool
16
     */
17
    private $providerPool;
18
19
    /**
20
     * @var UrlGenerator
21
     */
22
    private $imageUrlGenerator;
23
24
    /**
25
     * @var UrlGenerator
26
     */
27
    private $downloadUrlGenerator;
28
29
    /**
30
     * @param ProviderPool $providerPool
31
     * @param UrlGenerator $imageUrlGenerator
32
     * @param UrlGenerator $downloadUrlGenerator
33
     */
34 13
    public function __construct(
35
        ProviderPool $providerPool,
36
        UrlGenerator $imageUrlGenerator,
37
        UrlGenerator $downloadUrlGenerator
38
    ) {
39 13
        $this->providerPool = $providerPool;
40 13
        $this->imageUrlGenerator = $imageUrlGenerator;
41 13
        $this->downloadUrlGenerator = $downloadUrlGenerator;
42 13
    }
43
44
    /**
45
     * @return string
46
     */
47 13
    public function getName()
48
    {
49 13
        return 'media';
50
    }
51
52
    /**
53
     * @return array
54
     */
55 12
    public function getFilters()
56
    {
57
        return [
58 12
            new \Twig_SimpleFilter(
59 12
                'media', [$this, 'media'], [
60 12
                    'needs_environment' => true,
61 12
                    'is_safe'           => ['html'],
62
                ]
63 12
            ),
64 12
            new \Twig_SimpleFilter(
65 12
                'media_embed', [$this, 'mediaEmbed'], [
66 12
                    'needs_environment' => true,
67 12
                    'is_safe'           => ['html'],
68
                ]
69 12
            ),
70 12
            new \Twig_SimpleFilter(
71 12
                'media_image', [$this, 'mediaImage'], [
72 12
                    'needs_environment' => true,
73 12
                    'is_safe'           => ['html'],
74
                ]
75 12
            ),
76 12
            new \Twig_SimpleFilter(
77 12
                'media_download', [$this, 'mediaDownload'], [
78 12
                    'needs_environment' => true,
79 12
                    'is_safe'           => ['html'],
80
                ]
81 12
            ),
82 12
            new \Twig_SimpleFilter(
83 12
                'media_download_url', [$this, 'mediaDownloadUrl']
84 12
            ),
85 12
            new \Twig_SimpleFilter(
86 12
                'media_supports', [$this, 'mediaSupports']
87 12
            ),
88 12
        ];
89
    }
90
91
    /**
92
     * @param \Twig_Environment $environment
93
     * @param MediaInterface $media
94
     * @param $width
95
     * @param $height
96
     * @param array $extra
97
     * @param null $routeName
98
     * @param bool $bustCache
99
     * @return string
100
     */
101 2
    public function media(
102
        \Twig_Environment $environment,
103
        MediaInterface $media,
104
        $width,
105
        $height,
106
        array $extra = [],
107
        $routeName = null,
108
        $bustCache = false
109
    ) {
110 2
        $provider = $this->getProviderByMedia($media);
111
112 2
        if ($provider->supportsEmbed()) {
113 1
            return $this->mediaEmbed($environment, $media, $width, $height, $extra);
114
        }
115
116 1
        return $this->mediaImage($environment, $media, $width, $height, $extra, $routeName, $bustCache);
117
    }
118
119
    /**
120
     * @param \Twig_Environment $environment
121
     * @param MediaInterface $media
122
     * @param $width
123
     * @param $height
124
     * @param array $parameters
125
     * @return string
126
     */
127 3
    public function mediaEmbed(
128
        \Twig_Environment $environment,
129
        MediaInterface $media,
130
        $width,
131
        $height,
132
        array $parameters = []
133
    ) {
134 3
        return $environment->render(
135 3
            $this->getProviderByMedia($media)->getEmbedTemplate(),
136
            [
137 3
                'media'      => $media,
138 3
                'width'      => $width,
139 3
                'height'     => $height,
140 3
                'parameters' => $parameters,
141
            ]
142 3
        );
143
    }
144
145
    /**
146
     * @param \Twig_Environment $environment
147
     * @param MediaInterface $media
148
     * @param $width
149
     * @param $height
150
     * @param array $extra
151
     * @param null $routeName
152
     * @param bool $bustCache
153
     * @return string
154
     */
155 7
    public function mediaImage(
156
        \Twig_Environment $environment,
157
        MediaInterface $media,
158
        $width,
159
        $height,
160
        array $extra = [],
161
        $routeName = null,
162
        $bustCache = false
163
    ) {
164
165 7
        if ($bustCache) {
166 7
            $extra['bc'] = time();
167 7
        }
168
169 7
        $src = $this->imageUrlGenerator->generate($media, new ImageParameterBag($width, $height, $extra), $routeName);
170
171 7
        return $environment->render(
172 7
            'MediaMonksSonataMediaBundle:Media:image.html.twig',
173
            [
174 7
                'media'  => $media,
175 7
                'src'    => $src,
176 7
                'width'  => $width,
177 7
                'height' => $height,
178 7
                'title'  => $media->getTitle(),
179
            ]
180 7
        );
181
    }
182
183
    /**
184
     * @param \Twig_Environment $environment
185
     * @param MediaInterface $media
186
     * @param $width
187
     * @param $height
188
     * @param array $extra
189
     * @param null $routeNameImage
190
     * @param null $routeNameDownload
191
     * @return string
192
     */
193 1
    public function mediaDownload(
194
        \Twig_Environment $environment,
195
        MediaInterface $media,
196
        $width,
197
        $height,
198
        array $extra = [],
199
        $routeNameImage = null,
200
        $routeNameDownload = null
201
    ) {
202 1
        return $environment->render(
203 1
            'MediaMonksSonataMediaBundle:Media:file.html.twig',
204
            [
205 1
                'media'       => $media,
206 1
                'downloadSrc' => $this->downloadUrlGenerator->generate(
207 1
                    $media,
208 1
                    new DownloadParameterBag(),
209
                    $routeNameDownload
210 1
                ),
211 1
                'src'         => $this->imageUrlGenerator->generate(
212 1
                    $media,
213 1
                    new ImageParameterBag($width, $height, $extra),
214
                    $routeNameImage
215 1
                ),
216 1
                'width'       => $width,
217 1
                'height'      => $height,
218 1
                'title'       => $media->getTitle(),
219
            ]
220 1
        );
221
    }
222
223
    /**
224
     * @param MediaInterface $media
225
     * @param null $routeName
226
     * @param array $extra
227
     * @return string
228
     */
229 4
    public function mediaDownloadUrl(MediaInterface $media, $routeName = null, array $extra = [])
230
    {
231 4
        return $this->downloadUrlGenerator->generate($media, new DownloadParameterBag($extra), $routeName);
232
    }
233
234
    /**
235
     * @param MediaInterface $media
236
     * @param $type
237
     * @return mixed
238
     */
239 7
    public function mediaSupports(MediaInterface $media, $type)
240
    {
241 7
        return $this->getProviderByMedia($media)->supports($type);
242
    }
243
244
    /**
245
     * @param MediaInterface $media
246
     * @return ProviderInterface
247
     */
248 7
    private function getProviderByMedia(MediaInterface $media)
249
    {
250 7
        return $this->providerPool->getProvider($media->getProvider());
251
    }
252
}
253