MediaExtension::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Twig\Extension;
15
16
use Sonata\Doctrine\Model\ManagerInterface;
17
use Sonata\MediaBundle\Model\MediaInterface;
18
use Sonata\MediaBundle\Provider\Pool;
19
use Sonata\MediaBundle\Twig\TokenParser\MediaTokenParser;
20
use Sonata\MediaBundle\Twig\TokenParser\PathTokenParser;
21
use Sonata\MediaBundle\Twig\TokenParser\ThumbnailTokenParser;
22
use Twig\Environment;
23
use Twig\Extension\AbstractExtension;
24
use Twig\Extension\InitRuntimeInterface;
25
26
/**
27
 * @final since sonata-project/media-bundle 3.21.0
28
 */
29
class MediaExtension extends AbstractExtension implements InitRuntimeInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Twig\Extension\InitRuntimeInterface has been deprecated with message: since Twig 2.7, to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
30
{
31
    /**
32
     * @var Pool
33
     */
34
    protected $mediaService;
35
36
    /**
37
     * @var array
38
     */
39
    protected $resources = [];
40
41
    /**
42
     * @var ManagerInterface
43
     */
44
    protected $mediaManager;
45
46
    /**
47
     * @var Environment
48
     */
49
    protected $environment;
50
51
    public function __construct(Pool $mediaService, ManagerInterface $mediaManager)
52
    {
53
        $this->mediaService = $mediaService;
54
        $this->mediaManager = $mediaManager;
55
    }
56
57
    public function getTokenParsers()
58
    {
59
        return [
60
            new MediaTokenParser(static::class),
61
            new ThumbnailTokenParser(static::class),
62
            new PathTokenParser(static::class),
63
        ];
64
    }
65
66
    public function initRuntime(Environment $environment): void
67
    {
68
        $this->environment = $environment;
69
    }
70
71
    /**
72
     * @param MediaInterface $media
73
     * @param string         $format
74
     * @param array          $options
75
     *
76
     * @return string
77
     */
78
    public function media($media, $format, $options = [])
79
    {
80
        $media = $this->getMedia($media);
81
82
        if (null === $media) {
83
            return '';
84
        }
85
86
        $provider = $this
87
            ->getMediaService()
88
            ->getProvider($media->getProviderName());
89
90
        $format = $provider->getFormatName($media, $format);
91
92
        $options = $provider->getHelperProperties($media, $format, $options);
93
94
        return $this->render($provider->getTemplate('helper_view'), [
95
            'media' => $media,
96
            'format' => $format,
97
            'options' => $options,
98
        ]);
99
    }
100
101
    /**
102
     * Returns the thumbnail for the provided media.
103
     *
104
     * @param MediaInterface $media
105
     * @param string         $format
106
     * @param array          $options
107
     *
108
     * @return string
109
     */
110
    public function thumbnail($media, $format, $options = [])
111
    {
112
        $media = $this->getMedia($media);
113
114
        if (null === $media) {
115
            return '';
116
        }
117
118
        $provider = $this->getMediaService()
119
           ->getProvider($media->getProviderName());
120
121
        $format = $provider->getFormatName($media, $format);
122
        $format_definition = $provider->getFormat($format);
123
124
        // build option
125
        $defaultOptions = [
126
            'title' => $media->getName(),
127
            'alt' => $media->getName(),
128
        ];
129
130
        if (\is_array($format_definition) && $format_definition['width']) {
131
            $defaultOptions['width'] = $format_definition['width'];
132
        }
133
        if (\is_array($format_definition) && $format_definition['height']) {
134
            $defaultOptions['height'] = $format_definition['height'];
135
        }
136
137
        $options = array_merge($defaultOptions, $options);
138
139
        $options['src'] = $provider->generatePublicUrl($media, $format);
140
141
        return $this->render($provider->getTemplate('helper_thumbnail'), [
142
            'media' => $media,
143
            'options' => $options,
144
        ]);
145
    }
146
147
    /**
148
     * @param string $template
149
     *
150
     * @return mixed
151
     */
152
    public function render($template, array $parameters = [])
153
    {
154
        if (!isset($this->resources[$template])) {
155
            $this->resources[$template] = $this->environment->loadTemplate($template);
156
        }
157
158
        return $this->resources[$template]->render($parameters);
159
    }
160
161
    /**
162
     * @param MediaInterface $media
163
     * @param string         $format
164
     *
165
     * @return string
166
     */
167
    public function path($media, $format)
168
    {
169
        $media = $this->getMedia($media);
170
171
        if (!$media) {
172
            return '';
173
        }
174
175
        $provider = $this->getMediaService()
176
           ->getProvider($media->getProviderName());
177
178
        $format = $provider->getFormatName($media, $format);
179
180
        return $provider->generatePublicUrl($media, $format);
181
    }
182
183
    /**
184
     * @return Pool
185
     */
186
    public function getMediaService()
187
    {
188
        return $this->mediaService;
189
    }
190
191
    /**
192
     * @param mixed $media
193
     */
194
    private function getMedia($media): ?MediaInterface
195
    {
196
        if (!$media instanceof MediaInterface && \strlen((string) $media) > 0) {
197
            $media = $this->mediaManager->findOneBy([
198
                'id' => $media,
199
            ]);
200
        }
201
202
        if (!$media instanceof MediaInterface) {
203
            return null;
204
        }
205
206
        if (MediaInterface::STATUS_OK !== $media->getProviderStatus()) {
207
            return null;
208
        }
209
210
        return $media;
211
    }
212
}
213