Completed
Push — master ( 74371a...6da8c0 )
by Vladimir
02:21
created

AbstractFilesystemTwigExtension   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 0
cbo 5
dl 0
loc 26
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 19 3
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Templating\Twig\Extension;
9
10
use allejo\stakx\Service;
11
use allejo\stakx\Filesystem\FilesystemLoader as fs;
12
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
13
use Twig_Environment;
14
15
abstract class AbstractFilesystemTwigExtension extends AbstractTwigExtension
16
{
17
    protected $globs;
18
    protected $dir;
19
    protected $path;
20
21
    public function __invoke(Twig_Environment $env, $location)
22
    {
23
        $this->globs = $env->getGlobals();
24
        $this->dir = fs::getFolderPath($this->globs['__currentTemplate']);
25
        $this->path = fs::appendPath($this->dir, $location);
26
27
        if (is_file($this->path))
28
        {
29
            $this->path = realpath($this->path);
30
        }
31
32
        if (strpos($this->path, Service::getWorkingDirectory()) !== 0)
33
        {
34
            throw new FileNotFoundException(sprintf(
35
                "The '%s' file could not be found or is outside the website working directory",
36
                $location
37
            ));
38
        }
39
    }
40
}
41