Passed
Push — feature/remove-profiler ( 1b185f )
by Chema
04:28
created

SetupGacela   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Test Coverage

Coverage 97.8%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 30
eloc 79
c 4
b 0
f 0
dl 0
loc 269
rs 10
ccs 89
cts 91
cp 0.978

25 Methods

Rating   Name   Duplication   Size   Complexity  
A setSuffixTypesBuilder() 0 5 1
A getConfigKeyValues() 0 3 1
A buildConfig() 0 9 2
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 setConfigKeyValues() 0 5 1
A getProjectNamespaces() 0 3 1
A fromGacelaConfig() 0 14 1
A setConfigFn() 0 5 1
A fromCallable() 0 6 1
A setExternalServices() 0 5 1
A externalServices() 0 3 1
A fromFile() 0 13 3
A buildSuffixTypes() 0 9 2
A setProjectNamespaces() 0 5 1
A getFileCacheDirectory() 0 3 1
A setFileCacheEnabled() 0 5 1
A isFileCacheEnabled() 0 3 1
A setShouldResetInMemoryCache() 0 5 1
A shouldResetInMemoryCache() 0 3 1
A setFileCacheDirectory() 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 77
    public function __construct()
48
    {
49 77
        $this->configFn = static function (): void {
50
        };
51 77
        $this->mappingInterfacesFn = static function (): void {
52
        };
53 77
        $this->suffixTypesFn = static function (): void {
54
        };
55
    }
56
57 10
    public static function fromFile(string $gacelaFilePath): self
58
    {
59 10
        if (!is_file($gacelaFilePath)) {
60
            throw new RuntimeException("Invalid file path: '{$gacelaFilePath}'");
61
        }
62
63
        /** @var callable(GacelaConfig):void|null $setupGacelaFileFn */
64 10
        $setupGacelaFileFn = include $gacelaFilePath;
65 10
        if (!is_callable($setupGacelaFileFn)) {
66
            return new self();
67
        }
68
69 10
        return self::fromCallable($setupGacelaFileFn);
70
    }
71
72
    /**
73
     * @param callable(GacelaConfig):void $setupGacelaFileFn
74
     */
75 41
    public static function fromCallable(callable $setupGacelaFileFn): self
76
    {
77 41
        $gacelaConfig = new GacelaConfig();
78 41
        $setupGacelaFileFn($gacelaConfig);
79
80 41
        return self::fromGacelaConfig($gacelaConfig);
81
    }
82
83 47
    public static function fromGacelaConfig(GacelaConfig $gacelaConfig): self
84
    {
85 47
        $build = $gacelaConfig->build();
86
87 47
        return (new self())
88 47
            ->setConfigBuilder($build['config-builder'])
89 47
            ->setSuffixTypesBuilder($build['suffix-types-builder'])
90 47
            ->setMappingInterfacesBuilder($build['mapping-interfaces-builder'])
91 47
            ->setExternalServices($build['external-services'])
92 47
            ->setShouldResetInMemoryCache($build['should-reset-in-memory-cache'])
93 47
            ->setFileCacheEnabled($build['file-cache-enabled'])
94 47
            ->setFileCacheDirectory($build['file-cache-directory'])
95 47
            ->setProjectNamespaces($build['project-namespaces'])
96 47
            ->setConfigKeyValues($build['config-key-values']);
97
    }
98
99 47
    public function setMappingInterfacesBuilder(MappingInterfacesBuilder $builder): self
100
    {
101 47
        $this->mappingInterfacesBuilder = $builder;
102
103 47
        return $this;
104
    }
105
106 47
    public function setSuffixTypesBuilder(SuffixTypesBuilder $builder): self
107
    {
108 47
        $this->suffixTypesBuilder = $builder;
109
110 47
        return $this;
111
    }
112
113 47
    public function setConfigBuilder(ConfigBuilder $builder): self
114
    {
115 47
        $this->configBuilder = $builder;
116
117 47
        return $this;
118
    }
119
120
    /**
121
     * @param callable(ConfigBuilder):void $callable
122
     */
123 3
    public function setConfigFn(callable $callable): self
124
    {
125 3
        $this->configFn = $callable;
126
127 3
        return $this;
128
    }
