Passed
Push — feature/reset-cache-from-class... ( b5ffe1...0b1fbf )
by Chema
03:12
created

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