Passed
Push — master ( 09ce70...3c2452 )
by Andrea
32:18 queued 16s
created

AssetExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Twig\Extension;
4
5
use Twig\Extension\AbstractExtension;
6
use Twig\TwigFunction;
7
8
class AssetExtension extends AbstractExtension
9
{
10
    private $projectpath;
11
12 25
    public function __construct($projectpath)
13
    {
14 25
        $this->projectpath = $projectpath;
15 25
    }
16
17 1
    public function getFunctions()
18
    {
19 1
        return [new TwigFunction('asset_exists', [$this, 'assetExists'], ['is_safe' => ['html']])];
20
    }
21
22 1
    public function assetExists($path)
23
    {
24 1
        $publicRoot = realpath($this->projectpath.'/public/').DIRECTORY_SEPARATOR;
25 1
        $toCheck = $publicRoot.$path;
26
27
        // check if the file exists
28 1
        if (!is_file($toCheck)) {
29 1
            return false;
30
        }
31
32
        // check if file is well contained in web/ directory (prevents ../ in paths)
33
        if (0 !== strncmp($publicRoot, $toCheck, strlen($publicRoot))) {
34
            return false;
35
        }
36
37
        return true;
38
    }
39
}
40