MediaHelper::media()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 3
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\Templating\Helper;
15
16
use Sonata\MediaBundle\Model\MediaInterface;
17
use Sonata\MediaBundle\Provider\MediaProviderInterface;
18
use Sonata\MediaBundle\Provider\Pool;
19
use Symfony\Component\Templating\EngineInterface;
20
use Symfony\Component\Templating\Helper\Helper;
21
22
/**
23
 * MediaHelper manages action inclusions.
24
 *
25
 * @author Thomas Rabaix <[email protected]>
26
 *
27
 * @deprecated since sonata-project/media-bundle 3.10, will be removed in 4.0.
28
 * NEXT_MAJOR : remove this class
29
 */
30
class MediaHelper extends Helper
31
{
32
    /**
33
     * @var Pool|null
34
     */
35
    protected $pool = null;
36
37
    /**
38
     * @var EngineInterface|null
39
     */
40
    protected $templating = null;
41
42
    public function __construct(Pool $pool, EngineInterface $templating)
43
    {
44
        $this->pool = $pool;
45
        $this->templating = $templating;
46
    }
47
48
    public function getName()
49
    {
50
        return 'sonata_media';
51
    }
52
53
    /**
54
     * Returns the provider view for the provided media.
55
     *
56
     * @param MediaInterface $media
57
     * @param string         $format
58
     * @param array          $options
59
     *
60
     * @return string
61
     */
62
    public function media($media, $format, $options = [])
63
    {
64
        if (!$media) {
65
            return '';
66
        }
67
68
        $provider = $this->getProvider($media);
69
70
        $format = $provider->getFormatName($media, $format);
71
72
        $options = $provider->getHelperProperties($media, $format, $options);
73
74
        return $this->templating->render($provider->getTemplate('helper_view'), [
75
             'media' => $media,
76
             'format' => $format,
77
             'options' => $options,
78
        ]);
79
    }
80
81
    /**
82
     * Returns the thumbnail for the provided media.
83
     *
84
     * @param MediaInterface $media
85
     * @param string         $format
86
     * @param array          $options
87
     *
88
     * @return string
89
     */
90
    public function thumbnail($media, $format, $options = [])
91
    {
92
        if (!$media) {
93
            return '';
94
        }
95
96
        $provider = $this->getProvider($media);
97
98
        $format = $provider->getFormatName($media, $format);
99
        $formatDefinition = $provider->getFormat($format);
100
101
        // build option
102
        $options = array_merge([
103
            'title' => $media->getName(),
104
            'width' => $formatDefinition['width'],
105
        ], $options);
106
107
        $options['src'] = $provider->generatePublicUrl($media, $format);
108
109
        return $this->getTemplating()->render($provider->getTemplate('helper_thumbnail'), [
0 ignored issues
show
Bug introduced by
The method getTemplating() does not seem to exist on object<Sonata\MediaBundl...ing\Helper\MediaHelper>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
            'media' => $media,
111
            'options' => $options,
112
        ]);
113
    }
114
115
    /**
116
     * @param MediaInterface $media
117
     * @param string         $format
118
     *
119
     * @return string
120
     */
121
    public function path($media, $format)
122
    {
123
        if (!$media) {
124
            return '';
125
        }
126
127
        $provider = $this->getProvider($media);
128
129
        $format = $provider->getFormatName($media, $format);
130
131
        return $provider->generatePublicUrl($media, $format);
132
    }
133
134
    /**
135
     * @return MediaProviderInterface
136
     */
137
    private function getProvider(MediaInterface $media)
138
    {
139
        return $this->pool->getProvider($media->getProviderName());
140
    }
141
}
142