Passed
Push — master ( 13320f...91e8dd )
by Chema
03:27
created

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