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

Config   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 16
c 1
b 0
f 0
dl 0
loc 57
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 6 2
A bool() 0 7 2
A string() 0 7 2
A int() 0 7 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 Config
10
{
11
    /**
12
    * Get an environment configuration value.
13
    *
14
    * Key existence and value type are validated.
15
    */
16
    public static function bool(string $key): bool
17
    {
18
        $value = self::key($key);
19
        if (!\is_bool($value)) {
20
            throw new ConfigurationException(\sprintf('Value type for key "%s" is not valid', $key));
21
        }
22
        return $value;
23
    }
24
25
    /**
26
    * Get an environment configuration value.
27
    *
28
    * Key existence is validated.
29
    *
30
    * @return mixed
31
    */
32
    public static function key(string $key)
33
    {
34
        if (!\array_key_exists($key, $_SERVER)) {
35
            throw new ConfigurationException(\sprintf('Key not found: "%s".', $key));
36
        }
37
        return $_SERVER[$key];
38
    }
39
40
    /**
41
    * Get an environment configuration value.
42
    *
43
    * Key existence and value type are validated.
44
    */
45
    public static function int(string $key): int
46
    {
47
        $value = self::key($key);
48
        if (!\is_int($value)) {
49
            throw new ConfigurationException(\sprintf('Value type for key "%s" is not valid', $key));
50
        }
51
        return $value;
52
    }
53
54
    /**
55
    * Get an environment configuration value.
56
    *
57
    * Key existence and value type are validated.
58
    */
59
    public static function string(string $key): string
60
    {
61
        $value = self::key($key);
62
        if (!\is_string($value)) {
63
            throw new ConfigurationException(\sprintf('Value type for key "%s" is not valid', $key));
64
        }
65
        return $value;
66
    }
67
}
68