Completed
Push — master ( 2d9a32...09a899 )
by
unknown
17:36 queued 06:37
created

MediaExtension::mediaImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 14
cp 0
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 6
crap 2
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Twig\Extension;
4
5
use MediaMonks\SonataMediaBundle\Provider\ProviderPool;
6
use MediaMonks\SonataMediaBundle\Helper\Parameter;
7
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
8
9
class MediaExtension extends \Twig_Extension
10
{
11
    /**
12
     * @var ProviderPool
13
     */
14
    protected $providerPool;
15
16
    /**
17
     * @var Parameter
18
     */
19
    protected $parameterHelper;
20
21
    /**
22
     * MediaExtension constructor.
23
     * @param Parameter $parameter
24
     */
25
    public function __construct(ProviderPool $providerPool, Parameter $parameter)
26
    {
27
        $this->providerPool = $providerPool;
28
        $this->parameterHelper = $parameter;
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public function getName()
35
    {
36
        return 'media';
37
    }
38
39
    /**
40
     * @return array
41
     */
42
    public function getFilters()
43
    {
44
        return [
45
            new \Twig_SimpleFilter(
46
                'media', [$this, 'media'], [
47
                    'needs_environment' => true,
48
                    'is_safe'           => ['html'],
49
                ]
50
            ),
51
            new \Twig_SimpleFilter(
52
                'media_image', [$this, 'mediaImage'], [
53
                    'needs_environment' => true,
54
                    'is_safe'           => ['html'],
55
                ]
56
            ),
57
            new \Twig_SimpleFilter(
58
                'media_type', [$this, 'mediaType'], [
59
                    'needs_environment' => true,
60
                    'is_safe'           => ['html'],
61
                ]
62
            ),
63
        ];
64
    }
65
66
    /**
67
     * @param \Twig_Environment $environment
68
     * @param MediaInterface $media
69
     * @param int $width
70
     * @param int $height
71
     * @param string $type
72
     * @param array $parameters
73
     * @return string
74
     */
75
    public function media(
76
        \Twig_Environment $environment,
77
        MediaInterface $media,
78
        $width,
79
        $height,
80
        $type = Parameter::ROUTE_NAME_DEFAULT,
81
        array $parameters = []
82
    ) {
83
        $provider = $this->providerPool->getProvider($media->getProviderName());
84
85
        return $environment->render(
86
            $provider->getMediaTemplate(),
87
            [
88
                'media'      => $media,
89
                'width'      => $width,
90
                'height'     => $height,
91
                'type'       => $type,
92
                'parameters' => $parameters,
93
            ]
94
        );
95
    }
96
97
    /**
98
     * @param \Twig_Environment $environment
99
     * @param MediaInterface $media
100
     * @param int $width
101
     * @param int $height
102
     * @param string $routeName
103
     * @param array $parameters
104
     * @return string
105
     */
106
    public function mediaImage(
107
        \Twig_Environment $environment,
108
        MediaInterface $media,
109
        $width,
110
        $height,
111
        $routeName = Parameter::ROUTE_NAME_DEFAULT,
112
        array $parameters = []
113
    ) {
114
        $parameters += [
115
            'w' => $width,
116
            'h' => $height,
117
        ];
118
119
        return sprintf(
120
            '<img src="%s" width="%d" height="%d" title="%s">',
121
            $this->parameterHelper->generateUrl($media, $parameters, $routeName),
122
            $width,
123
            $height,
124
            $media->getTitle()
125
        );
126
    }
127
128
    /**
129
     * @param \Twig_Environment $environment
130
     * @param MediaInterface $media
131
     */
132
    public function mediaType(\Twig_Environment $environment, MediaInterface $media)
133
    {
134
        return $this->providerPool->getProvider($media->getProviderName())->getName();
135
    }
136
}
137