ConfigDAO   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 49
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A findConfig() 0 22 4
A getConfigPath() 0 4 1
A loadConfig() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the Clover to Html package.
5
 *
6
 * (c) Stéphane Demonchaux <[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
namespace CloverToHtml;
12
13
class ConfigDAO
14
{
15
    /**
16
     * @param string $templatePath
17
     * @param string $key
18
     * @throws \InvalidArgumentException
19
     * @return array
20
     */
21
    public function findConfig($templatePath, $key = null): array
22
    {
23
        if (is_file($this->getConfigPath($templatePath)) === false) {
24
            throw new \InvalidArgumentException(
25
                sprintf('Template "%s" with key "%s" not found', $templatePath, $key)
26
            );
27
        }
28
29
        if ($key === null) {
30
            return [];
31
        }
32
33
        $config = $this->loadConfig($templatePath);
34
35
        if (isset($config[$key])) {
36
            return $config[$key];
37
        }
38
39
        throw new \InvalidArgumentException(
40
            sprintf('Template "%s" with key "%s" not found', $templatePath, $key)
41
        );
42
    }
43
44
    /**
45
     * @param string $templatePath
46
     * @return string
47
     */
48
    private function getConfigPath($templatePath): string
49
    {
50
        return $templatePath.'/config.json';
51
    }
52
53
    /**
54
     * @param string $templatePath
55
     * @return array
56
     */
57
    private function loadConfig($templatePath): array
58
    {
59
        return json_decode(file_get_contents($this->getConfigPath($templatePath)), true);
60
    }
61
}
62