MediaExtension::getFunctions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Media\Twig;
4
5
use Media\Entity\MediaInterface;
6
use Symfony\Component\HttpFoundation\RequestStack;
7
8
class MediaExtension extends \Twig_Extension
9
{
10
11
    /**
12
     *
13
     * @var RequestStack
14
     */
15
    private $request;
16
    
17
    public function __construct(RequestStack $request)
18
    {
19
        $this->request = $request;
20
    }
21
    
22
    /**
23
     * 
24
     * @return array
25
     */
26
    public function getFunctions()
27
    {
28
        return [
29
            new \Twig_SimpleFunction('media', [$this, 'media'])
30
        ];
31
    }
32
33
    /**
34
     * 
35
     * @param MediaInterface $media
36
     * 
37
     * @return string
38
     */
39
    public function media(MediaInterface $media = null)
40
    {
41
        if (empty($media)) {
42
            return true;
43
        }
44
        return sprintf('%s/uploads/%s/%s', $this->request->getCurrentRequest()->getBasePath(), $media->getContext(), $media->getFileName());
45
    }
46
47
}
48