Passed
Push — bugfix/allow-gacela-anon-class... ( 98e2e1 )
by Chema
09:04
created

GacelaConfig::withDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Bootstrap;
6
7
use Gacela\Framework\Config\ConfigReaderInterface;
8
use Gacela\Framework\Config\GacelaConfigBuilder\ConfigBuilder;
9
use Gacela\Framework\Config\GacelaConfigBuilder\MappingInterfacesBuilder;
10
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder;
11
12
final class GacelaConfig
13
{
14
    private ConfigBuilder $configBuilder;
15
16
    private SuffixTypesBuilder $suffixTypesBuilder;
17
18
    private MappingInterfacesBuilder $mappingInterfacesBuilder;
19
20
    /** @var array<string,class-string|object|callable> */
1 ignored issue
show
Documentation Bug introduced by
The doc comment array<string,class-string|object|callable> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string,class-string|object|callable>.
Loading history...
21
    private array $externalServices;
22
23
    /**
24
     * @param array<string,class-string|object|callable> $externalServices
1 ignored issue
show
Documentation Bug introduced by
The doc comment array<string,class-string|object|callable> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string,class-string|object|callable>.
Loading history...
25
     */
26 15
    public function __construct(array $externalServices = [])
27
    {
28 15
        $this->externalServices = $externalServices;
29 15
        $this->configBuilder = new ConfigBuilder();
30 15
        $this->suffixTypesBuilder = new SuffixTypesBuilder();
31 15
        $this->mappingInterfacesBuilder = new MappingInterfacesBuilder();
32
    }
33
34
    /**
35
     * @return callable(GacelaConfig):void
36
     */
37 1
    public static function withDefaults(): callable
38
    {
39 1
        return static function (self $config): void {
40 1
            $config->addAppConfig('config/*.php');
41
        };
42
    }
43
44
    /**
45
     * @param string $path define the path where Gacela will read all the config files
46
     * @param string $pathLocal define the path where Gacela will read the local config file
47
     * @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...
48
     */
49 6
    public function addAppConfig(string $path, string $pathLocal = '', $reader = null): self
50
    {
51 6
        $this->configBuilder->add($path, $pathLocal, $reader);
52
53 6
        return $this;
54
    }
55
56 2
    public function addSuffixTypeFacade(string $suffix): self
57
    {
58 2
        $this->suffixTypesBuilder->addFacade($suffix);
59
60 2
        return $this;
61
    }
62
63 2
    public function addSuffixTypeFactory(string $suffix): self
64
    {
65 2
        $this->suffixTypesBuilder->addFactory($suffix);
66
67 2
        return $this;
68
    }
69
70 2
    public function addSuffixTypeConfig(string $suffix): self
71
    {
72 2
        $this->suffixTypesBuilder->addConfig($suffix);
73
74 2
        return $this;
75
    }
76
77 3
    public function addSuffixTypeDependencyProvider(string $suffix): self
78
    {
79 3
        $this->suffixTypesBuilder->addDependencyProvider($suffix);
80
81 3
        return $this;
82
    }
83
84
    /**
85
     * @param class-string $key
1 ignored issue
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
86
     * @param class-string|object|callable $value
87
     */
88 9
    public function addMappingInterface(string $key, $value): self
89
    {
90 9
        $this->mappingInterfacesBuilder->bind($key, $value);
91
92 9
        return $this;
93
    }
94
95
    /**
96
     * @return class-string|object|callable
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|object|callable at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|object|callable.
Loading history...
97
     */
98 4
    public function getExternalService(string $key)
99
    {
100 4
        return $this->externalServices[$key];
101
    }
102
103
    /**
104
     * @param class-string|object|callable $value
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|object|callable at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|object|callable.
Loading history...
105
     */
106 2
    public function addExternalService(string $key, $value): self
107
    {
108 2
        $this->externalServices[$key] = $value;
109
110 2
        return $this;
111
    }
112
113
    /**
114
     * @internal
115
     *
116
     * @return array{
117
     *     external-services:array<string,class-string|object|callable>,
118
     *     config-builder:ConfigBuilder,
119
     *     suffix-types-builder:SuffixTypesBuilder,
120
     *     mapping-interfaces-builder:MappingInterfacesBuilder,
121
     * }
122
     */
123 15
    public function build(): array
124
    {
125
        return [
126 15
            'external-services' => $this->externalServices,
127 15
            'config-builder' => $this->configBuilder,
128 15
            'suffix-types-builder' => $this->suffixTypesBuilder,
129 15
            'mapping-interfaces-builder' => $this->mappingInterfacesBuilder,
130
        ];
131
    }
132
}
133