Passed
Push — feature/refactor-setup-gacela ( 5d2d02 )
by Chema
33:02
created

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