Passed
Push — master ( 94e2db...d4af17 )
by Melech
07:30 queued 03:33
created

DataConfig::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Config;
15
16
use ArrayAccess;
17
use RuntimeException;
18
use Valkyrja\Application\Env;
19
20
use function defined;
21
22
/**
23
 * Abstract Class Config.
24
 *
25
 * @author Melech Mizrachi
26
 *
27
 * @implements ArrayAccess<string, mixed>
28
 */
29
abstract class DataConfig implements ArrayAccess
30
{
31
    /**
32
     * The model properties env keys.
33
     *
34
     * @var array<string, string>
35
     */
36
    protected static array $envKeys = [];
37
38
    /**
39
     * Create config from Env.
40
     *
41
     * @param class-string<Env> $env The env
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Env> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Env>.
Loading history...
42
     */
43
    public static function fromEnv(string $env): static
44
    {
45
        $new = new static();
46
47
        $new->setPropertiesBeforeSettingFromEnv();
48
        $new->setPropertiesFromEnv($env);
49
        $new->setPropertiesAfterSettingFromEnv();
50
51
        return $new;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function offsetExists($offset): bool
58
    {
59
        /** @var string $offset */
60
        return isset($this->$offset);
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function offsetGet($offset): mixed
67
    {
68
        /** @var string $offset */
69
        return $this->$offset;
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function offsetSet($offset, $value): void
76
    {
77
        /** @var string $offset */
78
        $this->$offset = $value;
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    public function offsetUnset(mixed $offset): void
85
    {
86
        throw new RuntimeException("Cannot remove offset with name $offset from config.");
87
    }
88
89
    /**
90
     * Set properties from env.
91
     *
92
     * @param class-string<Env> $env The env
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Env> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Env>.
Loading history...
93
     */
94
    protected function setPropertiesFromEnv(string $env): void
95
    {
96
        foreach (static::$envKeys as $property => $value) {
97
            if (defined("$env::$value")) {
98
                $this->$property = constant("$env::$value") ?? $this->$property;
99
            }
100
        }
101
    }
102
103
    /**
104
     * Set properties' values before setting from env.
105
     */
106
    protected function setPropertiesBeforeSettingFromEnv(): void
107
    {
108
    }
109
110
    /**
111
     * Set properties' values after setting from env.
112
     */
113
    protected function setPropertiesAfterSettingFromEnv(): void
114
    {
115
    }
116
}
117