Passed
Pull Request — master (#2)
by Enjoys
10:21
created

Parse   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 30
c 3
b 0
f 0
dl 0
loc 116
ccs 20
cts 20
cp 1
rs 10
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getOption() 0 6 2
A parse() 0 12 3
A setOption() 0 4 1
A setOptions() 0 6 2
A addConfigSource() 0 3 1
A setLogger() 0 3 1
A __construct() 0 3 1
A applyValueHandlers() 0 8 2
A parseFile() 0 4 1
A getOptions() 0 3 1
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
    public function __construct()
28
    {
29 23
        $this->logger = new NullLogger();
30
    }
31 23
32
    public function setLogger(LoggerInterface $logger)
33
    {
34 15
        $this->logger = $logger;
35
    }
36 15
37
38
    /**
39 22
     * @param array<string, mixed> $options
40
     * @return $this
41 22
     * @psalm-suppress MixedAssignment
42
     */
43
    public function setOptions(array $options = []): self
44
    {
45
        foreach ($options as $key => $value) {
46
            $this->setOption($key, $value);
47 23
        }
48
        return $this;
49 23
    }
50 1
51 1
    /**
52
     * @param mixed $value
53
     */
54 22
    public function setOption(string $key, $value): self
55 17
    {
56
        $this->options[$key] = $value;
57
        return $this;
58 5
    }
59
60
    /**
61 22
     * @param mixed $defaults
62
     * @return mixed
63
     */
64 22
    public function getOption(string $key, $defaults = null)
65 22
    {
66
        if (array_key_exists($key, $this->options)) {
67
            return $this->options[$key];
68 22
        }
69
        return $defaults;
70
    }
71
72
    /**
73
     * @return array<string, mixed>
74
     */
75 5
    public function getOptions(): array
76
    {
77 5
        return $this->options;
78 5
    }
79
80
81
    public function addConfigSource(string $source): void
82
    {
83
        $this->configSource = $source;
84
    }
85
86
    /**
87
     * @return array|false|null
88
     */
89
    public function parse()
90
    {
91
        if (is_null($this->configSource)) {
92
            $this->logger->notice('Add data for parsing');
93
            return null;
94
        }
95
96
        if (!is_file($this->configSource)) {
97
            return $this->parseString($this->applyValueHandlers($this->configSource));
98
        }
99
100
        return $this->parseFile($this->configSource);
101
    }
102
103
    private function applyValueHandlers(string $data): string
104
    {
105
        /** @var class-string<ValueHandlerInterface> $valueHandler */
106
        foreach ($this->valueHandlers as $valueHandler) {
107
            $data = (new $valueHandler())->handle($data);
108
        }
109
        //  var_dump($data);
110
        return $data;
111
    }
112
113
    /**
114
     * @param string $filename
115
     * @return array|null|false
116
     */
117
    private function parseFile(string $filename)
118
    {
119
        $data = file_get_contents($filename);
120
        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