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

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