129
130 77
    public function buildConfig(ConfigBuilder $builder): ConfigBuilder
131
    {
132 77
        if ($this->configBuilder) {
133 47
            $builder = $this->configBuilder;
134
        }
135
136 77
        ($this->configFn)($builder);
137
138 77
        return $builder;
139
    }
140
141
    /**
142
     * @param callable(MappingInterfacesBuilder,array<string,mixed>):void $callable
143
     */
144 3
    public function setMappingInterfacesFn(callable $callable): self
145
    {
146 3
        $this->mappingInterfacesFn = $callable;
147
148 3
        return $this;
149
    }
150
151
    /**
152
     * Define the mapping between interfaces and concretions, so Gacela services will auto-resolve them automatically.
153
     *
154
     * @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...
155
     */
156 77
    public function buildMappingInterfaces(
157
        MappingInterfacesBuilder $builder,
158
        array $externalServices
159
    ): MappingInterfacesBuilder {
160 77
        if ($this->mappingInterfacesBuilder) {
161 47
            $builder = $this->mappingInterfacesBuilder;
162
        }
163
164 77
        ($this->mappingInterfacesFn)(
165
            $builder,
166 77
            array_merge($this->externalServices, $externalServices)
167
        );
168
169 77
        return $builder;
170
    }
171
172
    /**
173
     * @param callable(SuffixTypesBuilder):void $callable
174
     */
175 3
    public function setSuffixTypesFn(callable $callable): self
176
    {
177 3
        $this->suffixTypesFn = $callable;
178
179 3
        return $this;
180
    }
181
182
    /**
183
     * Allow overriding gacela resolvable types.
184
     */
185 77
    public function buildSuffixTypes(SuffixTypesBuilder $builder): SuffixTypesBuilder
186
    {
187 77
        if ($this->suffixTypesBuilder) {
188 47
            $builder = $this->suffixTypesBuilder;
189
        }
190
191 77
        ($this->suffixTypesFn)($builder);
192
193 77
        return $builder;
194
    }
195
196
    /**
197
     * @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...
198
     */
199 50
    public function setExternalServices(array $array): self
200
    {
201 50
        $this->externalServices = $array;
202
203 50
        return $this;
204
    }
205
206
    /**
207
     * @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...
208
     */
209 16
    public function externalServices(): array
210
    {
211 16
        return $this->externalServices;
212
    }
213
214 47
    public function setShouldResetInMemoryCache(bool $flag): self
215
    {
216 47
        $this->shouldResetInMemoryCache = $flag;
217
218 47
        return $this;
219
    }
220
221 64
    public function shouldResetInMemoryCache(): bool
222
    {
223 64
        return $this->shouldResetInMemoryCache;
224
    }
225
226 47
    public function setFileCacheEnabled(bool $flag): self
227
    {
228 47
        $this->fileCacheEnabled = $flag;
229
230 47
        return $this;
231
    }
232
233 3
    public function isFileCacheEnabled(): bool
234
    {
235 3
        return $this->fileCacheEnabled;
236
    }
237
238 27
    public function getFileCacheDirectory(): string
239
    {
240 27
        return $this->fileCacheDirectory;
241
    }
242
243 47
    public function setFileCacheDirectory(string $dir): self
244
    {
245 47
        $this->fileCacheDirectory = $dir;
246
247 47
        return $this;
248
    }
249
250
    /**
251
     * @param list<string> $list
252
     */
253 47
    public function setProjectNamespaces(array $list): self
254
    {
255 47
        $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...
256
257 47
        return $this;
258
    }
259
260
    /**
261
     * @return list<string>
262
     */
263 33
    public function getProjectNamespaces(): array
264
    {
265 33
        return $this->projectNamespaces;
266
    }
267
268
    /**
269
     * @return array<string,mixed>
270
     */
271 64
    public function getConfigKeyValues(): array
272
    {
273 64
        return $this->configKeyValues;
274
    }
275
276
    /**
277
     * @param array<string,mixed> $configKeyValues
278
     */
279 47
    private function setConfigKeyValues(array $configKeyValues): self
280
    {
281 47
        $this->configKeyValues = $configKeyValues;
282
283 47
        return $this;
284
    }
285
}
286