Passed
Push — master ( b45e96...d0c0b5 )
by Andrea
13:59 queued 10s
created

AssetExtension::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Twig\Extension;
4
5
use Symfony\Component\HttpKernel\KernelInterface;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
8
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
9
10
class AssetExtension extends \Twig_Extension
11
{
12
13
    private $kernel;
14
15 14
    public function __construct(KernelInterface $kernel)
16
    {
17 14
        $this->kernel = $kernel;
18 14
    }
19
20
    public function getFunctions()
21
    {
22
        return [new \Twig_SimpleFunction('asset_exists', [$this, 'assetExists'], ['is_safe' => ['html']])];
23
    }
24
25 7
    public function assetExists($path)
26
    {
27 7
        $publicRoot = realpath($this->kernel->getRootDir() . '/../public/') . DIRECTORY_SEPARATOR;
28 7
        $toCheck = realpath($publicRoot . $path);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
29
        
30
        // check if the file exists
31 7
        if (!is_file($toCheck)) {
32 7
            return false;
33
        }
34
35
        // check if file is well contained in web/ directory (prevents ../ in paths)
36
        if (strncmp($publicRoot, $toCheck, strlen($publicRoot)) !== 0) {
37
            return false;
38
        }
39
40
        return true;
41
    }
42
43
    public function getName()
44
    {
45
        return 'asset_exists';
46
    }
47
}
48