Passed
Pull Request — master (#179)
by Chema
06:30 queued 03:13
created

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