Completed
Push — master ( f63aaf...b18312 )
by Alex
05:51
created

TwigExtensionSet   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 1 Features 3
Metric Value
wmc 8
c 11
b 1
f 3
lcom 0
cbo 4
dl 0
loc 49
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 4 1
A prepareExtension() 0 15 4
A assertValid() 0 10 3
1
<?php
2
3
namespace AlexMasterov\EquipTwig\Configuration;
4
5
use AlexMasterov\EquipTwig\Exception\ExtensionException;
6
use Auryn\Injector;
7
use Equip\Configuration\ConfigurationInterface;
8
use Equip\Structure\Set;
9
use Twig_Environment;
10
use Twig_ExtensionInterface;
11
use Twig_Extension_Debug;
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(Twig_Environment::class, [$this, 'prepareExtension']);
21 1
    }
22
23
    /**
24
     * @param Twig_Environment  $environment
25
     * @param Injector          $injector
26
     *
27
     * @return void
28
     */
29 1
    public function prepareExtension(Twig_Environment $environment, Injector $injector)
30
    {
31 1
        $extensions = $this->toArray();
32
33 1
        if ($environment->isDebug()) {
34 1
            $extensions[] = Twig_Extension_Debug::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, Twig_ExtensionInterface::class)) {
57 2
                throw ExtensionException::invalidClass($extension);
58
            }
59
        }
60 3
    }
61
}
62