Passed
Push — bugfix/support-windows ( 0e99e7...b5ecf8 )
by Jesús
06:33 queued 02:55
created

AppConfigBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 41
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 3 1
A add() 0 7 1
A normalizeReader() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Config\GacelaConfigBuilder;
6
7
use Gacela\Framework\Config\ConfigReader\PhpConfigReader;
8
use Gacela\Framework\Config\ConfigReaderInterface;
9
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigItem;
10
11
use function is_string;
12
13
final class AppConfigBuilder
14
{
15
    /** @var list<GacelaConfigItem> */
16
    private array $configItems = [];
17
18
    /**
19
     * @param string $path define the path where Gacela will read all the config files
20
     * @param string $pathLocal define the path where Gacela will read the local config file
21
     * @param class-string<ConfigReaderInterface>|ConfigReaderInterface|null $reader Define the reader class which will read and parse the config files
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ConfigReade...figReaderInterface|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ConfigReaderInterface>|ConfigReaderInterface|null.
Loading history...
22
     */
23 25
    public function add(string $path, string $pathLocal = '', string|ConfigReaderInterface $reader = null): self
24
    {
25 25
        $readerInstance = $this->normalizeReader($reader);
26
27 25
        $this->configItems[] = new GacelaConfigItem($path, $pathLocal, $readerInstance);
28
29 25
        return $this;
30
    }
31
32
    /**
33
     * @return list<GacelaConfigItem>
34
     */
35 69
    public function build(): array
36
    {
37 69
        return $this->configItems;
38
    }
39
40
    /**
41
     * @param class-string<ConfigReaderInterface>|ConfigReaderInterface|null $reader
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ConfigReade...figReaderInterface|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ConfigReaderInterface>|ConfigReaderInterface|null.
Loading history...
42
     */
43 25
    private function normalizeReader(string|ConfigReaderInterface|null $reader): ConfigReaderInterface
44
    {
45 25
        if ($reader instanceof ConfigReaderInterface) {
46 1
            return $reader;
47
        }
48
49 24
        if (is_string($reader)) {
50 2
            return new $reader();
51
        }
52
53 23
        return new PhpConfigReader();
54
    }
55
}
56