Passed
Branch new-architecture (2184ad)
by James
02:32
created

WebHelperRepository   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 2
cbo 4
dl 0
loc 110
ccs 41
cts 41
cp 1
rs 10

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 7
    public function __construct($resDir)
42
    {
43 7
        $this->finder = new Finder();
44 7
        $this->versionParser = new VersionParser();
45 7
        if ($resDir !== '') {
46 6
            $this->memoize = $this->memoize($resDir);
47 6
            $this->twig = $this->initialize($resDir);            
48 6
        }
49 7
    }
50
51
    /**
52
     * Initialize the Twig Environment.
53
     *
54
     * @param  string           $resDir the Path of a Directives Repository
55
     * @return Twig_Environment         the Twig Environment
56
     */
57 6
    private function initialize($resDir)
58
    {
59
        try {
60 6
            $loader = new Twig_Loader_Filesystem($resDir);
61 6
        } catch (\Twig_Error_Loader $e) {
62 2
            return null;
63
        }
64
65 4
        $twig = new Twig_Environment($loader, array(
66 4
            'cache' => __DIR__ . '/../var/cache',
67 4
        ));
68
69 4
        return $twig;
70
    }
71
72
    /**
73
     * Initialize the structured array of a directives repository.
74
     *
75
     * @param  string $resDir the Path of a Directives Repository
76
     * @return array          the structured array of a directives repository
77
     */
78 6
    private function memoize($resDir)
79
    {
80
        try {
81 6
            $this->finder->files()->name('*.twig')->in($resDir);
82 6
        } catch (\InvalidArgumentException $e) {
83 2
            return [];
84
        }
85
86 4
        $memoize = [];
87 4
        foreach ($this->finder as $file) {
88 4
            $parsedPath = explode('/', $file->getRelativePathname());
89 4
            if (count($parsedPath) == 2) {
90 4
                $parsedPath[2] = $parsedPath[1];
91 4
                $parsedPath[1] = 0;
92 4
            }
93 4
            $parsedPath[2] = str_replace('.twig', '', $parsedPath[2]);
94 4
            $memoize[$parsedPath[0]]
95 4
                [$this->versionParser->normalize($parsedPath[1])]
96 4
                [$parsedPath[2]] = $file->getRelativePathname();
97 4
        }
98
99 4
        return $memoize;
100
    }
101
102
    /**
103
     * Gets the structured array of a directives repository.
104
     *
105
     * @return array the structured array of a directives repository
106
     */
107 2
    public function getMemoize()
108
    {
109 2
        return $this->memoize;
110
    }
111
112
    /**
113
     * Gets the Twig Environment.
114
     *
115
     * @return Twig_Environment the Twig Environment
116
     */
117 2
    public function getTwig()
118 1
    {
119 2
        return $this->twig;
120 1
    }
121
122
    /**
123
     * Tells if the Repository can be used.
124
     *
125
     * @return boolean true if there are some directives in the Path of a Directives Repository
126
     */
127 4
    public function okGo()
128
    {
129 4
        return !empty($this->memoize) && is_a($this->twig, 'Twig_Environment');
130
    }
131
}
132