Config::check()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nymfonya\Component;
6
7
use Nymfonya\Component\Interfaces\IConfig;
8
9
/**
10
 * Nymfonya\Component\Config
11
 *
12
 * is a config manager
13
 *
14
 * @author pierrefromager
15
 */
16
17
class Config implements IConfig
18
{
19
20
    protected $path;
21
    protected $env;
22
    protected $settings;
23
24
    /**
25
     * instanciate
26
     *
27
     * @param string $env
28
     */
29 10
    public function __construct(string $env, string $path)
30
    {
31 10
        $this->path = $path;
32 10
        $this->setEnv($env);
33 10
        $this->load();
34 10
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
35
    }
36
37
    /**
38
     * set config environment
39
     *
40
     * @param string $env
41
     * @return Config
42
     */
43 10
    public function setEnv(string $env = self::ENV_DEV): Config
44
    {
45 10
        $this->env = $env;
46 10
        return $this;
47
    }
48
49
    /**
50
     * returns env
51
     *
52
     * @return string
53
     */
54 1
    public function getEnv(): string
55
    {
56 1
        return $this->env;
57
    }
58
59
    /**
60
     * set config path
61
     *
62
     * @param string $path
63
     * @return Config
64
     */
65 2
    public function setPath(string $path): Config
66
    {
67 2
        $this->path = $path;
68 2
        return $this;
69
    }
70
71
    /**
72
     * returns config path
73
     *
74
     * @return string
75
     */
76 1
    public function getPath(): string
77
    {
78 1
        return $this->path;
79
    }
80
81
    /**
82
     * return config array for a main entry key
83
     *
84
     * @param string $key
85
     * @return array
86
     */
87 1
    public function getSettings(string $key = ''): array
88
    {
89 1
        return ($key) ? $this->settings[$key] : $this->settings;
90
    }
91
92
    /**
93
     * return true if config main entry for a key exists
94
     *
95
     * @param string $key
96
     * @return boolean
97
     */
98 1
    public function hasEntry(string $key): bool
99
    {
100 1
        return isset($this->settings[$key]);
101
    }
102
103
    /**
104
     * load config for a given env
105
     *
106
     * @return Config
107
     */
108 10
    public function load(): Config
109
    {
110 10
        $filename = realpath($this->getFilename());
111 10
        if (false === $this->check($filename)) {
112
            throw new \Exception(
113
                sprintf(
114
                    self::CONFIG_ERROR_MISSING . '%s on %s',
115
                    $this->env,
116
                    $this->path
117
                )
118
            );
119
        }
120 10
        $this->settings = require $this->getFilename();
121 10
        return $this;
122
    }
123
124
    /**
125
     * getFilename
126
     *
127
     * @return string
128
     */
129 1
    protected function getFilename(): string
130
    {
131 1
        return $this->path . $this->env . '.php';
132
    }
133
134
    /**
135
     * check
136
     *
137
     * @param string $filename
138
     * @return boolean
139
     */
140 1
    protected function check($filename): bool
0 ignored issues
show
Coding Style introduced by
function check() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
141
    {
142 1
        return (in_array($this->env, $this->getAllowedEnv())
143 1
            && file_exists($filename));
144
    }
145
146
    /**
147
     * getAllowedEnv
148
     *
149
     * @return array
150
     */
151 1
    protected function getAllowedEnv(): array
152
    {
153
        return [
154 1
            self::ENV_DEV, self::ENV_INT, self::ENV_PROD,
155 1
            self::ENV_TEST, self::ENV_CLI
156
        ];
157
    }
158
}
159