Completed
Push — master ( 013ef7...4604a2 )
by Radu
07:01
created

Setting::getValidatedKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Environment;
6
7
use WebServCo\Framework\Exceptions\ConfigurationException;
8
9
final class Setting
10
{
11
    /**
12
    * @param mixed $value
13
    */
14
    public static function set(string $key, $value): bool
15
    {
16
        $key = self::getValidatedKey($key);
17
        switch ($key) {
18
            case 'APP_ENVIRONMENT':
19
                Value::validate($value);
20
                break;
21
            default:
22
                break;
23
        }
24
        $_SERVER[$key] = $value;
25
        return true;
26
    }
27
28
    public static function getValidatedKey(string $key): string
29
    {
30
        $key = \strtoupper($key);
31
        if (!\WebServCo\Framework\Utils\Strings::startsWith($key, 'APP_', false)) {
32
            throw new ConfigurationException(\sprintf('Invalid key name: "%s".', $key));
33
        }
34
        return $key;
35
    }
36
}
37