Completed
Pull Request — 1.x (#19)
by IWASAKI
04:50 queued 02:30
created

TwigModule   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 7
c 6
b 1
f 1
lcom 1
cbo 3
dl 0
loc 68
ccs 28
cts 28
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B configure() 0 27 1
B bindOptionalParameters() 0 10 5
1
<?php
2
3
namespace Madapaja\TwigModule;
4
5
use BEAR\Resource\RenderInterface;
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use Madapaja\TwigModule\Annotation\TwigOptions;
8
use Madapaja\TwigModule\Annotation\TwigPaths;
9
use Ray\Di\AbstractModule;
10
use Ray\Di\Scope;
11
use Twig_Environment;
12
use Twig_Loader_Filesystem;
13
use Twig_LoaderInterface;
14
15
class TwigModule extends AbstractModule
16
{
17
    /**
18
     * @var array
19
     */
20
    private $paths;
21
22
    /**
23
     * @var array
24
     */
25
    private $options;
26
27
    /**
28
     * @param array $paths   Twig template paths
29
     * @param array $options Twig_Environment options
30
     *
31
     * @see http://twig.sensiolabs.org/api/master/Twig_Environment.html
32
     */
33 21
    public function __construct($paths = [], $options = [])
34
    {
35 21
        $this->paths = $paths;
36 21
        $this->options = $options;
37
38 21
        parent::__construct();
39 21
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 21
    protected function configure()
45
    {
46 21
        AnnotationRegistry::registerFile(__DIR__ . '/DoctrineAnnotations.php');
47
48 21
        $this->bind(RenderInterface::class)->to(TwigRenderer::class)->in(Scope::SINGLETON);
49
50
        $this
51 21
            ->bind(Twig_LoaderInterface::class)
52 21
            ->annotatedWith('twig_loader')
53 21
            ->toConstructor(
54 21
                Twig_Loader_Filesystem::class,
55 21
                'paths=Madapaja\TwigModule\Annotation\TwigPaths'
56
            );
57
58
        $this
59 21
            ->bind(Twig_Environment::class)
60 21
            ->annotatedWith('original')
61 21
            ->toProvider(OriginalTwigEnvironmentProvider::class)
62 21
            ->in(Scope::SINGLETON);
63
64
        $this
65 21
            ->bind(Twig_Environment::class)
66 21
            ->toProvider(TwigEnvironmentProvider::class)
67 21
            ->in(Scope::SINGLETON);
68
69 21
        $this->bindOptionalParameters();
70 21
    }
71
72 21
    private function bindOptionalParameters()
73
    {
74 21
        if (is_array($this->paths) && !empty($this->paths)) {
75 13
            $this->bind()->annotatedWith(TwigPaths::class)->toInstance($this->paths);
76
        }
77
78 21
        if (is_array($this->options) && !empty($this->options)) {
79 1
            $this->bind()->annotatedWith(TwigOptions::class)->toInstance($this->options);
80
        }
81 21
    }
82
}
83