Total Complexity | 19 |
Total Lines | 180 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
12 | final class SetupGacela extends AbstractSetupGacela |
||
13 | { |
||
14 | /** @var callable(ConfigBuilder):void */ |
||
15 | private $configFn; |
||
16 | |||
17 | /** @var callable(MappingInterfacesBuilder,array<string,mixed>):void */ |
||
18 | private $mappingInterfacesFn; |
||
19 | |||
20 | /** @var callable(SuffixTypesBuilder):void */ |
||
21 | private $suffixTypesFn; |
||
22 | |||
23 | /** @var array<string,class-string|object|callable> */ |
||
1 ignored issue
–
show
|
|||
24 | private array $externalServices = []; |
||
25 | |||
26 | private ?ConfigBuilder $configBuilder = null; |
||
27 | |||
28 | private ?SuffixTypesBuilder $suffixTypesBuilder = null; |
||
29 | |||
30 | private ?MappingInterfacesBuilder $mappingInterfacesBuilder = null; |
||
31 | |||
32 | private bool $cacheEnabled = true; |
||
33 | |||
34 | 51 | public function __construct() |
|
35 | { |
||
36 | 51 | $this->configFn = static function (): void { |
|
37 | }; |
||
38 | 51 | $this->mappingInterfacesFn = static function (): void { |
|
39 | }; |
||
40 | 51 | $this->suffixTypesFn = static function (): void { |
|
41 | }; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param Closure(GacelaConfig):void $setupGacelaFileFn |
||
46 | */ |
||
47 | 18 | public static function fromCallable(Closure $setupGacelaFileFn): self |
|
48 | { |
||
49 | 18 | $gacelaConfig = new GacelaConfig(); |
|
50 | 18 | $setupGacelaFileFn($gacelaConfig); |
|
51 | |||
52 | 18 | return self::fromGacelaConfig($gacelaConfig); |
|
53 | } |
||
54 | |||
55 | 30 | public static function fromGacelaConfig(GacelaConfig $gacelaConfig): self |
|
56 | { |
||
57 | 30 | $build = $gacelaConfig->build(); |
|
58 | |||
59 | 30 | return (new self()) |
|
60 | 30 | ->setConfigBuilder($build['config-builder']) |
|
61 | 30 | ->setSuffixTypesBuilder($build['suffix-types-builder']) |
|
62 | 30 | ->setMappingInterfacesBuilder($build['mapping-interfaces-builder']) |
|
63 | 30 | ->setExternalServices($build['external-services']) |
|
64 | 30 | ->setCacheEnabled($build['cache-enabled']); |
|
65 | } |
||
66 | |||
67 | 30 | public function setMappingInterfacesBuilder(MappingInterfacesBuilder $builder): self |
|
68 | { |
||
69 | 30 | $this->mappingInterfacesBuilder = $builder; |
|
70 | |||
71 | 30 | return $this; |
|
72 | } |
||
73 | |||
74 | 30 | public function setSuffixTypesBuilder(SuffixTypesBuilder $builder): self |
|
79 | } |
||
80 | |||
81 | 30 | public function setConfigBuilder(ConfigBuilder $builder): self |
|
82 | { |
||
83 | 30 | $this->configBuilder = $builder; |
|
84 | |||
85 | 30 | return $this; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param callable(ConfigBuilder):void $callable |
||
90 | */ |
||
91 | 3 | public function setConfigFn(callable $callable): self |
|
92 | { |
||
93 | 3 | $this->configFn = $callable; |
|
94 | |||
95 | 3 | return $this; |
|
96 | } |
||
97 | |||
98 | 51 | public function buildConfig(ConfigBuilder $configBuilder): ConfigBuilder |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param callable(MappingInterfacesBuilder,array<string,mixed>):void $callable |
||
111 | */ |
||
112 | 3 | public function setMappingInterfacesFn(callable $callable): self |
|
113 | { |
||
114 | 3 | $this->mappingInterfacesFn = $callable; |
|
115 | |||
116 | 3 | return $this; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Define the mapping between interfaces and concretions, so Gacela services will auto-resolve them automatically. |
||
121 | * |
||
122 | * @param array<string,class-string|object|callable> $externalServices |
||
1 ignored issue
–
show
|
|||
123 | */ |
||
124 | 51 | public function buildMappingInterfaces( |
|
125 | MappingInterfacesBuilder $mappingInterfacesBuilder, |
||
126 | array $externalServices |
||
127 | ): MappingInterfacesBuilder { |
||
128 | 51 | if ($this->mappingInterfacesBuilder) { |
|
129 | 30 | $mappingInterfacesBuilder = $this->mappingInterfacesBuilder; |
|
130 | } |
||
131 | |||
132 | 51 | ($this->mappingInterfacesFn)( |
|
133 | $mappingInterfacesBuilder, |
||
134 | 51 | array_merge($this->externalServices, $externalServices) |
|
135 | ); |
||
136 | |||
137 | 51 | return $mappingInterfacesBuilder; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param callable(SuffixTypesBuilder):void $callable |
||
142 | */ |
||
143 | 6 | public function setSuffixTypesFn(callable $callable): self |
|
144 | { |
||
145 | 6 | $this->suffixTypesFn = $callable; |
|
146 | |||
147 | 6 | return $this; |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Allow overriding gacela resolvable types. |
||
152 | */ |
||
153 | 51 | public function buildSuffixTypes(SuffixTypesBuilder $suffixTypesBuilder): SuffixTypesBuilder |
|
154 | { |
||
155 | 51 | if ($this->suffixTypesBuilder) { |
|
156 | 30 | $suffixTypesBuilder = $this->suffixTypesBuilder; |
|
157 | } |
||
158 | |||
159 | 51 | ($this->suffixTypesFn)($suffixTypesBuilder); |
|
160 | |||
161 | 51 | return $suffixTypesBuilder; |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param array<string,class-string|object|callable> $array |
||
1 ignored issue
–
show
|
|||
166 | */ |
||
167 | 33 | public function setExternalServices(array $array): self |
|
168 | { |
||
169 | 33 | $this->externalServices = $array; |
|
170 | |||
171 | 33 | return $this; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * @return array<string,class-string|object|callable> |
||
1 ignored issue
–
show
|
|||
176 | */ |
||
177 | 12 | public function externalServices(): array |
|
178 | { |
||
179 | 12 | return $this->externalServices; |
|
180 | } |
||
181 | |||
182 | 30 | public function setCacheEnabled(bool $flag): self |
|
183 | { |
||
184 | 30 | $this->cacheEnabled = $flag; |
|
185 | |||
186 | 30 | return $this; |
|
187 | } |
||
188 | |||
189 | 37 | public function isCacheEnabled(): bool |
|
192 | } |
||
193 | } |
||
194 |