Completed
Push — master ( 61e1ff...22d949 )
by
unknown
03:51
created

MediaExtension::getFilters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 23
cp 0
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
crap 2
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Twig\Extension;
4
5
use MediaMonks\SonataMediaBundle\Generator\UrlGenerator;
6
use MediaMonks\SonataMediaBundle\Provider\ProviderInterface;
7
use MediaMonks\SonataMediaBundle\Provider\ProviderPool;
8
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
9
10
class MediaExtension extends \Twig_Extension
11
{
12
    /**
13
     * @var ProviderPool
14
     */
15
    private $providerPool;
16
17
    /**
18
     * @var UrlGenerator
19
     */
20
    private $urlGenerator;
21
22
    /**
23
     * @param ProviderPool $providerPool
24
     * @param UrlGenerator $urlGenerator
25
     */
26
    public function __construct(ProviderPool $providerPool, UrlGenerator $urlGenerator)
27
    {
28
        $this->providerPool = $providerPool;
29
        $this->urlGenerator = $urlGenerator;
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public function getName()
36
    {
37
        return 'media';
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function getFilters()
44
    {
45
        return [
46
            new \Twig_SimpleFilter(
47
                'media', [$this, 'media'], [
48
                    'needs_environment' => true,
49
                    'is_safe'           => ['html'],
50
                ]
51
            ),
52
            new \Twig_SimpleFilter(
53
                'media_image', [$this, 'mediaImage'], [
54
                    'needs_environment' => true,
55
                    'is_safe'           => ['html'],
56
                ]
57
            ),
58
            new \Twig_SimpleFilter(
59
                'media_download', [$this, 'mediaDownload'], [
60
                    'needs_environment' => true,
61
                    'is_safe'           => ['html'],
62
                ]
63
            ),
64
        ];
65
    }
66
67
    /**
68
     * @param \Twig_Environment $environment
69
     * @param MediaInterface $media
70
     * @param $width
71
     * @param $height
72
     * @param array $parameters
73
     * @return string
74
     */
75
    public function media(
76
        \Twig_Environment $environment,
77
        MediaInterface $media,
78
        $width,
79
        $height,
80
        array $parameters = []
81
    ) {
82
        return $environment->render(
83
            $this->getProviderByMedia($media)->getMediaTemplate(),
84
            [
85
                'media'      => $media,
86
                'width'      => $width,
87
                'height'     => $height,
88
                'parameters' => $parameters,
89
            ]
90
        );
91
    }
92
93
    /**
94
     * @param \Twig_Environment $environment
95
     * @param MediaInterface $media
96
     * @param $width
97
     * @param $height
98
     * @param array $parameters
99
     * @param null $routeName
100
     * @return string
101
     */
102 View Code Duplication
    public function mediaImage(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
        \Twig_Environment $environment,
104
        MediaInterface $media,
105
        $width,
106
        $height,
107
        array $parameters = [],
108
        $routeName = null
109
    ) {
110
        $parameters += [
111
            'w' => $width,
112
            'h' => $height,
113
        ];
114
115
        return $environment->render(
116
            'MediaMonksSonataMediaBundle:Image:image.html.twig',
117
            [
118
                'src'    => $this->urlGenerator->generate($media, $parameters, $routeName),
119
                'width'  => $width,
120
                'height' => $height,
121
                'title'  => $media->getTitle(),
122
            ]
123
        );
124
    }
125
126
    /**
127
     * @param \Twig_Environment $environment
128
     * @param MediaInterface $media
129
     * @param $width
130
     * @param $height
131
     * @param array $parameters
132
     * @param null $routeName
133
     * @return string
134
     */
135 View Code Duplication
    public function mediaDownload(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
        \Twig_Environment $environment,
137
        MediaInterface $media,
138
        $width,
139
        $height,
140
        array $parameters = [],
141
        $routeName = null
142
    ) {
143
        $parameters += [
144
            'w' => $width,
145
            'h' => $height,
146
        ];
147
148
        return $environment->render(
149
            'MediaMonksSonataMediaBundle:Image:file.html.twig',
150
            [
151
                'src'    => $this->urlGenerator->generate($media, $parameters, $routeName),
152
                'width'  => $width,
153
                'height' => $height,
154
                'title'  => $media->getTitle(),
155
            ]
156
        );
157
    }
158
159
    /**
160
     * @param MediaInterface $media
161
     * @return ProviderInterface
162
     */
163
    private function getProviderByMedia(MediaInterface $media)
164
    {
165
        return $this->providerPool->getProvider($media->getProvider());
166
    }
167
}
168