MediaExtension::generateURL()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace MediaBundle\Twig;
4
5
use MediaBundle\Model\Media;
6
use Symfony\Component\HttpFoundation\RequestStack;
7
8
class MediaExtension extends \Twig_Extension
9
{
10
11
    /**
12
     *
13
     * @var RequestStack
14
     */
15
    private $requestStack;
16
17
    /**
18
     *
19
     * @var string
20
     */
21
    private $uploadPath;
22
23
    /**
24
     * 
25
     * @param RequestStack $requestStack
26
     * @param string $uploadPath
27
     */
28
    public function __construct(RequestStack $requestStack, $uploadPath)
29
    {
30
        $this->requestStack = $requestStack;
31
        $this->uploadPath = $uploadPath;
32
    }
33
34
    /**
35
     * 
36
     * @return array
37
     */
38
    public function getFunctions()
39
    {
40
        return [
41
            'media' => new \Twig_SimpleFunction('media', [$this, 'generateURL'])
42
        ];
43
    }
44
45
    /**
46
     * 
47
     * @param Media $media
48
     * 
49
     * @return string
50
     */
51
    public function generateURL(Media $media)
52
    {
53
        return sprintf('%s/%s/%s/%s', $this->requestStack->getCurrentRequest()->getBasePath(), $this->uploadPath, $media->getFormat(), $media->getFilename());
54
    }
55
56
}
57