TemplateLoader::load()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
rs 10
ccs 9
cts 9
cp 1
crap 3
1
<?php
2
3
/*
4
 *  This file is part of the Micro framework package.
5
 *
6
 *  (c) Stanislau Komar <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace Micro\Plugin\Twig\Business\Loader;
13
14
use Micro\Plugin\Twig\Plugin\TwigTemplatePluginInterface;
15
use Twig\Environment;
16
use Twig\Error\LoaderError;
17
use Twig\Loader\FilesystemLoader;
18
19
class TemplateLoader implements LoaderInterface
20
{
21
    /**
22
     * @throws LoaderError
23
     */
24 2
    public function load(Environment $environment, object $plugin): void
25
    {
26 2
        if (!($plugin instanceof TwigTemplatePluginInterface)) {
27 2
            return;
28
        }
29
30
        /** @var FilesystemLoader $loader */
31 2
        $loader = $environment->getLoader();
32 2
        if (!($loader instanceof FilesystemLoader)) {
0 ignored issues
show
introduced by
$loader is always a sub-type of Twig\Loader\FilesystemLoader.
Loading history...
33 1
            throw new \InvalidArgumentException(sprintf('The loader supports only %s', FilesystemLoader::class));
34
        }
35
36 1
        $namespace = $plugin->getTwigNamespace();
37 1
        $paths = $plugin->getTwigTemplatePaths();
38
39 1
        $this->registerTemplates($loader, $paths, $namespace);
40
    }
41
42
    /**
43
     * @param string[] $paths
44
     *
45
     * @throws LoaderError
46
     */
47 1
    protected function registerTemplates(FilesystemLoader $loader, array $paths, string $namespace = null): void
48
    {
49 1
        if (null === $namespace) {
50 1
            $namespace = FilesystemLoader::MAIN_NAMESPACE;
51
        }
52
53 1
        foreach ($paths as $path) {
54 1
            $loader->addPath($path, $namespace);
55
        }
56
    }
57
}
58