Completed
Push — master ( 139fb8...7c9eb0 )
by Thomas
10s
created

serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the ekino Drupal Debug project.
7
 *
8
 * (c) ekino
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\Drupal\Debug\Configuration\Model;
15
16
use Composer\Autoload\ClassLoader;
17
use Ekino\Drupal\Debug\Configuration\ConfigurationManager;
18
19
class SubstituteOriginalDrupalKernelConfiguration extends AbstractConfiguration
20
{
21
    /**
22
     * @var ClassLoader|null
23
     */
24
    private $classLoader;
25
26
    /**
27
     * @param array $processedConfiguration
28
     */
29 2
    public function __construct(array $processedConfiguration)
30
    {
31 2
        parent::__construct($processedConfiguration);
32
33 2
        $this->classLoader = null;
34 2
    }
35
36
    /**
37
     * @return bool
38
     */
39
    public function isEnabled(): bool
40
    {
41
        return $this->processedConfiguration['enabled'];
42
    }
43
44
    /**
45
     * @return ClassLoader
46
     */
47
    public function getClassLoader(): ClassLoader
48
    {
49
        if (!$this->classLoader instanceof ClassLoader) {
50
            if (!$this->isEnabled()) {
51
                throw new \LogicException('The class loader getter should not be called if the original DrupalKernel substitution is disabled.');
52
            }
53
54
            $classLoader = require $this->processedConfiguration['composer_autoload_file_path'];
55
            if (!$classLoader instanceof ClassLoader) {
56
                throw new \RuntimeException(\sprintf('The composer autoload.php file did not return a "%s" instance.', ClassLoader::class));
57
            }
58
59
            $this->classLoader = $classLoader;
60
        }
61
62
        return $this->classLoader;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getCacheDirectory(): string
69
    {
70
        if (!$this->isEnabled()) {
71
            throw new \LogicException('The cache directory getter should not be called if the original DrupalKernel substitution is disabled.');
72
        }
73
74
        if (!isset($this->processedConfiguration['cache_directory'])) {
75
            return ConfigurationManager::getDefaultsConfiguration()->getCacheDirectory();
76
        }
77
78
        return $this->processedConfiguration['cache_directory'];
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 2
    public function serialize(): ?string
85
    {
86 2
        return \serialize(array(
87 2
            $this->processedConfiguration,
88
            null,
89
        ));
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function unserialize($serialized): void
96
    {
97
        list($this->processedConfiguration, $this->classLoader) = \unserialize($serialized);
98
    }
99
}
100