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

TwigExtensionSet   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 1 Features 3
Metric Value
wmc 8
c 7
b 1
f 3
lcom 0
cbo 4
dl 0
loc 45
ccs 22
cts 22
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 4 1
A prepareExtension() 0 16 4
A assertValid() 0 10 3
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