Passed
Push — master ( 4fa241...13320f )
by Chema
03:42 queued 11s
created

SetupGacela::buildConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Bootstrap;
6
7
use Closure;
8
use Gacela\Framework\ClassResolver\Cache\GacelaCache;
9
use Gacela\Framework\Config\GacelaConfigBuilder\ConfigBuilder;
10
use Gacela\Framework\Config\GacelaConfigBuilder\MappingInterfacesBuilder;
11
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder;
12
use RuntimeException;
13
14
final class SetupGacela extends AbstractSetupGacela
15
{
16
    /** @var callable(ConfigBuilder):void */
17
    private $configFn;
18
19
    /** @var callable(MappingInterfacesBuilder,array<string,mixed>):void */
20
    private $mappingInterfacesFn;
21
22
    /** @var callable(SuffixTypesBuilder):void */
23
    private $suffixTypesFn;
24
25
    /** @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...
26
    private array $externalServices = [];
27
28
    private ?ConfigBuilder $configBuilder = null;
29
30
    private ?SuffixTypesBuilder $suffixTypesBuilder = null;
31
32
    private ?MappingInterfacesBuilder $mappingInterfacesBuilder = null;
33
34
    private bool $cacheEnabled = true;
35
36
    private string $cacheDirectory = GacelaCache::DEFAULT_DIRECTORY_VALUE;
37
38
    /** @var list<string> */
39
    private array $projectNamespaces = [];
40
41
    /** @var array<string,mixed> */
42
    private array $configKeyValues = [];
43
44 64
    public function __construct()
45
    {
46 64
        $this->configFn = static function (): void {
47
        };
48 64
        $this->mappingInterfacesFn = static function (): void {
49
        };
50 64
        $this->suffixTypesFn = static function (): void {
51
        };
52
    }
53
54 10
    public static function fromFile(string $gacelaFilePath): self
55
    {
56 10
        if (!is_file($gacelaFilePath)) {
57
            throw new RuntimeException("Invalid file path: '{$gacelaFilePath}'");
58
        }
59
60
        /** @var Closure(GacelaConfig):void $setupGacelaFileFn */
61 10
        $setupGacelaFileFn = include $gacelaFilePath;
62
63 10
        return self::fromCallable($setupGacelaFileFn);
64
    }
65
66
    /**
67
     * @param Closure(GacelaConfig):void $setupGacelaFileFn
68
     */
69 40
    public static function fromCallable(Closure $setupGacelaFileFn): self
70
    {
71 40
        $gacelaConfig = new GacelaConfig();
72 40
        $setupGacelaFileFn($gacelaConfig);
73
74 40
        return self::fromGacelaConfig($gacelaConfig);
75
    }
76
77 46
    public static function fromGacelaConfig(GacelaConfig $gacelaConfig): self
78
    {
79 46
        $build = $gacelaConfig->build();
80
81 46
        return (new self())
82 46
            ->setConfigBuilder($build['config-builder'])
83 46
            ->setSuffixTypesBuilder($build['suffix-types-builder'])
84 46
            ->setMappingInterfacesBuilder($build['mapping-interfaces-builder'])
85 46
            ->setExternalServices($build['external-services'])
86 46
            ->setCacheEnabled($build['cache-enabled'])
87 46
            ->setCacheDirectory($build['cache-directory'])
88 46
            ->setProjectNamespaces($build['project-namespaces'])
89 46
            ->setConfigKeyValues($build['config-key-values']);
90
    }
91
92 46
    public function setMappingInterfacesBuilder(MappingInterfacesBuilder $builder): self
93
    {
94 46
        $this->mappingInterfacesBuilder = $builder;
95
96 46
        return $this;
97
    }
98
99 46
    public function setSuffixTypesBuilder(SuffixTypesBuilder $builder): self
100
    {
101 46
        $this->suffixTypesBuilder = $builder;
102
103 46
        return $this;
104
    }
105
106 46
    public function setConfigBuilder(ConfigBuilder $builder): self
107
    {
108 46
        $this->configBuilder = $builder;
109
110 46
        return $this;
111
    }
112
113
    /**
114
     * @param callable(ConfigBuilder):void $callable
115
     */
116 3
    public function setConfigFn(callable $callable): self
117
    {
118 3
        $this->configFn = $callable;
119
120 3
        return $this;
121
    }
122
123 64
    public function buildConfig(ConfigBuilder $configBuilder): ConfigBuilder
124
    {
125 64
        if ($this->configBuilder) {
126 46
            $configBuilder = $this->configBuilder;
127
        }
128
129 64
        ($this->configFn)($configBuilder);
130
131 64
        return $configBuilder;
132
    }
133
134
    /**
135
     * @param callable(MappingInterfacesBuilder,array<string,mixed>):void $callable
136
     */
137 3
    public function setMappingInterfacesFn(callable $callable): self
138
    {
139 3
        $this->mappingInterfacesFn = $callable;
140
141 3
        return $this;
142
    }
143
144
    /**
145
     * Define the mapping between interfaces and concretions, so Gacela services will auto-resolve them automatically.
146
     *
147
     * @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...
148
     */
149 64
    public function buildMappingInterfaces(
150
        MappingInterfacesBuilder $mappingInterfacesBuilder,
151
        array $externalServices
152
    ): MappingInterfacesBuilder {
153 64
        if ($this->mappingInterfacesBuilder) {
154 46
            $mappingInterfacesBuilder = $this->mappingInterfacesBuilder;
155
        }
156
157 64
        ($this->mappingInterfacesFn)(
158
            $mappingInterfacesBuilder,
159 64
            array_merge($this->externalServices, $externalServices)
160
        );
161
162 64
        return $mappingInterfacesBuilder;
163
    }
164
165
    /**
166
     * @param callable(SuffixTypesBuilder):void $callable
167
     */
168 3
    public function setSuffixTypesFn(callable $callable): self
169
    {
170 3
        $this->suffixTypesFn = $callable;
171
172 3
        return $this;
173
    }
174
175
    /**
176
     * Allow overriding gacela resolvable types.
177
     */
178 64
    public function buildSuffixTypes(SuffixTypesBuilder $suffixTypesBuilder): SuffixTypesBuilder
179
    {
180 64
        if ($this->suffixTypesBuilder) {
181 46
            $suffixTypesBuilder = $this->suffixTypesBuilder;
182
        }
183
184 64
        ($this->suffixTypesFn)($suffixTypesBuilder);
185
186 64
        return $suffixTypesBuilder;
187
    }
188
189
    /**
190
     * @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...
191
     */
192 49
    public function setExternalServices(array $array): self
193
    {
194 49
        $this->externalServices = $array;
195
196 49
        return $this;
197
    }
198
199
    /**
200
     * @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...
201
     */
202 16
    public function externalServices(): array
203
    {
204 16
        return $this->externalServices;
205
    }
206
207 46
    public function setCacheEnabled(bool $flag): self
208
    {
209 46
        $this->cacheEnabled = $flag;
210
211 46
        return $this;
212
    }
213
214 51
    public function isCacheEnabled(): bool
215
    {
216 51
        return $this->cacheEnabled;
217
    }
218
219 46
    public function setCacheDirectory(string $dir): self
220
    {
221 46
        $this->cacheDirectory = $dir;
222
223 46
        return $this;
224
    }
225
226 27
    public function getCacheDirectory(): string
227
    {
228 27
        return $this->cacheDirectory;
229
    }
230
231
    /**
232
     * @param list<string> $list
233
     */
234 46
    public function setProjectNamespaces(array $list): self
235
    {
236 46
        $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...
237
238 46
        return $this;
239
    }
240
241
    /**
242
     * @return list<string>
243
     */
244 29
    public function getProjectNamespaces(): array
245
    {
246 29
        return $this->projectNamespaces;
247
    }
248
249
    /**
250
     * @return array<string,mixed>
251
     */
252 51
    public function getConfigKeyValues(): array
253
    {
254 51
        return $this->configKeyValues;
255
    }
256
257
    /**
258
     * @param array<string,mixed> $configKeyValues
259
     */
260 46
    private function setConfigKeyValues(array $configKeyValues): self
261
    {
262 46
        $this->configKeyValues = $configKeyValues;
263
264 46
        return $this;
265
    }
266
}
267