ConfigDAO::findConfig()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 13
cp 0
rs 9.568
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 20
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