Passed
Push — feature/178-allow-adding-key-v... ( 9721fa )
by Chema
04:36
created

SetupGacela::getConfigKeyValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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