Passed
Push — master ( f28aa5...f8cd9d )
by Mike
08:28
created

TwigFactory::getTwigEnvironment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Xervice\Twig;
6
7
8
use Xervice\Core\Factory\AbstractFactory;
9
use Xervice\Twig\Business\Loader\PathInjector;
10
use Xervice\Twig\Business\Loader\PathInjectorInterface;
11
use Xervice\Twig\Business\Loader\XerviceLoader;
12
use Xervice\Twig\Business\Path\PathCollection;
13
14
/**
15
 * @method \Xervice\Twig\TwigConfig getConfig()
16
 */
17
class TwigFactory extends AbstractFactory
18
{
19
    /**
20
     * @var \Twig_Environment
21
     */
22
    private $twigEnvironment;
23
24
    /**
25
     * @return \Twig_Environment
26
     */
27 2
    public function createTwigEnvironment(): \Twig_Environment
28
    {
29 2
        return new \Twig_Environment(
30 2
            $this->createXerviceLoader(),
31
            [
32 2
                'debug'               => $this->getConfig()->isDebug(),
33 2
                'charset'             => $this->getConfig()->getCharset(),
34 2
                'base_template_class' => $this->getConfig()->getBaseTemplateClass(),
35 2
                'strict_variables'    => $this->getConfig()->isStrictVariables(),
36 2
                'autoescape'          => $this->getConfig()->getAutoescape(),
37 2
                'cache'               => $this->getConfig()->isCache() ? $this->getConfig()->getCachePath() : false,
38 2
                'auto_reload'         => $this->getConfig()->isAutoReload(),
39 2
                'optimizations'       => $this->getConfig()->getOptimization()
40
            ]
41
        );
42
    }
43
44
    /**
45
     * @return \Xervice\Twig\Business\Loader\PathInjector
46
     */
47 2
    public function createPathInjector(): PathInjectorInterface
48
    {
49 2
        return new PathInjector(
50 2
            $this->getTwigEnvironment()->getLoader()
51
        );
52
    }
53
54
    /**
55
     * @return \Twig_LoaderInterface
56
     */
57 2
    public function createXerviceLoader(): \Twig_LoaderInterface
58
    {
59 2
        return new XerviceLoader(
60 2
            $this->createTwigFilesystemLoader(),
61 2
            $this->getPathProviderCollection()
62
        );
63
    }
64
65
    /**
66
     * @return \Twig_Loader_Filesystem
67
     */
68 2
    public function createTwigFilesystemLoader(): \Twig_Loader_Filesystem
69
    {
70 2
        return new \Twig_Loader_Filesystem();
71
    }
72
73
    /***
74
     * @return \Twig_Environment
75
     */
76 2
    public function getTwigEnvironment(): \Twig_Environment
77
    {
78 2
        if ($this->twigEnvironment === null) {
79 2
            $this->twigEnvironment = $this->createTwigEnvironment();
80
        }
81
82 2
        return $this->twigEnvironment;
83
    }
84
85
    /**
86
     * @return \Xervice\Twig\Business\Path\PathCollection
87
     */
88 2
    public function getPathProviderCollection(): PathCollection
89
    {
90 2
        return $this->getDependency(TwigDependencyProvider::PATH_PROVIDER_COLLECTION);
91
    }
92
}