Completed
Push — master ( 769748...7b8fb0 )
by Baptiste
12s
created

TwigTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 0
dl 0
loc 22
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A renderString() 0 16 2
1
<?php
2
namespace Behapi\Context;
3
4
use Twig_Environment;
5
6
trait TwigTrait
7
{
8
    /** @var  Twig_Environment */
9
    private $twig;
10
11
    public function renderString(string $string, array $context = []): string
12
    {
13
        if (null === $this->twig) {
14
            return $string;
15
        }
16
17
        $key = sprintf('__behapi_tpl__%s', hash('sha256', $string));
18
19
        // this is assuming that the loader is Twig_Loader_Array
20
        // as this was privately set in the initializer, it should be OK
21
        // to assume that this is still a Twig_Loader_Array
22
        $loader = $this->twig->getLoader();
23
        $loader->setTemplate($key, $string);
24
25
        return $this->twig->load($key)->render($context);
26
    }
27
}
28
29