Passed
Push — feature/rename-classresolverca... ( 4454d3 )
by Jesús
04:01
created

SetupGacela::setResetCache()   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
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
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\Config\GacelaConfigBuilder\ConfigBuilder;
9
use Gacela\Framework\Config\GacelaConfigBuilder\MappingInterfacesBuilder;
10
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder;
11
12
class SetupGacela extends AbstractSetupGacela
13
{
14
    /** @var ?callable(ConfigBuilder):void */
15
    private $configFn = null;
16
17
    /** @var ?callable(MappingInterfacesBuilder,array<string,mixed>):void */
18
    private $mappingInterfacesFn = null;
19
20
    /** @var ?callable(SuffixTypesBuilder):void */
21
    private $suffixTypesFn = null;
22
23
    /** @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...
24
    private array $externalServices = [];
25
26
    private ?ConfigBuilder $configBuilder = null;
27
28
    private ?SuffixTypesBuilder $suffixTypesBuilder = null;
29
30
    private ?MappingInterfacesBuilder $mappingInterfacesBuilder = null;
31
32
    private bool $resetCache = false;
33
34
    /**
35
     * @param Closure(GacelaConfig):void $setupGacelaFileFn
36
     */
37 18
    public static function fromCallable(Closure $setupGacelaFileFn): self
38
    {
39 18
        $gacelaConfig = new GacelaConfig();
40 18
        $setupGacelaFileFn($gacelaConfig);
41
42 18
        return self::fromGacelaConfig($gacelaConfig);
43
    }
44
45 31
    public static function fromGacelaConfig(GacelaConfig $gacelaConfig): self
46
    {
47 31
        $build = $gacelaConfig->build();
48
49 31
        return (new self())
50 31
            ->setConfigBuilder($build['config-builder'])
51 31
            ->setSuffixTypesBuilder($build['suffix-types-builder'])
52 31
            ->setMappingInterfacesBuilder($build['mapping-interfaces-builder'])
53 31
            ->setExternalServices($build['external-services'])
54 31
            ->setResetCache($build['reset-cache']);
55
    }
56
57 31
    public function setMappingInterfacesBuilder(MappingInterfacesBuilder $builder): self
58
    {
59 31
        $this->mappingInterfacesBuilder = $builder;
60
61 31
        return $this;
62
    }
63
64 31
    public function setSuffixTypesBuilder(SuffixTypesBuilder $builder): self
65
    {
66 31
        $this->suffixTypesBuilder = $builder;
67
68 31
        return $this;
69
    }
70
71 31
    public function setConfigBuilder(ConfigBuilder $builder): self
72
    {
73 31
        $this->configBuilder = $builder;
74
75 31
        return $this;
76
    }
77
78
    /**
79
     * @param callable(ConfigBuilder):void $callable
80
     */
81 3
    public function setConfigFn(callable $callable): self
82
    {
83 3
        $this->configFn = $callable;
84
85 3
        return $this;
86
    }
87
88 49
    public function buildConfig(ConfigBuilder $configBuilder): ConfigBuilder
89
    {
90 49
        if ($this->configBuilder) {
91 31
            $configBuilder = $this->configBuilder;
92
        }
93
94 49
        $this->configFn && ($this->configFn)($configBuilder);
95
96 49
        return $configBuilder;
97
    }
98
99
    /**
100
     * @param callable(MappingInterfacesBuilder,array<string,mixed>):void $callable
101
     */
102 3
    public function setMappingInterfacesFn(callable $callable): self
103
    {
104 3
        $this->mappingInterfacesFn = $callable;
105
106 3
        return $this;
107
    }
108
109
    /**
110
     * Define the mapping between interfaces and concretions, so Gacela services will auto-resolve them automatically.
111
     *
112
     * @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...
113
     */
114 49
    public function buildMappingInterfaces(
115
        MappingInterfacesBuilder $mappingInterfacesBuilder,
116
        array $externalServices
117
    ): MappingInterfacesBuilder {
118 49
        if ($this->mappingInterfacesBuilder) {
119 31
            $mappingInterfacesBuilder = $this->mappingInterfacesBuilder;
120
        }
121
122 49
        $this->mappingInterfacesFn && ($this->mappingInterfacesFn)(
123
            $mappingInterfacesBuilder,
124 3
            array_merge($this->externalServices, $externalServices)
125
        );
126
127 49
        return $mappingInterfacesBuilder;
128
    }
129
130
    /**
131
     * @param callable(SuffixTypesBuilder):void $callable
132
     */
133 6
    public function setSuffixTypesFn(callable $callable): self
134
    {
135 6
        $this->suffixTypesFn = $callable;
136
137 6
        return $this;
138
    }
139
140
    /**
141
     * Allow overriding gacela resolvable types.
142
     */
143 49
    public function buildSuffixTypes(SuffixTypesBuilder $suffixTypesBuilder): SuffixTypesBuilder
144
    {
145 49
        if ($this->suffixTypesBuilder) {
146 31
            $suffixTypesBuilder = $this->suffixTypesBuilder;
147
        }
148
149 49
        $this->suffixTypesFn && ($this->suffixTypesFn)($suffixTypesBuilder);
150
151 49
        return $suffixTypesBuilder;
152
    }
153
154
    /**
155
     * @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...
156
     */
157 34
    public function setExternalServices(array $array): self
158
    {
159 34
        $this->externalServices = $array;
160
161 34
        return $this;
162
    }
163
164
    /**
165
     * @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...
166
     */
167 13
    public function externalServices(): array
168
    {
169 13
        return $this->externalServices;
170
    }
171
172 31
    public function setResetCache(bool $flag): self
173
    {
174 31
        $this->resetCache = $flag;
175
176 31
        return $this;
177
    }
178
179 34
    public function isResetCache(): bool
180
    {
181 34
        return $this->resetCache;
182
    }
183
}
184