Completed
Push — master ( 962636...4acf43 )
by Alex
02:24
created

TwigExtensionSet::apply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Asmaster\EquipTwig\Configuration;
4
5
use Auryn\Injector;
6
use Equip\Structure\Set;
7
use Equip\Configuration\ConfigurationInterface;
8
use Asmaster\EquipTwig\Exception\ExtensionException;
9
use Twig_ExtensionInterface;
10
use Twig_Environment as TwigEnvironment;
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 1
    public function prepareExtension(TwigEnvironment $environment, Injector $injector)
28
    {
29 1
        $extensions = $this->toArray();
30
31 1
        if ($environment->isDebug()) {
32 1
            $extensions[] = TwigExtensionDebug::class;
33 1
        }
34
35 1
        foreach ($extensions as $extension) {
36 1
            if (!is_object($extension)) {
37 1
                $extension = $injector->make($extension);
38 1
            }
39
40 1
            $environment->addExtension($extension);
41 1
        }
42 1
    }
43
44
    /**
45
     * @throws ExtensionException::invalidExtension
46
     */
47 4
    protected function assertValid(array $extensions)
48
    {
49 4
        parent::assertValid($extensions);
50
51 4
        foreach ($extensions as $extension) {
52 3
            if (!is_subclass_of($extension, Twig_ExtensionInterface::class)) {
53 1
                throw ExtensionException::invalidExtension($extension);
54
            }
55 3
        }
56 3
    }
57
}
58