Passed
Push — feature/change-setup-override-... ( 7b5c3a...d078d0 )
by Chema
03:52
created

GacelaConfig   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 284
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 69
dl 0
loc 284
ccs 72
cts 75
cp 0.96
rs 10
c 3
b 0
f 0
wmc 22

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addAppConfigKeyValue() 0 5 1
A build() 0 15 1
A getExternalService() 0 3 1
A addAppConfig() 0 5 1
A addExternalService() 0 5 1
A addSuffixTypeFacade() 0 5 1
A addMappingInterface() 0 5 1
A setProjectNamespaces() 0 5 1
A setFileCacheEnabled() 0 5 1
A addSuffixTypeFactory() 0 5 1
A withPhpConfigDefault() 0 4 1
A registerGenericListener() 0 8 2
A addAppConfigKeyValues() 0 5 1
A resetInMemoryCache() 0 5 1
A setFileCacheDirectory() 0 5 1
A addSuffixTypeConfig() 0 5 1
A addSuffixTypeDependencyProvider() 0 5 1
A disableEventListeners() 0 5 1
A registerSpecificListener() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Bootstrap;
6
7
use Closure;
8
use Gacela\Framework\Config\ConfigReaderInterface;
9
use Gacela\Framework\Config\GacelaConfigBuilder\ConfigBuilder;
10
use Gacela\Framework\Config\GacelaConfigBuilder\MappingInterfacesBuilder;
11
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder;
12
use Gacela\Framework\Event\GacelaEventInterface;
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 $shouldResetInMemoryCache = null;
26
27
    private ?bool $fileCacheEnabled = null;
28
29
    private ?string $fileCacheDirectory = null;
30
31
    /** @var list<string> */
32
    private ?array $projectNamespaces = null;
33
34
    /** @var array<string,mixed> */
35
    private ?array $configKeyValues = null;
36
37
    private ?bool $areEventListenersEnabled = null;
38
39
    /** @var list<callable> */
40
    private ?array $genericListeners = null;
41
42
    /** @var array<class-string,list<callable>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string,list<callable>> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string,list<callable>>.
Loading history...
43
    private ?array $specificListeners = null;
44
45
    /**
46
     * @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...
47
     */
48 66
    public function __construct(array $externalServices = [])
49
    {
50 66
        $this->externalServices = $externalServices;
51 66
        $this->configBuilder = new ConfigBuilder();
52 66
        $this->suffixTypesBuilder = new SuffixTypesBuilder();
53 66
        $this->mappingInterfacesBuilder = new MappingInterfacesBuilder();
54
    }
55
56
    /**
57
     * Define 'config/*.php' as path, and 'config/local.php' as local path for the configuration.
58
     *
59
     * @return Closure(GacelaConfig):void
60
     */
61 7
    public static function withPhpConfigDefault(): callable
62
    {
63 7
        return static function (self $config): void {
64 7
            $config->addAppConfig('config/*.php', 'config/local.php');
65
        };
66
    }
67
68
    /**
69
     * Define the path where the configuration will be stored.
70
     *
71
     * @param string $path define the path where Gacela will read all the config files
72
     * @param string $pathLocal define the path where Gacela will read the local config file
73
     * @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...
74
     */
75 25
    public function addAppConfig(string $path, string $pathLocal = '', $reader = null): self
76
    {
77 25
        $this->configBuilder->add($path, $pathLocal, $reader);
78
79 25
        return $this;
80
    }
81
82
    /**
83
     * Allow overriding gacela facade suffixes.
84
     */
85 5
    public function addSuffixTypeFacade(string $suffix): self
86
    {
87 5
        $this->suffixTypesBuilder->addFacade($suffix);
88
89 5
        return $this;
90
    }
91
92
    /**
93
     * Allow overriding gacela factory suffixes.
94
     */
95 5
    public function addSuffixTypeFactory(string $suffix): self
96
    {
97 5
        $this->suffixTypesBuilder->addFactory($suffix);
98
99 5
        return $this;
100
    }
101
102
    /**
103
     * Allow overriding gacela config suffixes.
104
     */
105 5
    public function addSuffixTypeConfig(string $suffix): self
106
    {
107 5
        $this->suffixTypesBuilder->addConfig($suffix);
108
109 5
        return $this;
110
    }
111
112
    /**
113
     * Allow overriding gacela dependency provider suffixes.
114
     */
115 6
    public function addSuffixTypeDependencyProvider(string $suffix): self
116
    {
117 6
        $this->suffixTypesBuilder->addDependencyProvider($suffix);
118
119 6
        return $this;
120
    }
121
122
    /**
123
     * Define the mapping between interfaces and concretions, so Gacela services will auto-resolve them automatically.
124
     *
125
     * @param class-string $key
126
     * @param class-string|object|callable $value
127
     */
128 9
    public function addMappingInterface(string $key, $value): self
129
    {
130 9
        $this->mappingInterfacesBuilder->bind($key, $value);
131
132 9
        return $this;
133
    }
134
135
    /**
136
     * Useful to pass services while bootstrapping Gacela to the gacela.php config file.
137
     *
138
     * @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...
139
     */
