Completed
Push — master ( 26278b...61e1ff )
by
unknown
04:27
created

MediaExtension::mediaDownload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 23
loc 23
ccs 0
cts 22
cp 0
rs 9.0856
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\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
            new \Twig_SimpleFilter(
65
                'media_type', [$this, 'mediaType']
66
            ),
67
        ];
68
    }
69
70
    /**
71
     * @param \Twig_Environment $environment
72
     * @param MediaInterface $media
73
     * @param $width
74
     * @param $height
75
     * @param array $parameters
76
     * @return string
77
     */
78
    public function media(
79
        \Twig_Environment $environment,
80
        MediaInterface $media,
81
        $width,
82
        $height,
83
        array $parameters = []
84
    ) {
85
        return $environment->render(
86
            $this->getProviderByMedia($media)->getMediaTemplate(),
87
            [
88
                'media'      => $media,
89
                'width'      => $width,
90
                'height'     => $height,
91
                'parameters' => $parameters,
92
            ]
93
        );
94
    }
95
96
    /**
97
     * @param \Twig_Environment $environment
98
     * @param MediaInterface $media
99
     * @param $width
100
     * @param $height
101
     * @param array $parameters
102
     * @param null $routeName
103
     * @return string
104
     */
105 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...
106
        \Twig_Environment $environment,
107
        MediaInterface $media,
108
        $width,
109
        $height,
110
        array $parameters = [],
111
        $routeName = null
112
    ) {
113
        $parameters += [
114
            'w' => $width,
115
            'h' => $height,
116
        ];
117
118
        return $environment->render(
119
            'MediaMonksSonataMediaBundle:Image:image.html.twig',
120
            [
121
                'src'    => $this->urlGenerator->generate($media, $parameters, $routeName),
122
                'width'  => $width,
123
                'height' => $height,
124
                'title'  => $media->getTitle(),
125
            ]
126
        );
127
    }
128
129
    /**
130
     * @param \Twig_Environment $environment
131
     * @param MediaInterface $media
132
     * @param $width
133
     * @param $height
134
     * @param array $parameters
135
     * @param null $routeName
136
     * @return string
137
     */
138 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...
139
        \Twig_Environment $environment,
140
        MediaInterface $media,
141
        $width,
142
        $height,
143
        array $parameters = [],
144
        $routeName = null
145
    ) {
146
        $parameters += [
147
            'w' => $width,
148
            'h' => $height,
149
        ];
150
151
        return $environment->render(
152
            'MediaMonksSonataMediaBundle:Image:file.html.twig',
153
            [
154
                'src'    => $this->urlGenerator->generate($media, $parameters, $routeName),
155
                'width'  => $width,
156
                'height' => $height,
157
                'title'  => $media->getTitle(),
158
            ]
159
        );
160
    }
161
162
    /**
163
     * @param MediaInterface $media
164
     * @return string
165
     */
166
    public function mediaType(MediaInterface $media)
167
    {
168
        return $this->getProviderByMedia($media)->getName();
169
    }
170
171
    /**
172
     * @param MediaInterface $media
173
     * @return ProviderInterface
174
     */
175
    private function getProviderByMedia(MediaInterface $media)
176
    {
177
        return $this->providerPool->getProvider($media->getProviderName());
178
    }
179
}
180