TwigExtensionSet::prepareExtension()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 6
nop 2
crap 4
1
<?php
2
declare(strict_types=1);
3
4
namespace AlexMasterov\EquipTwig\Configuration;
5
6
use AlexMasterov\EquipTwig\Exception\ExtensionException;
7
use Auryn\Injector;
8
use Equip\Configuration\ConfigurationInterface;
9
use Equip\Structure\Set;
10
use Twig_Environment;
11
use Twig_ExtensionInterface;
12
use Twig_Extension_Debug;
13
14
class TwigExtensionSet extends Set implements ConfigurationInterface
15
{
16 1
    public function apply(Injector $injector)
17
    {
18 1
        $injector->prepare(Twig_Environment::class, [$this, 'prepareExtension']);
19 1
    }
20
21 1
    public function prepareExtension(
22
        Twig_Environment $environment,
23
        Injector $injector
24
    ) {
25 1
        $extensions = $this->toArray();
26
27 1
        if ($environment->isDebug()) {
28 1
            $extensions[] = Twig_Extension_Debug::class;
29
        }
30
31 1
        foreach ($extensions as $extension) {
32 1
            if (!is_object($extension)) {
33 1
                $extension = $injector->make($extension);
34
            }
35 1
            $environment->addExtension($extension);
36
        }
37 1
    }
38
39
    /**
40
     * @inheritDoc
41
     *
42
     * @throws ExtensionException
43
     *  If $classes does not implement the correct interface.
44
     */
45 2
    protected function assertValid(array $classes)
46
    {
47 2
        parent::assertValid($classes);
48
49 2
        foreach ($classes as $extension) {
50 1
            if (!is_subclass_of($extension, Twig_ExtensionInterface::class)) {
51 1
                throw ExtensionException::invalidClass($extension);
52
            }
53
        }
54 1
    }
55
}
56