TwigTemplateEngine   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 29
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 5 1
A sourceEngine() 0 3 1
A process() 0 9 2
A __construct() 0 2 1
1
<?php
2
3
/**
4
 * This file is part of template
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\Template\Engine;
13
14
use Slick\Template\Exception\MissingParsedTemplate;
15
use Slick\Template\TemplateEngineInterface;
16
use Twig\Environment;
17
use Twig\TemplateWrapper;
18
19
/**
20
 * TwigTemplateEngine
21
 *
22
 * @package Slick\Template\Engine
23
 */
24
final class TwigTemplateEngine implements TemplateEngineInterface
25
{
26
    private ?TemplateWrapper $template = null;
27
28
    public function __construct(private readonly Environment $twigEnvironment)
29
    {
30
    }
31
32
    public function parse(string $source): TemplateEngineInterface
33
    {
34
        $engine = clone $this;
35
        $engine->template = $this->twigEnvironment->load($source);
36
        return $engine;
37
    }
38
39
    public function process(array $data = array()): string
40
    {
41
        if (null === $this->template) {
42
            throw new MissingParsedTemplate(
43
                "You need to set a template before using the process() method. ".
44
                "Use TemplateEngine::parse() first."
45
            );
46
        }
47
        return $this->template->render($data);
48
    }
49
50
    public function sourceEngine(): Environment
51
    {
52
        return $this->twigEnvironment;
53
    }
54
}
55