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

GacelaConfig::setClassResolverCached()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
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\ConfigReaderInterface;
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 GacelaConfig
13
{
14
    private ConfigBuilder $configBuilder;
15
16
    private SuffixTypesBuilder $suffixTypesBuilder;
17
18
    private MappingInterfacesBuilder $mappingInterfacesBuilder;
19
20
    /** @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...
21
    private array $externalServices;
22
23
    private bool $classResolverCached = true;
24
25
    /**
26
     * @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...
27
     */
28 23
    public function __construct(array $externalServices = [])
29
    {
30 23
        $this->externalServices = $externalServices;
31 23
        $this->configBuilder = new ConfigBuilder();
32 23
        $this->suffixTypesBuilder = new SuffixTypesBuilder();
33 23
        $this->mappingInterfacesBuilder = new MappingInterfacesBuilder();
34
    }
35
36
    /**
37
     * @return callable(GacelaConfig):void
38
     */
39 7
    public static function withPhpConfigDefault(): callable
40
    {
41 7
        return static function (self $config): void {
42 7
            $config->addAppConfig('config/*.php', 'config/local.php');
43
        };
44
    }
45
46
    /**
47
     * @param string $path define the path where Gacela will read all the config files
48
     * @param string $pathLocal define the path where Gacela will read the local config file
49
     * @param class-string<ConfigReaderInterface>|ConfigReaderInterface|null $reader Define the reader class which will read and parse the config files
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ConfigReade...figReaderInterface|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ConfigReaderInterface>|ConfigReaderInterface|null.
Loading history...
50
     */
51 13
    public function addAppConfig(string $path, string $pathLocal = '', $reader = null): self
52
    {
53 13
        $this->configBuilder->add($path, $pathLocal, $reader);
54
55 13
        return $this;
56
    }
57
58 2
    public function addSuffixTypeFacade(string $suffix): self
59
    {
60 2
        $this->suffixTypesBuilder->addFacade($suffix);
61
62 2
        return $this;
63
    }
64
65 2
    public function addSuffixTypeFactory(string $suffix): self
66
    {
67 2
        $this->suffixTypesBuilder->addFactory($suffix);
68
69 2
        return $this;
70
    }
71
72 2
    public function addSuffixTypeConfig(string $suffix): self
73
    {
74 2
        $this->suffixTypesBuilder->addConfig($suffix);
75
76 2
        return $this;
77
    }
78
79 3
    public function addSuffixTypeDependencyProvider(string $suffix): self
80
    {
81 3
        $this->suffixTypesBuilder->addDependencyProvider($suffix);
82
83 3
        return $this;
84
    }
85
86
    /**
87
     * @param class-string $key
1 ignored issue
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
88
     * @param class-string|object|callable $value
89
     */
90 10
    public function addMappingInterface(string $key, $value): self
91
    {
92 10
        $this->mappingInterfacesBuilder->bind($key, $value);
93
94 10
        return $this;
95
    }
96
97
    /**
98
     * @return class-string|object|callable
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|object|callable at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|object|callable.
Loading history...
99
     */
100 5
    public function getExternalService(string $key)
101
    {
102 5
        return $this->externalServices[$key];
103
    }
104
105
    /**
106
     * @param class-string|object|callable $value
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|object|callable at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|object|callable.
Loading history...
107
     */
108 2
    public function addExternalService(string $key, $value): self
109
    {
110 2
        $this->externalServices[$key] = $value;
111
112 2
        return $this;
113
    }
114
115
    public function setClassResolverCached(bool $flag): self
116
    {
117
        $this->classResolverCached = $flag;
118
119
        return $this;
120
    }
121
122
    /**
123
     * @return array{
124
     *     external-services:array<string,class-string|object|callable>,
125
     *     config-builder:ConfigBuilder,
126
     *     suffix-types-builder:SuffixTypesBuilder,
127
     *     mapping-interfaces-builder:MappingInterfacesBuilder,
128
     *     class-resolver-cached:bool,
129
     * }
130
     *
131
     * @internal
132
     */
133 23
    public function build(): array
134
    {
135
        return [
136 23
            'external-services' => $this->externalServices,
137 23
            'config-builder' => $this->configBuilder,
138 23
            'suffix-types-builder' => $this->suffixTypesBuilder,
139 23
            'mapping-interfaces-builder' => $this->mappingInterfacesBuilder,
140 23
            'class-resolver-cached' => $this->classResolverCached,
141
        ];
142
    }
143
}
144