Passed
Push — develop ( bc7c49...ecfe6e )
by Fabian
01:26
created

Config::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cakasim\Payone\Sdk\Config;
6
7
/**
8
 * The ConfigInterface implementation.
9
 *
10
 * @author Fabian Böttcher <[email protected]>
11
 * @since 0.1.0
12
 */
13
class Config implements ConfigInterface
14
{
15
    /**
16
     * The static configuration defaults.
17
     */
18
    protected const DEFAULTS = [
19
        'api.endpoint'      => 'https://api.pay1.de/post-gateway/',
20
        'api.key_hash_type' => 'sha384',
21
        'api.mode'          => 'test',
22
23
        'notification.sender_address_whitelist' => [
24
            '185.60.20.0/24',
25
            '217.70.200.0/24',
26
            '213.178.72.196',
27
            '213.178.72.197',
28
        ],
29
30
        'redirect.token_lifetime'          => 3600,
31
        'redirect.token_encryption_method' => 'aes-256-ctr',
32
        'redirect.token_signing_algo'      => 'sha256',
33
    ];
34
35
    /**
36
     * @var array Stores the config entries.
37
     */
38
    protected $config = [];
39
40
    /**
41
     * Constructs the config with defaults.
42
     */
43
    public function __construct()
44
    {
45
        $this->applyDefaults();
46
    }
47
48
    /**
49
     * Applies the configuration defaults.
50
     */
51
    protected function applyDefaults(): void
52
    {
53
        foreach ($this->getDefaults() as $name => $value) {
54
            $this->set($name, $value);
55
        }
56
    }
57
58
    /**
59
     * Returns the configuration defaults.
60
     *
61
     * @return array The configuration defaults.
62
     */
63
    protected function getDefaults(): array
64
    {
65
        return static::DEFAULTS;
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71
    public function has(string $name): bool
72
    {
73
        return array_key_exists($name, $this->config);
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function get(string $name)
80
    {
81
        if ($this->has($name)) {
82
            return $this->config[$name];
83
        }
84
85
        throw new ConfigException("Failed to read config entry '{$name}', the entry does not exist.");
86
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91
    public function set(string $name, $value): void
92
    {
93
        $this->config[$name] = $value;
94
    }
95
}
96