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

AssetExtension::assetExists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.4746

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 16
ccs 5
cts 8
cp 0.625
crap 3.4746
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