Passed
Push — master ( 8635a1...042cb2 )
by Enjoys
06:25 queued 04:49
created

Parse::getOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Config;
6
7
use Enjoys\Config\ValueHandler\DefinedConstantsValueHandler;
8
use Enjoys\Config\ValueHandler\EnvValueHandler;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
11
12
abstract class Parse implements ParseInterface
13
{
14
    /**
15
     * @var array<string, mixed>
16
     */
17
    private array $options = [];
18
19
    private ?string $configSource = null;
20
    protected LoggerInterface $logger;
21
22
    private array $valueHandlers = [
23
        EnvValueHandler::class,
24
        DefinedConstantsValueHandler::class
25
    ];
26
27 23
    public function __construct()
28
    {
29 23
        $this->logger = new NullLogger();
30
    }
31
32 15
    public function setLogger(LoggerInterface $logger)
33
    {
34 15
        $this->logger = $logger;
35
    }
36
37
38
    /**
39
     * @param array<string, mixed> $options
40
     * @return $this
41
     * @psalm-suppress MixedAssignment
42
     */
43 12
    public function setOptions(array $options = []): self
44
    {
45 12
        foreach ($options as $key => $value) {
46 1
            $this->setOption($key, $value);
47
        }
48 12
        return $this;
49
    }
50
51
    /**
52
     * @param mixed $value
53
     */
54 2
    public function setOption(string $key, $value): self
55
    {
56 2
        $this->options[$key] = $value;
57 2
        return $this;
58
    }
59
60
    /**
61
     * @param mixed $defaults
62
     * @return mixed
63
     */
64 22
    public function getOption(string $key, $defaults = null)
65
    {
66 22
        if (array_key_exists($key, $this->options)) {
67 2
            return $this->options[$key];
68
        }
69 21
        return $defaults;
70
    }
71
72
    /**
73
     * @return array<string, mixed>
74
     */
75
    public function getOptions(): array
76
    {
77
        return $this->options;
78
    }
79
80
81 22
    public function addConfigSource(string $source): void
82
    {
83 22
        $this->configSource = $source;
84
    }
85
86
    /**
87
     * @return array|false|null
88
     */
89 23
    public function parse()
90
    {
91 23
        if (is_null($this->configSource)) {
92 1
            $this->logger->notice('Add data for parsing');
93 1
            return null;
94
        }
95
96 22
        if (!is_file($this->configSource)) {
97 17
            return $this->parseString($this->applyValueHandlers($this->configSource));
98
        }
99
100 5
        return $this->parseFile($this->configSource);
101
    }
102
103 22
    private function applyValueHandlers(string $data): string
104
    {
105
        /** @var class-string<ValueHandlerInterface> $valueHandler */
106 22
        foreach ($this->valueHandlers as $valueHandler) {
107 22
            $data = (new $valueHandler())->handle($data);
108
        }
109
        //  var_dump($data);
110 22
        return $data;
111
    }
112
113
    /**
114
     * @param string $filename
115
     * @return array|null|false
116
     */
117 5
    private function parseFile(string $filename)
118
    {
119 5
        $data = file_get_contents($filename);
120 5
        return $this->parseString($this->applyValueHandlers($data));
121
    }
122
123
    /**
124
     * @param string $input
125
     * @return array|null|false
126
     */
127
    abstract protected function parseString(string $input);
128
129
130
}
131