Passed
Push — feature/178-allow-adding-key-v... ( 9721fa )
by Chema
04:36
created

GacelaConfig::addAppConfigKeyValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 2
dl 0
loc 5
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 Closure;
8
use Gacela\Framework\ClassResolver\Cache\GacelaCache;
9
use Gacela\Framework\Config\ConfigReaderInterface;
10
use Gacela\Framework\Config\GacelaConfigBuilder\ConfigBuilder;
11
use Gacela\Framework\Config\GacelaConfigBuilder\MappingInterfacesBuilder;
12
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder;
13
14
final class GacelaConfig
15
{
16
    private ConfigBuilder $configBuilder;
17
18
    private SuffixTypesBuilder $suffixTypesBuilder;
19
20
    private MappingInterfacesBuilder $mappingInterfacesBuilder;
21
22
    /** @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...
23
    private array $externalServices;
24
25
    private bool $cacheEnabled = true;
26
27
    private string $cacheDirectory = GacelaCache::DEFAULT_DIRECTORY_VALUE;
28
29
    /** @var list<string> */
30
    private array $projectNamespaces = [];
31
32
    /** @var array<string,mixed> */
33
    private array $configKeyValues = [];
34
35
    /**
36
     * @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...
37
     */
38 44
    public function __construct(array $externalServices = [])
39
    {
40 44
        $this->externalServices = $externalServices;
41 44
        $this->configBuilder = new ConfigBuilder();
42 44
        $this->suffixTypesBuilder = new SuffixTypesBuilder();
43 44
        $this->mappingInterfacesBuilder = new MappingInterfacesBuilder();
44
    }
45
46
    /**
47
     * @return Closure(GacelaConfig):void
48
     */
49 7
    public static function withPhpConfigDefault(): callable
50
    {
51 7
        return static function (self $config): void {
52 7
            $config->addAppConfig('config/*.php', 'config/local.php');
53
        };
54
    }
55
56
    /**
57
     * @param string $path define the path where Gacela will read all the config files
58
     * @param string $pathLocal define the path where Gacela will read the local config file
59
     * @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...
60
     */
61 24
    public function addAppConfig(string $path, string $pathLocal = '', $reader = null): self
62
    {
63 24
        $this->configBuilder->add($path, $pathLocal, $reader);
64
65 24
        return $this;
66
    }
67
68 2
    public function addSuffixTypeFacade(string $suffix): self
69
    {
70 2
        $this->suffixTypesBuilder->addFacade($suffix);
71
72 2
        return $this;
73
    }
74
75 2
    public function addSuffixTypeFactory(string $suffix): self
76
    {
77 2
        $this->suffixTypesBuilder->addFactory($suffix);
78
79 2
        return $this;
80
    }
81
82 2
    public function addSuffixTypeConfig(string $suffix): self
83
    {
84 2
        $this->suffixTypesBuilder->addConfig($suffix);
85
86 2
        return $this;
87
    }
88
89 3
    public function addSuffixTypeDependencyProvider(string $suffix): self
90
    {
91 3
        $this->suffixTypesBuilder->addDependencyProvider($suffix);
92
93 3
        return $this;
94
    }
95
96
    /**
97
     * @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...
98
     * @param class-string|object|callable $value
99
     */
100 9
    public function addMappingInterface(string $key, $value): self
101
    {
102 9
        $this->mappingInterfacesBuilder->bind($key, $value);
103
104 9
        return $this;
105
    }
106
107
    /**
108
     * @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...
109
     */
110 4
    public function getExternalService(string $key)
111
    {
112 4
        return $this->externalServices[$key];
113
    }
114
115
    /**
116
     * @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...
117
     */
118 2
    public function addExternalService(string $key, $value): self
119
    {
120 2
        $this->externalServices[$key] = $value;
121
122 2
        return $this;
123
    }
124
125 8
    public function setCacheEnabled(bool $flag): self
126
    {
127 8
        $this->cacheEnabled = $flag;
128
129 8
        return $this;
130
    }
131
132 1
    public function setCacheDirectory(string $dir): self
133
    {
134 1
        $this->cacheDirectory = $dir;
135
136 1
        return $this;
137
    }
138
139
    /**
140
     * @param list<string> $list
141
     */
142 3
    public function setProjectNamespaces(array $list): self
143
    {
144 3
        $this->projectNamespaces = $list;
0 ignored issues
show
Documentation Bug introduced by
It seems like $list of type array is incompatible with the declared type Gacela\Framework\Bootstrap\list of property $projectNamespaces.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
145
146 3
        return $this;
147
    }
148
149
    /**
150
     * @param mixed $value
151
     */
152 3
    public function addAppConfigKeyValue(string $key, $value): self
153
    {
154 3
        $this->configKeyValues[$key] = $value;
155
156 3
        return $this;
157
    }
158
159
    /**
160
     * @return array{
161
     *     external-services:array<string,class-string|object|callable>,
162
     *     config-builder:ConfigBuilder,
163
     *     suffix-types-builder:SuffixTypesBuilder,
164
     *     mapping-interfaces-builder:MappingInterfacesBuilder,
165
     *     cache-enabled:bool,
166
     *     cache-directory:string,
167
     *     project-namespaces:list<string>,
168
     *     config-key-values:array<string,mixed>,
169
     * }
170
     *
171
     * @internal
172
     */
173 44
    public function build(): array
174
    {
175
        return [
176 44
            'external-services' => $this->externalServices,
177 44
            'config-builder' => $this->configBuilder,
178 44
            'suffix-types-builder' => $this->suffixTypesBuilder,
179 44
            'mapping-interfaces-builder' => $this->mappingInterfacesBuilder,
180 44
            'cache-enabled' => $this->cacheEnabled,
181 44
            'cache-directory' => $this->cacheDirectory,
182 44
            'project-namespaces' => $this->projectNamespaces,
183 44
            'config-key-values' => $this->configKeyValues,
184
        ];
185
    }
186
}
187