Test Failed
Push — feature/gacela-config-callable ( f0c396 )
by Chema
04:24
created

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