Completed
Push — master ( 8b9114...86ab3d )
by Alex
07:01 queued 04:44
created

TwigExtensionSet::assertValid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 3.0416
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
    public function apply(Injector $injector)
17
    {
18
        $injector->prepare(Twig_Environment::class, [$this, 'prepareExtension']);
19
    }
20
21
    public function prepareExtension(
22
        Twig_Environment $environment,
23
        Injector $injector
24
    ) {
25
        $extensions = $this->toArray();
26
27
        if ($environment->isDebug()) {
28
            $extensions[] = Twig_Extension_Debug::class;
29
        }
30
31
        foreach ($extensions as $extension) {
32
            if (!is_object($extension)) {
33
                $extension = $injector->make($extension);
34
            }
35
            $environment->addExtension($extension);
36
        }
37
    }
38
39
    /**
40
     * @inheritDoc
41
     *
42
     * @throws ExtensionException
43
     *  If $classes does not implement the correct interface.
44
     */
45 1
    protected function assertValid(array $classes)
46
    {
47 1
        parent::assertValid($classes);
48
49 1
        foreach ($classes as $extension) {
50 1
            if (!is_subclass_of($extension, Twig_ExtensionInterface::class)) {
51 1
                throw ExtensionException::invalidClass($extension);
52
            }
53
        }
54
    }
55
}
56