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

Setting   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 14
c 0
b 0
f 0
dl 0
loc 26
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidatedKey() 0 7 2
A set() 0 12 2
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