WebHelperRepository   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 4
dl 0
loc 112
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A initialize() 0 14 2
B memoize() 0 23 4
A getMemoize() 0 4 1
A getTwig() 0 4 1
A okGo() 0 4 2
1
<?php
2
3
/**
4
 * This file is, guess what, part of WebHelper.
5
 *
6
 * (c) James <[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 JamesRezo\WebHelper;
13
14
use Symfony\Component\Finder\Finder;
15
use Composer\Semver\VersionParser;
16
use Twig_Loader_Filesystem;
17
use Twig_Environment;
18
19
/**
20
 * WebHelper Repository.
21
 */
22
class WebHelperRepository
23
{
24
    /** @var Finder a Finder instance */
25
    private $finder;
26
27
    /** @var VersionParser a VersionParser instance */
28
    private $versionParser;
29
30
    /** @var array a structured array of a directives repository */
31
    private $memoize = [];
32
33
    /** @var Twig_Environment a Twig_Environment instance */
34
    private $twig = null;
35
36
    /**
37
     * Base constructor.
38
     *
39
     * @param string $resDir the Path of a Directives Repository
40
     */
41 10
    public function __construct($resDir)
42
    {
43 10
        $this->finder = new Finder();
44 10
        $this->versionParser = new VersionParser();
45 10
        if ($resDir !== '') {
46 9
            $this->memoize = $this->memoize($resDir);
47 9
            $this->twig = $this->initialize($resDir);
48 9
        }
49 10
    }
50
51
    /**
52
     * Initialize the Twig Environment.
53
     *
54
     * @param string $resDir the Path of a Directives Repository
55
     *
56
     * @return Twig_Environment the Twig Environment
57
     */
58 9
    private function initialize($resDir)
59
    {
60
        try {
61 9
            $loader = new Twig_Loader_Filesystem($resDir);
62 9
        } catch (\Twig_Error_Loader $e) {
63 2
            return null;
64
        }
65
66 7
        $twig = new Twig_Environment($loader, array(
67 7
            'cache' => __DIR__.'/../var/cache',
68 7
        ));
69
70 7
        return $twig;
71
    }
72
73
    /**
74
     * Initialize the structured array of a directives repository.
75
     *
76
     * @param string $resDir the Path of a Directives Repository
77
     *
78
     * @return array the structured array of a directives repository
79
     */
80 9
    private function memoize($resDir)
81
    {
82
        try {
83 9
            $this->finder->files()->name('*.twig')->in($resDir);
84 9
        } catch (\InvalidArgumentException $e) {
85 2
            return [];
86
        }
87
88 7
        $memoize = [];
89 7
        foreach ($this->finder as $file) {
90 7
            $parsedPath = explode('/', $file->getRelativePathname());
91 7
            if (count($parsedPath) == 2) {
92 7
                $parsedPath[2] = $parsedPath[1];
93 7
                $parsedPath[1] = 0;
94 7
            }
95 7
            $parsedPath[2] = str_replace('.twig', '', $parsedPath[2]);
96 7
            $memoize[$parsedPath[0]]
97 7
                [$this->versionParser->normalize($parsedPath[1])]
98 7
                [$parsedPath[2]] = $file->getRelativePathname();
99 7
        }
100
101 7
        return $memoize;
102
    }
103
104
    /**
105
     * Gets the structured array of a directives repository.
106
     *
107
     * @return array the structured array of a directives repository
108
     */
109 4
    public function getMemoize()
110
    {
111 4
        return $this->memoize;
112
    }
113
114
    /**
115
     * Gets the Twig Environment.
116
     *
117
     * @return Twig_Environment the Twig Environment
118
     */
119 4
    public function getTwig()
120 1
    {
121 4
        return $this->twig;
122
    }
123
124
    /**
125
     * Tells if the Repository can be used.
126
     *
127
     * @return bool true if there are some directives in the Path of a Directives Repository
128
     */
129 6
    public function okGo()
130
    {
131 6
        return !empty($this->memoize) && is_a($this->twig, 'Twig_Environment');
132
    }
133
}
134