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

AssetExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 53.33%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 36
ccs 8
cts 15
cp 0.5333
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A assetExists() 0 16 3
A getName() 0 3 1
A getFunctions() 0 3 1
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