Passed
Push — feature/container-extend ( b81097...93c762 )
by Chema
03:26
created

SetupGacela::getServicesToExtend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Bootstrap;
6
7
use Closure;
8
use Gacela\Framework\ClassResolver\Cache\GacelaFileCache;
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\Dispatcher\ConfigurableEventDispatcher;
13
use Gacela\Framework\Event\Dispatcher\EventDispatcherInterface;
14
use Gacela\Framework\Event\Dispatcher\NullEventDispatcher;
15
use RuntimeException;
16
17
use function is_callable;
18
19
final class SetupGacela extends AbstractSetupGacela
20
{
21
    private const DEFAULT_ARE_EVENT_LISTENERS_ENABLED = true;
22
    private const DEFAULT_SHOULD_RESET_IN_MEMORY_CACHE = false;
23
    private const DEFAULT_FILE_CACHE_ENABLED = GacelaFileCache::DEFAULT_ENABLED_VALUE;
24
    private const DEFAULT_FILE_CACHE_DIRECTORY = GacelaFileCache::DEFAULT_DIRECTORY_VALUE;
25
    private const DEFAULT_PROJECT_NAMESPACES = [];
26
    private const DEFAULT_CONFIG_KEY_VALUES = [];
27
    private const DEFAULT_GENERIC_LISTENERS = [];
28
    private const DEFAULT_SPECIFIC_LISTENERS = [];
29
    private const DEFAULT_SERVICES_TO_EXTEND = [];
30
31
    /** @var callable(ConfigBuilder):void */
32
    private $configFn;
33
34
    /** @var callable(MappingInterfacesBuilder,array<string,mixed>):void */
35
    private $mappingInterfacesFn;
36
37
    /** @var callable(SuffixTypesBuilder):void */
38
    private $suffixTypesFn;
39
40
    /** @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...
41
    private ?array $externalServices = null;
42
43
    private ?ConfigBuilder $configBuilder = null;
44
45
    private ?SuffixTypesBuilder $suffixTypesBuilder = null;
46
47
    private ?MappingInterfacesBuilder $mappingInterfacesBuilder = null;
48
49
    private ?bool $shouldResetInMemoryCache = null;
50
51
    private ?bool $fileCacheEnabled = null;
52
53
    private ?string $fileCacheDirectory = null;
54
55
    /** @var ?list<string> */
56
    private ?array $projectNamespaces = null;
57
58
    /** @var ?array<string,mixed> */
59
    private ?array $configKeyValues = null;
60
61
    private ?bool $areEventListenersEnabled = null;
62
63
    /** @var ?list<callable> */
64
    private ?array $genericListeners = null;
65
66
    /** @var ?array<class-string,list<callable>> */
1 ignored issue
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...
67
    private ?array $specificListeners = null;
68
69
    private ?EventDispatcherInterface $eventDispatcher = null;
70
71
    /** @var ?array<string,bool> */
72
    private ?array $changedProperties = null;
73
74
    /** @var ?array<string,list<Closure>> */
1 ignored issue
show
Documentation Bug introduced by
The doc comment ?array<string,list<Closure>> at position 4 could not be parsed: Expected '>' at position 4, but found 'list'.
Loading history...
75
    private ?array $servicesToExtend = null;
76
77 102
    public function __construct()
78
    {
79 102
        $this->configFn = static function (): void {
80
        };
81 102
        $this->mappingInterfacesFn = static function (): void {
82
        };
83 102
        $this->suffixTypesFn = static function (): void {
84
        };
85
    }
86
87 10
    public static function fromFile(string $gacelaFilePath): self
88
    {
89 10
        if (!is_file($gacelaFilePath)) {
90
            throw new RuntimeException("Invalid file path: '{$gacelaFilePath}'");
91
        }
92
93
        /** @var callable(GacelaConfig):void|null $setupGacelaFileFn */
94 10
        $setupGacelaFileFn = include $gacelaFilePath;
95 10
        if (!is_callable($setupGacelaFileFn)) {
96
            return new self();
97
        }
98
99 10
        return self::fromCallable($setupGacelaFileFn);
100
    }
101
102
    /**
103
     * @param callable(GacelaConfig):void $setupGacelaFileFn
104
     */
105 55
    public static function fromCallable(callable $setupGacelaFileFn): self
106
    {
107 55
        $gacelaConfig = new GacelaConfig();
108 55
        $setupGacelaFileFn($gacelaConfig);
109
110 55
        return self::fromGacelaConfig($gacelaConfig);
111
    }
112
113 71
    public static function fromGacelaConfig(GacelaConfig $gacelaConfig): self
114
    {
115 71
        $build = $gacelaConfig->build();
116
117 71
        return (new self())
118 71
            ->setExternalServices($build['external-services'])
119 71
            ->setConfigBuilder($build['config-builder'])
120 71
            ->setSuffixTypesBuilder($build['suffix-types-builder'])
121 71
            ->setMappingInterfacesBuilder($build['mapping-interfaces-builder'])
122 71
            ->setShouldResetInMemoryCache($build['should-reset-in-memory-cache'])
123 71
            ->setFileCacheEnabled($build['file-cache-enabled'])
124 71
            ->setFileCacheDirectory($build['file-cache-directory'])
125 71
            ->setProjectNamespaces($build['project-namespaces'])
126 71
            ->setConfigKeyValues($build['config-key-values'])
127 71
            ->setAreEventListenersEnabled($build['are-event-listeners-enabled'])
128 71
            ->setGenericListeners($build['generic-listeners'])
129 71
            ->setSpecificListeners($build['specific-listeners'])
130 71
            ->setServicesToExtend($build['services-to-extend']);
131
    }
132
133 71
    public function setMappingInterfacesBuilder(MappingInterfacesBuilder $builder): self
134
    {
135 71
        $this->markPropertyChanged('mappingInterfacesBuilder', true);
136 71
        $this->mappingInterfacesBuilder = $builder;
137
138 71
        return $this;
139
    }
140
141 71
    public function setSuffixTypesBuilder(SuffixTypesBuilder $builder): self
142
    {
143 71
        $this->markPropertyChanged('suffixTypesBuilder', true);
144 71
        $this->suffixTypesBuilder = $builder;
145
146 71
        return $this;
147
    }
148
149 71
    public function setConfigBuilder(ConfigBuilder $builder): self
150
    {
151 71
        $this->markPropertyChanged('configBuilder', true);
152 71
        $this->configBuilder = $builder;
153
154 71
        return $this;
155
    }
156
157
    /**
158
     * @param callable(ConfigBuilder):void $callable
159
     */
160 3
    public function setConfigFn(callable $callable): self
161
    {
162 3
        $this->markPropertyChanged('configFn', true);
163 3
        $this->configFn = $callable;
164
165 3
        return $this;
166
    }
167
168 94
    public function buildConfig(ConfigBuilder $builder): ConfigBuilder
169
    {
170 94
        if ($this->configBuilder) {
171 63
            $builder = $this->configBuilder;
172
        }
173
174 94
        ($this->configFn)($builder);
175
176 94
        return $builder;
177
    }
178
179
    /**
180
     * @param callable(MappingInterfacesBuilder,array<string,mixed>):void $callable
181
     */
182 3
    public function setMappingInterfacesFn(callable $callable): self
183
    {
184 3
        $this->markPropertyChanged('mappingInterfacesFn', true);
185 3
        $this->mappingInterfacesFn = $callable;
186
187 3
        return $this;
188
    }
189
190
    /**
191
     * Define the mapping between interfaces and concretions, so Gacela services will auto-resolve them automatically.
192
     *
193
     * @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...
194
     */
195 94
    public function buildMappingInterfaces(
196
        MappingInterfacesBuilder $builder,
197
        array $externalServices
198
    ): MappingInterfacesBuilder {
199 94
        if ($this->mappingInterfacesBuilder) {
200 63
            $builder = $this->mappingInterfacesBuilder;
201
        }
202
203 94
        ($this->mappingInterfacesFn)(
204
            $builder,
205 94
            array_merge($this->externalServices ?? [], $externalServices)
206
        );
207
208 94
        return $builder;
209
    }
210
211
    /**
212
     * @param callable(SuffixTypesBuilder):void $callable
213
     */
214 3
    public function setSuffixTypesFn(callable $callable): self
215
    {
216 3
        $this->markPropertyChanged('suffixTypesFn', true);
217 3
        $this->suffixTypesFn = $callable;
218
219 3
        return $this;
220
    }
221
222
    /**
223
     * Allow overriding gacela resolvable types.
224
     */
225 94
    public function buildSuffixTypes(SuffixTypesBuilder $builder): SuffixTypesBuilder
226
    {
227 94
        if ($this->suffixTypesBuilder) {
228 63
            $builder = $this->suffixTypesBuilder;
229
        }
230
231 94
        ($this->suffixTypesFn)($builder);
232
233 94
        return $builder;
234
    }
235
236
    /**
237
     * @param ?array<string,class-string|object|callable> $array
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...
238
     */
239 74
    public function setExternalServices(?array $array): self
240
    {
241 74
        $this->markPropertyChanged('externalServices', $array);
242 74
        $this->externalServices = $array;
243
244 74
        return $this;
245
    }
246
247
    /**
248
     * @return 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...
249
     */
250 25
    public function externalServices(): array
251
    {
252 25
        return (array)$this->externalServices;
253
    }
254
255 71
    public function setShouldResetInMemoryCache(?bool $flag): self
256
    {
257 71
        $this->markPropertyChanged('shouldResetInMemoryCache', $flag);
258 71
        $this->shouldResetInMemoryCache = $flag ?? self::DEFAULT_SHOULD_RESET_IN_MEMORY_CACHE;
259
260 71
        return $this;
261
    }
262
263 80
    public function shouldResetInMemoryCache(): bool
264
    {
265 80
        return (bool)$this->shouldResetInMemoryCache;
266
    }
267
268 23
    public function isFileCacheEnabled(): bool
269
    {
270 23
        return (bool)$this->fileCacheEnabled;
271
    }
272
273 6
    public function getFileCacheDirectory(): string
274
    {
275 6
        return (string)$this->fileCacheDirectory;
276
    }
277
278 71
    public function setFileCacheDirectory(?string $dir): self
279
    {
280 71
        $this->markPropertyChanged('fileCacheDirectory', $dir);
281 71
        $this->fileCacheDirectory = $dir ?? self::DEFAULT_FILE_CACHE_DIRECTORY;
282
283 71
        return $this;
284
    }
285
286
    /**
287
     * @param ?list<string> $list
288
     */
289 71
    public function setProjectNamespaces(?array $list): self
290
    {
291 71
        $this->markPropertyChanged('projectNamespaces', $list);
292 71
        $this->projectNamespaces = $list ?? self::DEFAULT_PROJECT_NAMESPACES;
1 ignored issue
show
Documentation Bug introduced by
It seems like $list ?? self::DEFAULT_PROJECT_NAMESPACES of type array is incompatible with the declared type Gacela\Framework\Bootstrap\list|null 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...
293
294 71
        return $this;
295
    }
296
297
    /**
298
     * @return list<string>
299
     */
300 23
    public function getProjectNamespaces(): array
301
    {
302 23
        return (array)$this->projectNamespaces;
303
    }
304
305
    /**
306
     * @return array<string,mixed>
307
     */
308 80
    public function getConfigKeyValues(): array
309
    {
310 80
        return (array)$this->configKeyValues;
311
    }
312
313 32
    public function getEventDispatcher(): EventDispatcherInterface
314
    {
315 32
        if ($this->eventDispatcher !== null) {
316 7
            return $this->eventDispatcher;
317
        }
318
319 29
        if ($this->canCreateEventDispatcher()) {
320 11
            $this->eventDispatcher = new ConfigurableEventDispatcher();
321 11
            $this->eventDispatcher->registerGenericListeners($this->genericListeners ?? []);
1 ignored issue
show
Bug introduced by
The method registerGenericListeners() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

321
            $this->eventDispatcher->/** @scrutinizer ignore-call */ 
322
                                    registerGenericListeners($this->genericListeners ?? []);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
322
323 11
            foreach ($this->specificListeners ?? [] as $event => $listeners) {
324 5
                foreach ($listeners as $callable) {
325 5
                    $this->eventDispatcher->registerSpecificListener($event, $callable);
326
                }
327
            }
328
        } else {
329 18
            $this->eventDispatcher = new NullEventDispatcher();
330
        }
331
332 29
        return $this->eventDispatcher;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->eventDispatcher returns the type null which is incompatible with the type-hinted return Gacela\Framework\Event\D...ventDispatcherInterface.
Loading history...
333
    }
334
335 25
    public function combine(self $other): self
336
    {
337 25
        $this->overrideResetInMemoryCache($other);
338 25
        $this->overrideFileCacheSettings($other);
339
340 25
        $this->combineExternalServices($other);
341 25
        $this->combineProjectNamespaces($other);
342 25
        $this->combineConfigKeyValues($other);
343 25
        $this->combineEventDispatcher($other);
344
345 25
        return $this;
346
    }
347
348
    /**
349
     * @return array<string,list<Closure>>
1 ignored issue
show
Documentation Bug introduced by
The doc comment array<string,list<Closure>> at position 4 could not be parsed: Expected '>' at position 4, but found 'list'.
Loading history...
350
     */
351 10
    public function getServicesToExtend(): array
352
    {
353 10
        return (array)$this->servicesToExtend;
354
    }
355
356 71
    private function setFileCacheEnabled(?bool $flag): self
357
    {
358 71
        $this->markPropertyChanged('fileCacheEnabled', $flag);
359 71
        $this->fileCacheEnabled = $flag ?? self::DEFAULT_FILE_CACHE_ENABLED;
360
361 71
        return $this;
362
    }
363
364 71
    private function setAreEventListenersEnabled(?bool $flag): self
365
    {
366 71
        $this->markPropertyChanged('areEventListenersEnabled', $flag);
367 71
        $this->areEventListenersEnabled = $flag ?? self::DEFAULT_ARE_EVENT_LISTENERS_ENABLED;
368
369 71
        return $this;
370
    }
371
372 25
    private function combineExternalServices(self $other): void
373
    {
374 25
        if ($other->isPropertyChanged('externalServices')) {
375 25
            $this->externalServices = array_merge($this->externalServices ?? [], $other->externalServices());
376
        }
377
    }
378
379 25
    private function overrideResetInMemoryCache(self $other): void
380
    {
381 25
        if ($other->isPropertyChanged('shouldResetInMemoryCache')) {
382 1
            $this->shouldResetInMemoryCache = $other->shouldResetInMemoryCache();
383
        }
384
    }
385
386 25
    private function overrideFileCacheSettings(self $other): void
387
    {
388 25
        if ($other->isPropertyChanged('fileCacheEnabled')) {
389 1
            $this->fileCacheEnabled = $other->isFileCacheEnabled();
390
        }
391 25
        if ($other->isPropertyChanged('fileCacheDirectory')) {
392 1
            $this->fileCacheDirectory = $other->getFileCacheDirectory();
393
        }
394
    }
395
396 25
    private function combineProjectNamespaces(self $other): void
397
    {
398 25
        if ($other->isPropertyChanged('projectNamespaces')) {
399 1
            $this->projectNamespaces = array_merge($this->projectNamespaces ?? [], $other->getProjectNamespaces());
1 ignored issue
show
Documentation Bug introduced by
It seems like array_merge($this->proje...getProjectNamespaces()) of type array is incompatible with the declared type Gacela\Framework\Bootstrap\list|null 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...
400
        }
401
    }
402
403 25
    private function combineConfigKeyValues(self $other): void
404
    {
405 25
        if ($other->isPropertyChanged('configKeyValues')) {
406 1
            $this->configKeyValues = array_merge($this->configKeyValues ?? [], $other->getConfigKeyValues());
407
        }
408
    }
409
410 25
    private function combineEventDispatcher(self $other): void
411
    {
412 25
        if ($other->canCreateEventDispatcher()) {
413 3
            if (!($this->eventDispatcher instanceof ConfigurableEventDispatcher)) {
414 2
                $this->eventDispatcher = new ConfigurableEventDispatcher();
415
            }
416 3
            $this->eventDispatcher->registerGenericListeners((array)$other->genericListeners);
0 ignored issues
show
Bug introduced by
The method registerGenericListeners() does not exist on Gacela\Framework\Event\D...ventDispatcherInterface. It seems like you code against a sub-type of Gacela\Framework\Event\D...ventDispatcherInterface such as Gacela\Framework\Event\D...igurableEventDispatcher. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

416
            $this->eventDispatcher->/** @scrutinizer ignore-call */ 
417
                                    registerGenericListeners((array)$other->genericListeners);
Loading history...
417
418 3
            foreach ($other->specificListeners ?? [] as $event => $listeners) {
419 2
                foreach ($listeners as $callable) {
420 2
                    $this->eventDispatcher->registerSpecificListener($event, $callable);
0 ignored issues
show
Bug introduced by
The method registerSpecificListener() does not exist on Gacela\Framework\Event\D...ventDispatcherInterface. It seems like you code against a sub-type of Gacela\Framework\Event\D...ventDispatcherInterface such as Gacela\Framework\Event\D...igurableEventDispatcher. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

420
                    $this->eventDispatcher->/** @scrutinizer ignore-call */ 
421
                                            registerSpecificListener($event, $callable);
Loading history...
421
                }
422
            }
423
        } else {
424 22
            $this->eventDispatcher = new NullEventDispatcher();
425
        }
426
    }
427
428 51
    private function canCreateEventDispatcher(): bool
429
    {
430 51
        return $this->areEventListenersEnabled
431 51
            && $this->hasEventListeners();
432
    }
433
434 50
    private function hasEventListeners(): bool
435
    {
436 50
        return !empty($this->genericListeners)
437 50
            || !empty($this->specificListeners);
438
    }
439
440
    /**
441
     * @param ?array<string,mixed> $configKeyValues
442
     */
443 71
    private function setConfigKeyValues(?array $configKeyValues): self
444
    {
445 71
        $this->markPropertyChanged('configKeyValues', $configKeyValues);
446 71
        $this->configKeyValues = $configKeyValues ?? self::DEFAULT_CONFIG_KEY_VALUES;
447
448 71
        return $this;
449
    }
450
451
    /**
452
     * @param ?list<callable> $listeners
453
     */
454 71
    private function setGenericListeners(?array $listeners): self
455
    {
456 71
        $this->markPropertyChanged('genericListeners', $listeners);
457 71
        $this->genericListeners = $listeners ?? self::DEFAULT_GENERIC_LISTENERS;
1 ignored issue
show
Documentation Bug introduced by
It seems like $listeners ?? self::DEFAULT_GENERIC_LISTENERS of type array is incompatible with the declared type Gacela\Framework\Bootstrap\list|null 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...
458
459 71
        return $this;
460
    }
461
462
    /**
463
     * @param ?array<class-string,list<callable>> $listeners
1 ignored issue
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...
464
     */
465 71
    private function setSpecificListeners(?array $listeners): self
466
    {
467 71
        $this->markPropertyChanged('specificListeners', $listeners);
468 71
        $this->specificListeners = $listeners ?? self::DEFAULT_SPECIFIC_LISTENERS;
469
470 71
        return $this;
471
    }
472
473
    /**
474
     * @param ?array<string,list<Closure>> $list
1 ignored issue
show
Documentation Bug introduced by
The doc comment ?array<string,list<Closure>> at position 4 could not be parsed: Expected '>' at position 4, but found 'list'.
Loading history...
475
     */
476 71
    private function setServicesToExtend(?array $list): self
477
    {
478 71
        $this->markPropertyChanged('extendGlobalServices', $list);
479 71
        $this->servicesToExtend = $list ?? self::DEFAULT_SERVICES_TO_EXTEND;
480
481 71
        return $this;
482
    }
483
484
    /**
485
     * @param mixed $value
486
     */
487 76
    private function markPropertyChanged(string $name, $value): void
488
    {
489 76
        $this->changedProperties[$name] = ($value !== null);
490
    }
491
492 25
    private function isPropertyChanged(string $name): bool
493
    {
494 25
        return $this->changedProperties[$name] ?? false;
495
    }
496
}
497