Passed
Pull Request — master (#208)
by Jesús
07:17 queued 03:55
created

SetupGacela::setCacheEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1

1 Method

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