Completed
Push — master ( 062693...6b78cb )
by
unknown
10:53
created

MediaExtension::media()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
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\ParameterBag\ParameterBagInterface;
9
use MediaMonks\SonataMediaBundle\Provider\ProviderInterface;
10
use MediaMonks\SonataMediaBundle\Provider\ProviderPool;
11
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
12
use Mockery\Generator\Parameter;
13
14
class MediaExtension extends \Twig_Extension
15
{
16
    /**
17
     * @var ProviderPool
18
     */
19
    private $providerPool;
20
21
    /**
22
     * @var UrlGenerator
23
     */
24
    private $imageUrlGenerator;
25
26
    /**
27
     * @var UrlGenerator
28
     */
29
    private $downloadUrlGenerator;
30
31
    /**
32
     * @param ProviderPool $providerPool
33
     * @param UrlGenerator $imageUrlGenerator
34
     * @param UrlGenerator $downloadUrlGenerator
35
     */
36 13
    public function __construct(
37
        ProviderPool $providerPool,
38
        UrlGenerator $imageUrlGenerator,
39
        UrlGenerator $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_download_url', [$this, 'mediaDownloadUrl']
86
            ),
87 12
            new \Twig_SimpleFilter(
88 12
                'media_supports', [$this, 'mediaSupports']
89
            ),
90
        ];
91
    }
92
93
    /**
94
     * @param \Twig_Environment $environment
95
     * @param MediaInterface $media
96
     * @param $width
97
     * @param $height
98
     * @param array $extra
99
     * @param null $routeName
100
     * @param bool $bustCache
101
     * @return string
102
     */
103 2
    public function media(
104
        \Twig_Environment $environment,
105
        MediaInterface $media,
106
        $width,
107
        $height,
108
        array $extra = [],
109
        $routeName = null,
110
        $bustCache = false
111
    ) {
112 2
        $provider = $this->getProviderByMedia($media);
113
114 2
        if ($provider->supportsEmbed()) {
115 1
            return $this->mediaEmbed($environment, $media, $width, $height, $extra);
116
        }
117
118 1
        return $this->mediaImage($environment, $media, $width, $height, $extra, $routeName, $bustCache);
119
    }
120
121
    /**
122
     * @param \Twig_Environment $environment
123
     * @param MediaInterface $media
124
     * @param $width
125
     * @param $height
126
     * @param array $parameters
127
     * @return string
128
     */
129 3
    public function mediaEmbed(
130
        \Twig_Environment $environment,
131
        MediaInterface $media,
132
        $width,
133
        $height,
134
        array $parameters = []
135
    ) {
136 3
        return $environment->render(
137 3
            $this->getProviderByMedia($media)->getEmbedTemplate(),
138
            [
139 3
                'media'      => $media,
140 3
                'width'      => $width,
141 3
                'height'     => $height,
142 3
                'parameters' => $parameters,
143
            ]
144
        );
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 7
    public function mediaImage(
158
        \Twig_Environment $environment,
159
        MediaInterface $media,
160
        $width,
161
        $height,
162
        array $extra = [],
163
        $routeName = null,
164
        $bustCache = false
165
    ) {
166
167 7
        if ($bustCache) {
168 7
            $extra['bc'] = time();
169
        }
170
171 7
        $src = $this->imageUrlGenerator->generate($media, new ImageParameterBag($width, $height, $extra), $routeName);
172
173 7
        return $environment->render(
174 7
            'MediaMonksSonataMediaBundle:Media:image.html.twig',
175
            [
176 7
                'media'  => $media,
177 7
                'src'    => $src,
178 7
                'width'  => $width,
179 7
                'height' => $height,
180 7
                'title'  => $media->getTitle(),
181
            ]
182
        );
183
    }
184
185
    /**
186
     * @param \Twig_Environment $environment
187
     * @param MediaInterface $media
188
     * @param $width
189
     * @param $height
190
     * @param array $extra
191
     * @param null $routeNameImage
192
     * @param null $routeNameDownload
193
     * @return string
194
     */
195 1
    public function mediaDownload(
196
        \Twig_Environment $environment,
197
        MediaInterface $media,
198
        $width,
199
        $height,
200
        array $extra = [],
201
        $routeNameImage = null,
202
        $routeNameDownload = null
203
    ) {
204 1
        return $environment->render(
205 1
            'MediaMonksSonataMediaBundle:Media:file.html.twig',
206
            [
207 1
                'media'       => $media,
208 1
                'downloadSrc' => $this->downloadUrlGenerator->generate(
209
                    $media,
210 1
                    new DownloadParameterBag(),
211 1
                    $routeNameDownload
212
                ),
213 1
                'src'         => $this->imageUrlGenerator->generate(
214
                    $media,
215 1
                    new ImageParameterBag($width, $height, $extra),
216 1
                    $routeNameImage
217
                ),
218 1
                'width'       => $width,
219 1
                'height'      => $height,
220 1
                'title'       => $media->getTitle(),
221
            ]
222
        );
223
    }
224
225
    /**
226
     * @param MediaInterface $media
227
     * @param null $routeName
228
     * @param array $extra
229
     * @return string
230
     */
231 4
    public function mediaDownloadUrl(MediaInterface $media, $routeName = null, array $extra = [])
232
    {
233 4
        return $this->downloadUrlGenerator->generate($media, new DownloadParameterBag($extra), $routeName);
234
    }
235
236
    /**
237
     * @param MediaInterface $media
238
     * @param $type
239
     * @return mixed
240
     */
241 7
    public function mediaSupports(MediaInterface $media, $type)
242
    {
243 7
        return $this->getProviderByMedia($media)->supports($type);
244
    }
245
246
    /**
247
     * @param MediaInterface $media
248
     * @return ProviderInterface
249
     */
250 7
    private function getProviderByMedia(MediaInterface $media)
251
    {
252 7
        return $this->providerPool->getProvider($media->getProvider());
253
    }
254
}
255