Passed
Push — feature/class-resolver-events ( a85101...5f03d5 )
by Chema
04:51 queued 03:31
created

SetupGacela   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 305
Duplicated Lines 0 %

Test Coverage

Coverage 98.06%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 90
dl 0
loc 305
ccs 101
cts 103
cp 0.9806
rs 9.68
c 4
b 0
f 0
wmc 34

28 Methods

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