Passed
Push — feature/rename-reset-cache-by-... ( 99a3ec )
by Jesús
04:26
created

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