KernelBuilder   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 16

13 Methods

Rating   Name   Duplication   Size   Complexity  
A createKernel() 0 3 1
A __construct() 0 3 1
A withBundles() 0 5 1
A withKernelClass() 0 7 1
A withTempDir() 0 5 1
A withPublicService() 0 5 1
A guardKernelClass() 0 8 4
A withDebug() 0 5 1
A bootKernel() 0 6 1
A withPublicServices() 0 5 1
A withBundle() 0 5 1
A withEnvironment() 0 5 1
A withBundleConfiguration() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Zalas\BundleTest\HttpKernel;
5
6
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
7
use Symfony\Component\HttpKernel\KernelInterface;
8
use Zalas\BundleTest\HttpKernel\Exception\ClassNotFoundException;
9
use Zalas\BundleTest\HttpKernel\Exception\KernelNotSupportedException;
10
11
final class KernelBuilder
12
{
13
    /**
14
     * @var string
15
     */
16
    private $kernelClass = TestKernel::class;
17
18
    /**
19
     * @var KernelConfiguration
20
     */
21
    private $configuration;
22
23
    public function __construct(string $namespace = KernelConfiguration::DEFAULT_NAMESPACE)
24
    {
25
        $this->configuration = new KernelConfiguration($namespace);
26
    }
27
28
    public function createKernel(): KernelInterface
29
    {
30
        return new $this->kernelClass($this->configuration);
31
    }
32
33
    public function bootKernel(): KernelInterface
34
    {
35
        $kernel = $this->createKernel();
36
        $kernel->boot();
37
38
        return $kernel;
39
    }
40
41
    public function withEnvironment(string $environment): self
42
    {
43
        $this->configuration = $this->configuration->withEnvironment($environment);
44
45
        return $this;
46
    }
47
48
    public function withDebug(bool $debug): self
49
    {
50
        $this->configuration = $this->configuration->withDebug($debug);
51
52
        return $this;
53
    }
54
55
    public function withBundles(array $bundles): self
56
    {
57
        $this->configuration = $this->configuration->withBundles($bundles);
58
59
        return $this;
60
    }
61
62
    public function withBundle(BundleInterface $bundle): self
63
    {
64
        $this->configuration = $this->configuration->withBundle($bundle);
65
66
        return $this;
67
    }
68
69
    public function withBundleConfiguration(string $extensionName, array $configuration): self
70
    {
71
        $this->configuration = $this->configuration->withBundleConfiguration($extensionName, $configuration);
72
73
        return $this;
74
    }
75
76
    public function withPublicService(string $serviceId): self
77
    {
78
        $this->configuration = $this->configuration->withPublicServiceId($serviceId);
79
80
        return $this;
81
    }
82
83
    public function withPublicServices(array $serviceIds): self
84
    {
85
        $this->configuration = $this->configuration->withPublicServiceIds($serviceIds);
86
87
        return $this;
88
    }
89
90
    public function withTempDir(string $tempDir): self
91
    {
92
        $this->configuration = $this->configuration->withTempDir($tempDir);
93
94
        return $this;
95
    }
96
97
    public function withKernelClass(string $kernelClass): self
98
    {
99
        $this->guardKernelClass($kernelClass);
100
101
        $this->kernelClass = $kernelClass;
102
103
        return $this;
104
    }
105
106
    private function guardKernelClass(string $kernelClass): void
107
    {
108
        if (!\class_exists($kernelClass)) {
109
            throw new ClassNotFoundException($kernelClass);
110
        }
111
112
        if (TestKernel::class !== $kernelClass && !\in_array(TestKernel::class, \class_parents($kernelClass))) {
113
            throw new KernelNotSupportedException($kernelClass, TestKernel::class);
114
        }
115
    }
116
}
117