Completed
Branch erdiko2 (81b309)
by John
02:13
created

Config   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 27.87 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 17
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getContext() 0 4 1
A setContext() 0 4 1
A get() 0 11 1
A getConfigFile() 17 17 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Erdiko Config
4
 *
5
 * Read from json configs
6
 *
7
 * @package     erdiko
8
 * @copyright   2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com
9
 * @author      John Arroyo <[email protected]>
10
 */
11
namespace erdiko;
12
13
14
class Config
15
{
16
    /**
17
     *
18
     */
19
    public static function getContext(): string
20
    {
21
        return getenv('ERDIKO_CONTEXT') ?? 'default';
22
    }
23
24
    /**
25
     *
26
     */
27
    public static function setContext(string $context)
28
    {
29
        putenv("ERDIKO_CONTEXT={$context}");
30
    }
31
32
    /**
33
     * Get configuration
34
     *
35
     * @param string $name
36
     * @param string $context
37
     * @return array $config
38
     */
39
    public static function get(string $name = 'application', string $context = null): array
40
    {
41
        $context = $context ?? static::getContext();
42
43
        // @note if we remove ERDIKO_ROOT it will be more portable
44
        $folder = ERDIKO_ROOT;
45
        $subfolder = "/app/config";
46
        $filename = "{$folder}{$subfolder}/{$context}/{$name}.json";
47
48
        return static::getConfigFile($filename);
49
    }
50
51
    /**
52
     * Read JSON config file and return array
53
     *
54
     * @param string $file
55
     * @return array $config
56
     */
57 View Code Duplication
    public static function getConfigFile($file): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $file = addslashes($file);
60
        if (is_file($file)) {
61
            $data = str_replace("\\", "\\\\", file_get_contents($file));
62
            $json = json_decode($data, true);
63
64
            if (empty($json)) {
65
                throw new \Exception("Config file has a json parse error, $file");
66
            }
67
68
        } else {
69
            throw new \Exception("Config file not found, $file");
70
        }
71
        
72
        return $json;
73
    }
74
}