KernelConfiguration::withBundle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Zalas\BundleTest\HttpKernel;
5
6
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
7
8
class KernelConfiguration
9
{
10
    const DEFAULT_ENVIRONMENT = 'test';
11
    const DEFAULT_NAMESPACE = 'tests';
12
13
    /**
14
     * @var string
15
     */
16
    protected $environment = self::DEFAULT_ENVIRONMENT;
17
18
    /**
19
     * @var bool
20
     */
21
    protected $debug = true;
22
23
    /**
24
     * @var BundleInterface[]
25
     */
26
    protected $bundles = [];
27
28
    /**
29
     * @var array
30
     */
31
    protected $bundleConfigurations = [];
32
33
    /**
34
     * @var string|null
35
     */
36
    protected $tempDir;
37
38
    /**
39
     * @var string
40
     */
41
    private $namespace;
42
43
    /**
44
     * @var string[]
45
     */
46
    private $publicServiceIds = [];
47
48
    public function __construct(string $namespace = self::DEFAULT_NAMESPACE)
49
    {
50
        $this->namespace = $namespace;
51
    }
52
53
    public function withEnvironment(string $environment): self
54
    {
55
        $config = clone $this;
56
        $config->environment = $environment;
57
58
        return $config;
59
    }
60
61
    public function withDebug(bool $debug): self
62
    {
63
        $config = clone $this;
64
        $config->debug = $debug;
65
66
        return $config;
67
    }
68
69
    public function withBundle(BundleInterface $bundle): self
70
    {
71
        $config = clone $this;
72
        $config->bundles[] = $bundle;
73
74
        return $config;
75
    }
76
77
    /**
78
     * @param BundleInterface[] $bundles
79
     */
80
    public function withBundles(array $bundles): self
81
    {
82
        return \array_reduce($bundles, function (self $config, BundleInterface $bundle) {
83
            return $config->withBundle($bundle);
84
        }, $this);
85
    }
86
87
    public function withBundleConfiguration(string $extensionName, array $configuration): self
88
    {
89
        $config = clone $this;
90
        $config->bundleConfigurations[$extensionName] = \array_merge_recursive(
91
            $config->bundleConfigurations[$extensionName] ?? [],
92
            $configuration
93
        );
94
95
        return $config;
96
    }
97
98
    public function withTempDir(string $tempDir): self
99
    {
100
        $config = clone $this;
101
        $config->tempDir = $tempDir;
102
103
        return $config;
104
    }
105
106
    public function withPublicServiceId(string $serviceId): self
107
    {
108
        $config = clone $this;
109
        $config->publicServiceIds[] = $serviceId;
110
111
        return $config;
112
    }
113
114
    /**
115
     * @param string[] $serviceIds
116
     */
117
    public function withPublicServiceIds(array $serviceIds): self
118
    {
119
        return \array_reduce($serviceIds, function (self $config, string $serviceId) {
120
            return $config->withPublicServiceId($serviceId);
121
        }, $this);
122
    }
123
124
    /**
125
     * Computes an unique identifier of the current configuration.
126
     *
127
     * Cache will be scoped (so also refreshed) based on this value.
128
     */
129
    public function getHash(): string
130
    {
131
        return \sha1(\serialize([
132
            $this->getEnvironment(),
133
            $this->isDebug(),
134
            \array_map(function (BundleInterface $bundle) {
135
                return \get_class($bundle);
136
            }, $this->getBundles()),
137
            $this->getAllBundleConfigurations(),
138
            $this->tempDir,
139
            $this->namespace,
140
            $this->getPublicServiceIds(),
141
        ]));
142
    }
143
144
    public function getEnvironment(): string
145
    {
146
        return $this->environment;
147
    }
148
149
    public function isDebug(): bool
150
    {
151
        return $this->debug;
152
    }
153
154
    /**
155
     * @return BundleInterface[]
156
     */
157
    public function getBundles(): array
158
    {
159
        return $this->bundles;
160
    }
161
162
    public function getAllBundleConfigurations(): array
163
    {
164
        return $this->bundleConfigurations;
165
    }
166
167
    public function getTempDir(): string
168
    {
169
        return \sprintf('%s/%s/%s', $this->tempDir ?? \sys_get_temp_dir(), $this->namespace, $this->getHash());
170
    }
171
172
    final public function getCacheDir(): string
173
    {
174
        return \sprintf('%s/var/cache/%s', $this->getTempDir(), $this->environment);
175
    }
176
177
    final public function getLogDir(): string
178
    {
179
        return \sprintf('%s/var/log', $this->getTempDir());
180
    }
181
182
    /**
183
     * @return string[]
184
     */
185
    public function getPublicServiceIds(): array
186
    {
187
        return $this->publicServiceIds;
188
    }
189
}
190