Completed
Push — develop ( 043012...3d7377 )
by Vladimir
12s queued 10s
created

TwigStakxBridge::createTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 4
cts 5
cp 0.8
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.032
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Templating\Twig;
9
10
use allejo\stakx\Compiler;
11
use allejo\stakx\Templating\TemplateBridgeInterface;
12
use Psr\Log\LoggerInterface;
13
use Twig\Environment;
14
use Twig\Error\Error;
15
16
class TwigStakxBridge implements TemplateBridgeInterface
17
{
18
    private $twig;
19
    private $logger;
20
21
    /** @var \Twig_Profiler_Profile */
22
    private $profiler;
23
24 79
    public function __construct(Environment $twig)
25
    {
26 79
        $this->twig = $twig;
27 79
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function setLogger(LoggerInterface $logger)
33
    {
34
        $this->logger = $logger;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 12
    public function setGlobalVariable($key, $value)
41
    {
42 12
        $this->twig->addGlobal($key, $value);
43 12
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function clearTemplateCache()
49
    {
50
        $this->twig->clearTemplateCache();
0 ignored issues
show
Deprecated Code introduced by
The method Twig\Environment::clearTemplateCache() has been deprecated with message: since 1.18.3 (to be removed in 2.0)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 12
    public function createTemplate($templateContent)
57
    {
58
        try
59
        {
60 12
            $template = $this->twig->createTemplate($templateContent);
61
        }
62 12
        catch (Error $e)
63
        {
64
            throw new TwigError($e);
65
        }
66
67 12
        return new TwigTemplate($template);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 24 View Code Duplication
    public function getAssortmentDependencies($namespace, $bodyContent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        // To see what this regex should match and what shouldn't be see:
76
        //     tests/allejo/stakx/Test/FrontMatter/FrontMatterDocumentTest.php
77
78 24
        $regex = "/{[{%].*?(?:$namespace)(?:\.|\[['\"])?([^_][^\W]+)?(?:\.|['\"]\])?[^_=]*?[%}]}/";
79 24
        $results = [];
80
81 24
        preg_match_all($regex, $bodyContent, $results);
82
83 24
        return array_unique($results[1]);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 7 View Code Duplication
    public function getTemplateImportDependencies($bodyContent)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91 7
        $regex = "/{%\s?(?:import|from|include)\s?['\"](.+)['\"].+/";
92 7
        $results = [];
93
94 7
        preg_match_all($regex, $bodyContent, $results);
95
96 7
        if (empty($results[1]))
97 7
        {
98
            return [];
99
        }
100
101 7
        return array_unique($results[1]);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function hasProfiler()
108
    {
109
        return $this->profiler !== null;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 48
    public function setProfiler($profiler)
116
    {
117 48
        $this->profiler = $profiler;
118 48
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function getProfilerOutput(Compiler $compiler)
124
    {
125
        $dumper = new TwigTextProfiler();
126
        $dumper->setTemplateMappings($compiler->getTemplateMappings());
127
128
        return $dumper->dump($this->profiler);
129
    }
130
}
131