Completed
Push — master ( 8c0a67...1fafee )
by Grégoire
9s
created

MediaExtension   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 0
loc 197
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getTokenParsers() 0 8 1
A initRuntime() 0 4 1
A media() 0 22 2
B thumbnail() 0 36 4
A render() 0 8 2
A path() 0 15 2
A getMediaService() 0 4 1
B getMedia() 0 18 5
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\MediaBundle\Twig\Extension;
13
14
use Sonata\CoreBundle\Model\ManagerInterface;
15
use Sonata\MediaBundle\Model\MediaInterface;
16
use Sonata\MediaBundle\Provider\Pool;
17
use Sonata\MediaBundle\Twig\TokenParser\MediaTokenParser;
18
use Sonata\MediaBundle\Twig\TokenParser\PathTokenParser;
19
use Sonata\MediaBundle\Twig\TokenParser\ThumbnailTokenParser;
20
21
class MediaExtension extends \Twig_Extension implements \Twig_Extension_InitRuntimeInterface
22
{
23
    /**
24
     * @var Pool
25
     */
26
    protected $mediaService;
27
28
    /**
29
     * @var array
30
     */
31
    protected $resources = array();
32
33
    /**
34
     * @var ManagerInterface
35
     */
36
    protected $mediaManager;
37
38
    /**
39
     * @var \Twig_Environment
40
     */
41
    protected $environment;
42
43
    /**
44
     * @param Pool             $mediaService
45
     * @param ManagerInterface $mediaManager
46
     */
47
    public function __construct(Pool $mediaService, ManagerInterface $mediaManager)
48
    {
49
        $this->mediaService = $mediaService;
50
        $this->mediaManager = $mediaManager;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getTokenParsers()
57
    {
58
        return array(
59
            new MediaTokenParser(get_class()),
60
            new ThumbnailTokenParser(get_class()),
61
            new PathTokenParser(get_class()),
62
        );
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function initRuntime(\Twig_Environment $environment)
69
    {
70
        $this->environment = $environment;
71
    }
72
73
    /**
74
     * @param MediaInterface $media
75
     * @param string         $format
76
     * @param array          $options
77
     *
78
     * @return string
79
     */
80
    public function media($media, $format, $options = array())
81
    {
82
        $media = $this->getMedia($media);
83
84
        if (!$media) {
85
            return '';
86
        }
87
88
        $provider = $this
89
            ->getMediaService()
90
            ->getProvider($media->getProviderName());
91
92
        $format = $provider->getFormatName($media, $format);
93
94
        $options = $provider->getHelperProperties($media, $format, $options);
95
96
        return $this->render($provider->getTemplate('helper_view'), array(
97
            'media' => $media,
98
            'format' => $format,
99
            'options' => $options,
100
        ));
101
    }
102
103
    /**
104
     * Returns the thumbnail for the provided media.
105
     *
106
     * @param MediaInterface $media
107
     * @param string         $format
108
     * @param array          $options
109
     *
110
     * @return string
111
     */
112
    public function thumbnail($media, $format, $options = array())
113
    {
114
        $media = $this->getMedia($media);
115
116
        if (!$media) {
117
            return '';
118
        }
119
120
        $provider = $this->getMediaService()
121
           ->getProvider($media->getProviderName());
122
123
        $format = $provider->getFormatName($media, $format);
124
        $format_definition = $provider->getFormat($format);
125
126
        // build option
127
        $defaultOptions = array(
128
            'title' => $media->getName(),
129
            'alt' => $media->getName(),
130
        );
131
132
        if ($format_definition['width']) {
133
            $defaultOptions['width'] = $format_definition['width'];
134
        }
135
        if ($format_definition['height']) {
136
            $defaultOptions['height'] = $format_definition['height'];
137
        }
138
139
        $options = array_merge($defaultOptions, $options);
140
141
        $options['src'] = $provider->generatePublicUrl($media, $format);
142
143
        return $this->render($provider->getTemplate('helper_thumbnail'), array(
144
            'media' => $media,
145
            'options' => $options,
146
        ));
147
    }
148
149
    /**
150
     * @param string $template
151
     * @param array  $parameters
152
     *
153
     * @return mixed
154
     */
155
    public function render($template, array $parameters = array())
156
    {
157
        if (!isset($this->resources[$template])) {
158
            $this->resources[$template] = $this->environment->loadTemplate($template);
159
        }
160
161
        return $this->resources[$template]->render($parameters);
162
    }
163
164
    /**
165
     * @param MediaInterface $media
166
     * @param string         $format
167
     *
168
     * @return string
169
     */
170
    public function path($media, $format)
171
    {
172
        $media = $this->getMedia($media);
173
174
        if (!$media) {
175
            return '';
176
        }
177
178
        $provider = $this->getMediaService()
179
           ->getProvider($media->getProviderName());
180
181
        $format = $provider->getFormatName($media, $format);
182
183
        return $provider->generatePublicUrl($media, $format);
184
    }
185
186
    /**
187
     * @return Pool
188
     */
189
    public function getMediaService()
190
    {
191
        return $this->mediaService;
192
    }
193
194
    /**
195
     * @param mixed $media
196
     *
197
     * @return MediaInterface|null|bool
198
     */
199
    private function getMedia($media)
200
    {
201
        if (!$media instanceof MediaInterface && strlen($media) > 0) {
202
            $media = $this->mediaManager->findOneBy(array(
203
                'id' => $media,
204
            ));
205
        }
206
207
        if (!$media instanceof MediaInterface) {
208
            return false;
209
        }
210
211
        if ($media->getProviderStatus() !== MediaInterface::STATUS_OK) {
212
            return false;
213
        }
214
215
        return $media;
216
    }
217
}
218