Total Complexity | 21 |
Total Lines | 195 |
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 | private string $cacheDirectory = 'cache'; |
||
35 | |||
36 | 55 | public function __construct() |
|
37 | { |
||
38 | 55 | $this->configFn = static function (): void { |
|
39 | }; |
||
40 | 55 | $this->mappingInterfacesFn = static function (): void { |
|
41 | }; |
||
42 | 55 | $this->suffixTypesFn = static function (): void { |
|
43 | }; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param Closure(GacelaConfig):void $setupGacelaFileFn |
||
48 | */ |
||
49 | 25 | public static function fromCallable(Closure $setupGacelaFileFn): self |
|
50 | { |
||
51 | 25 | $gacelaConfig = new GacelaConfig(); |
|
52 | 25 | $setupGacelaFileFn($gacelaConfig); |
|
53 | |||
54 | 25 | return self::fromGacelaConfig($gacelaConfig); |
|
55 | } |
||
56 | |||
57 | 37 | public static function fromGacelaConfig(GacelaConfig $gacelaConfig): self |
|
58 | { |
||
59 | 37 | $build = $gacelaConfig->build(); |
|
60 | |||
61 | 37 | return (new self()) |
|
62 | 37 | ->setConfigBuilder($build['config-builder']) |
|
63 | 37 | ->setSuffixTypesBuilder($build['suffix-types-builder']) |
|
64 | 37 | ->setMappingInterfacesBuilder($build['mapping-interfaces-builder']) |
|
65 | 37 | ->setExternalServices($build['external-services']) |
|
66 | 37 | ->setCacheEnabled($build['cache-enabled']) |
|
67 | 37 | ->setCacheDirectory($build['cache-directory']); |
|
68 | } |
||
69 | |||
70 | 37 | public function setMappingInterfacesBuilder(MappingInterfacesBuilder $builder): self |
|
71 | { |
||
72 | 37 | $this->mappingInterfacesBuilder = $builder; |
|
73 | |||
74 | 37 | return $this; |
|
75 | } |
||
76 | |||
77 | 37 | public function setSuffixTypesBuilder(SuffixTypesBuilder $builder): self |
|
78 | { |
||
79 | 37 | $this->suffixTypesBuilder = $builder; |
|
80 | |||
81 | 37 | return $this; |
|
82 | } |
||
83 | |||
84 | 37 | public function setConfigBuilder(ConfigBuilder $builder): self |
|
85 | { |
||
86 | 37 | $this->configBuilder = $builder; |
|
87 | |||
88 | 37 | return $this; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param callable(ConfigBuilder):void $callable |
||
93 | */ |
||
94 | 3 | public function setConfigFn(callable $callable): self |
|
95 | { |
||
96 | 3 | $this->configFn = $callable; |
|
97 | |||
98 | 3 | return $this; |
|
99 | } |
||
100 | |||
101 | 55 | public function buildConfig(ConfigBuilder $configBuilder): ConfigBuilder |
|
102 | { |
||
103 | 55 | if ($this->configBuilder) { |
|
104 | 37 | $configBuilder = $this->configBuilder; |
|
105 | } |
||
106 | |||
107 | 55 | ($this->configFn)($configBuilder); |
|
108 | |||
109 | 55 | return $configBuilder; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param callable(MappingInterfacesBuilder,array<string,mixed>):void $callable |
||
114 | */ |
||
115 | 3 | public function setMappingInterfacesFn(callable $callable): self |
|
116 | { |
||
117 | 3 | $this->mappingInterfacesFn = $callable; |
|
118 | |||
119 | 3 | return $this; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Define the mapping between interfaces and concretions, so Gacela services will auto-resolve them automatically. |
||
124 | * |
||
125 | * @param array<string,class-string|object|callable> $externalServices |
||
1 ignored issue
–
show
|
|||
126 | */ |
||
127 | 55 | public function buildMappingInterfaces( |
|
128 | MappingInterfacesBuilder $mappingInterfacesBuilder, |
||
129 | array $externalServices |
||
130 | ): MappingInterfacesBuilder { |
||
131 | 55 | if ($this->mappingInterfacesBuilder) { |
|
132 | 37 | $mappingInterfacesBuilder = $this->mappingInterfacesBuilder; |
|
133 | } |
||
134 | |||
135 | 55 | ($this->mappingInterfacesFn)( |
|
136 | $mappingInterfacesBuilder, |
||
137 | 55 | array_merge($this->externalServices, $externalServices) |
|
138 | ); |
||
139 | |||
140 | 55 | return $mappingInterfacesBuilder; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param callable(SuffixTypesBuilder):void $callable |
||
145 | */ |
||
146 | 6 | public function setSuffixTypesFn(callable $callable): self |
|
147 | { |
||
148 | 6 | $this->suffixTypesFn = $callable; |
|
149 | |||
150 | 6 | return $this; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * Allow overriding gacela resolvable types. |
||
155 | */ |
||
156 | 55 | public function buildSuffixTypes(SuffixTypesBuilder $suffixTypesBuilder): SuffixTypesBuilder |
|
157 | { |
||
158 | 55 | if ($this->suffixTypesBuilder) { |
|
159 | 37 | $suffixTypesBuilder = $this->suffixTypesBuilder; |
|
160 | } |
||
161 | |||
162 | 55 | ($this->suffixTypesFn)($suffixTypesBuilder); |
|
163 | |||
164 | 55 | return $suffixTypesBuilder; |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param array<string,class-string|object|callable> $array |
||
1 ignored issue
–
show
|
|||
169 | */ |
||
170 | 40 | public function setExternalServices(array $array): self |
|
171 | { |
||
172 | 40 | $this->externalServices = $array; |
|
173 | |||
174 | 40 | return $this; |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * @return array<string,class-string|object|callable> |
||
1 ignored issue
–
show
|
|||
179 | */ |
||
180 | 12 | public function externalServices(): array |
|
181 | { |
||
182 | 12 | return $this->externalServices; |
|
183 | } |
||
184 | |||
185 | 37 | public function setCacheEnabled(bool $flag): self |
|
186 | { |
||
187 | 37 | $this->cacheEnabled = $flag; |
|
188 | |||
189 | 37 | return $this; |
|
190 | } |
||
191 | |||
192 | 41 | public function isCacheEnabled(): bool |
|
193 | { |
||
194 | 41 | return $this->cacheEnabled; |
|
195 | } |
||
196 | |||
197 | 37 | public function setCacheDirectory(string $dir): self |
|
202 | } |
||
203 | |||
204 | 24 | public function getCacheDirectory(): string |
|
205 | { |
||
207 | } |
||
208 | } |
||
209 |