Passed
Push — feature/custom-caching-dir ( f598fb )
by Chema
04:58
created

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