Completed
Push — master ( 434e76...71c234 )
by Demonchaux
02:02
created

ConfigDAO   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
B 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)
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 $config;
0 ignored issues
show
Bug introduced by
The variable $config seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
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)
49
    {
50
        return $templatePath.'/config.json';
51
    }
52
53
    /**
54
     * @param string $templatePath
55
     * @return array
56
     */
57
    private function loadConfig($templatePath)
58
    {
59
        return json_decode(file_get_contents($this->getConfigPath($templatePath)), true);
60
    }
61
}
62