140 3
    public function addExternalService(string $key, $value): self
141
    {
142 3
        $this->externalServices[$key] = $value;
143
144 3
        return $this;
145
    }
146
147
    /**
148
     * Get an external service from its defined key, previously added using `addExternalService()`.
149
     *
150
     * @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...
151
     */
152 4
    public function getExternalService(string $key)
153
    {
154 4
        return $this->externalServices[$key];
155
    }
156
157
    /**
158
     * Enable resetting the memory cache on each setup. Useful for functional tests.
159
     */
160 17
    public function resetInMemoryCache(): self
161
    {
162 17
        $this->shouldResetInMemoryCache = true;
163
164 17
        return $this;
165
    }
166
167
    /**
168
     * Define whether the file cache flag is enabled.
169
     */
170 16
    public function setFileCacheEnabled(bool $flag): self
171
    {
172 16
        $this->fileCacheEnabled = $flag;
173
174 16
        return $this;
175
    }
176
177
    /**
178
     * Define the file cache directory.
179
     */
180 4
    public function setFileCacheDirectory(string $dir): self
181
    {
182 4
        $this->fileCacheDirectory = $dir;
183
184 4
        return $this;
185
    }
186
187
    /**
188
     * Define a list of project namespaces.
189
     *
190
     * @param list<string> $list
191
     */
192 4
    public function setProjectNamespaces(array $list): self
193
    {
194 4
        $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...
195
196 4
        return $this;
197
    }
198
199
    /**
200
     * Add/replace an existent configuration key with a specific value.
201
     *
202
     * @param mixed $value
203
     */
204 2
    public function addAppConfigKeyValue(string $key, $value): self
205
    {
206 2
        $this->configKeyValues[$key] = $value;
207
208 2
        return $this;
209
    }
210
211
    /**
212
     * Add/replace a list of existent configuration keys with a specific value.
213
     *
214
     * @param array<string, mixed> $config
215
     */
216 2
    public function addAppConfigKeyValues(array $config): self
217
    {
218 2
        $this->configKeyValues = array_merge($this->configKeyValues ?? [], $config);
219
220 2
        return $this;
221
    }
222
223
    /**
224
     * Do not dispatch any event in the application.
225
     */
226
    public function disableEventListeners(): self
227
    {
228
        $this->areEventListenersEnabled = false;
229
230
        return $this;
231
    }
232
233
    /**
234
     * Register a generic listener when any event happens.
235
     * The callable argument must be the type `GacelaEventInterface`.
236
     *
237
     * @param callable(GacelaEventInterface):void $listener
238
     */
239 4
    public function registerGenericListener(callable $listener): self
240
    {
241 4
        if ($this->genericListeners === null) {
242 4
            $this->genericListeners = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type Gacela\Framework\Bootstrap\list of property $genericListeners.

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...
243
        }
244 4
        $this->genericListeners[] = $listener;
245
246 4
        return $this;
247
    }
248
249
    /**
250
     * Register a listener when some event happens.
251
     *
252
     * @param class-string $event
253
     * @param callable(GacelaEventInterface):void $listener
254
     */
255 7
    public function registerSpecificListener(string $event, callable $listener): self
256
    {
257 7
        if ($this->specificListeners === null) {
258 7
            $this->specificListeners = [];
259
        }
260 7
        $this->specificListeners[$event][] = $listener;
261
262 7
        return $this;
263
    }
264
265
    /**
266
     * @return array{
267
     *     external-services: ?array<string,class-string|object|callable>,
268
     *     config-builder: ConfigBuilder,
269
     *     suffix-types-builder: SuffixTypesBuilder,
270
     *     mapping-interfaces-builder: MappingInterfacesBuilder,
271
     *     should-reset-in-memory-cache: ?bool,
272
     *     file-cache-enabled: ?bool,
273
     *     file-cache-directory: ?string,
274
     *     project-namespaces: ?list<string>,
275
     *     config-key-values: ?array<string,mixed>,
276
     *     are-event-listeners-enabled: ?bool,
277
     *     generic-listeners: ?list<callable>,
278
     *     specific-listeners: ?array<class-string,list<callable>>,
279
     * }
280
     *
281
     * @internal
282
     */
283 65
    public function build(): array
284
    {
285
        return [
286 65
            'external-services' => $this->externalServices,
287 65
            'config-builder' => $this->configBuilder,
288 65
            'suffix-types-builder' => $this->suffixTypesBuilder,
289 65
            'mapping-interfaces-builder' => $this->mappingInterfacesBuilder,
290 65
            'should-reset-in-memory-cache' => $this->shouldResetInMemoryCache,
291 65
            'file-cache-enabled' => $this->fileCacheEnabled,
292 65
            'file-cache-directory' => $this->fileCacheDirectory,
293 65
            'project-namespaces' => $this->projectNamespaces,
294 65
            'config-key-values' => $this->configKeyValues,
295 65
            'are-event-listeners-enabled' => $this->areEventListenersEnabled,
296 65
            'generic-listeners' => $this->genericListeners,
297 65
            'specific-listeners' => $this->specificListeners,
298
        ];
299
    }
300
}
301