Total Complexity | 34 |
Total Lines | 305 |
Duplicated Lines | 0 % |
Coverage | 98.06% |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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
|
|||
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
|
|||
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 |
|
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
|
|||
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
|
|||
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
|
|||
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 |
|
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; |
|
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
|
|||
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 |
|
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
|
|||
314 | */ |
||
315 | 51 | private function setEventListeners(array $eventListeners): self |
|
320 | } |
||
321 | } |
||
322 |