TwigLoader::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 11
rs 10
1
<?php namespace Tarsana\Command\Template\Twig;
2
3
use Tarsana\Command\Interfaces\Template\TemplateInterface;
4
use Tarsana\Command\Interfaces\Template\TemplateLoaderInterface;
5
use Tarsana\Command\Template\Twig\TwigTemplate;
6
7
/**
8
 * Twig Templates Loader
9
 */
10
class TwigLoader implements TemplateLoaderInterface {
11
12
    /**
13
     * Twig Environment.
14
     *
15
     * @var \Twig_Environment
16
     */
17
    protected $env;
18
19
    /**
20
     * Initialize the loader.
21
     *
22
     * @param  string $templatesPath
23
     * @param  string $cachePath
24
     * @return self
25
     */
26
    public function init(string $templatesPath, string $cachePath = null) : TemplateLoaderInterface
27
    {
28
        $loader = new \Twig_Loader_Filesystem($templatesPath);
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Loader_Filesystem has been deprecated: since Twig 2.7, use "Twig\Loader\FilesystemLoader" instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

28
        $loader = /** @scrutinizer ignore-deprecated */ new \Twig_Loader_Filesystem($templatesPath);
Loading history...
29
        if (null !== $cachePath) {
30
            $this->env = new \Twig_Environment($loader, [
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Environment has been deprecated: since Twig 2.7, use "Twig\Environment" instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

30
            $this->env = /** @scrutinizer ignore-deprecated */ new \Twig_Environment($loader, [
Loading history...
31
                'cache' => $cachePath,
32
            ]);
33
        } else {
34
            $this->env = new \Twig_Environment($loader);
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Environment has been deprecated: since Twig 2.7, use "Twig\Environment" instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

34
            $this->env = /** @scrutinizer ignore-deprecated */ new \Twig_Environment($loader);
Loading history...
35
        }
36
        return $this;
37
    }
38
39
    /**
40
     * Loads a template.
41
     *
42
     * @param  string $name
43
     * @return Tarsana\Command\Template\Twig\TwigTemplate
0 ignored issues
show
Bug introduced by
The type Tarsana\Command\Template...plate\Twig\TwigTemplate was not found. Did you mean Tarsana\Command\Template\Twig\TwigTemplate? If so, make sure to prefix the type with \.
Loading history...
44
     */
45
    public function load(string $name) : TemplateInterface
46
    {
47
        return new TwigTemplate($this->env->loadTemplate($name));
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Tarsana\Comma...v->loadTemplate($name)) returns the type Tarsana\Command\Template\Twig\TwigTemplate which is incompatible with the documented return type Tarsana\Command\Template...plate\Twig\TwigTemplate.
Loading history...
48
    }
49
50
}
51