Completed
Push — master ( d78391...2d8f81 )
by Alex
28:22 queued 25:14
created

TwigExtensionSet::prepareExtension()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

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