Passed
Pull Request — master (#37)
by Thomas
03:43
created

toggleOriginalDrupalKernelSubstitution()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 8
nop 1
dl 0
loc 29
ccs 0
cts 13
cp 0
crap 56
rs 8.8333
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\Composer\Helper;
15
16
use Composer\Composer;
17
use Composer\IO\IOInterface;
18
use Ekino\Drupal\Debug\Composer\Command\DumpReferenceConfigurationFileCommand;
19
use Ekino\Drupal\Debug\Configuration\Configuration;
20
use Ekino\Drupal\Debug\Configuration\ConfigurationManager;
21
use Symfony\Component\Config\Definition\Dumper\YamlReferenceDumper;
22
use Symfony\Component\Filesystem\Exception\IOException;
23
use Symfony\Component\Filesystem\Filesystem;
24
use Symfony\Component\Yaml\Dumper;
25
use Symfony\Component\Yaml\Parser;
26
27
class ManageConfigurationHelper
28
{
29
    /**
30
     * @var Composer
31
     */
32
    private $composer;
33
34
    /**
35
     * @var IOInterface
36
     */
37
    private $IO;
38
39
    /**
40
     * @param Composer    $composer
41
     * @param IOInterface $IO
42
     */
43 1
    public function __construct(Composer $composer, IOInterface $IO)
44
    {
45 1
        $this->composer = $composer;
46 1
        $this->IO = $IO;
47 1
    }
48
49
    public function dumpReferenceConfigurationFile(): bool
50
    {
51
        list($configurationFilePath, $configurationFilePathExists) = ConfigurationManager::getConfigurationFilePathInfo();
52
        if ($configurationFilePathExists) {
53
            $this->IO->write(array(
54
                '<comment>An existing drupal-debug configuration file has been found at the following location:</comment>',
55
                \sprintf('<comment>--> "%s"</comment>', \realpath($configurationFilePath)),
56
                '',
57
            ));
58
59
            if (!$this->IO->askConfirmation('<question>Would you like to overwrite it?</question>')) {
60
                $this->IO->write(array(
61
                    '',
62
                    "<info>OK, let's keep it like this then!</info>",
63
                ));
64
65
                return true;
66
            }
67
68
            $this->IO->write('');
69
        }
70
71
        return $this->dumpConfigurationFile($configurationFilePath, $this->getReferenceConfigurationContent(), !$configurationFilePathExists);
72
    }
73
74
    // TODO: a real configuration update path
75
    public function warnAboutPotentialConfigurationChanges(): bool
76
    {
77
        list($configurationFilePath, $configurationFilePathExists) = ConfigurationManager::getConfigurationFilePathInfo();
78
        if (!$configurationFilePathExists) {
79
            return true;
80
        }
81
82
        $this->IO->write(array(
83
            '<comment>A custom drupal-debug configuration file has been found at the following location:</comment>',
84
            \sprintf('<comment>--> "%s"</comment>', \realpath($configurationFilePath)),
85
            '',
86
            '<comment>The drupal-debug configuration might have change in the freshly updated code.</comment>',
87
            '',
88
            '<comment>If you encounter any problem after this update, it will surely be related to configuration. Please refer to the documentation and the release changelog to fix it.</comment>',
89
            '',
90
            \sprintf('<comment>You can alternatively dump the reference configuration file with the dedicated command "%s".</comment>', DumpReferenceConfigurationFileCommand::NAME),
91
        ));
92
93
        return true;
94
    }
95
96
    public function askForConfigurationFileDeletion(): bool
97
    {
98
        list($configurationFilePath, $configurationFilePathExists) = ConfigurationManager::getConfigurationFilePathInfo();
99
        if (!$configurationFilePathExists) {
100
            return true;
101
        }
102
103
        $this->IO->write(array(
104
            '<comment>The drupal-debug configuration file is going to be useless: it should be deleted.</comment>',
105
            '',
106
            '<info>It has been found at the following location:</info>',
107
            \sprintf('<info>--> "%s"</info>', \realpath($configurationFilePath)),
108
            '',
109
        ));
110
111
        if (!$this->IO->askConfirmation('Would you like to delete it?')) {
112
            $this->IO->write(array(
113
                '',
114
                "<info>OK, let's keep it!</info>",
115
            ));
116
117
            return true;
118
        }
119
120
        $this->IO->write('');
121
122
        if (!@\unlink($configurationFilePath)) {
123
            $this->IO->writeError('<error>The drupal-debug configuration file could not be deleted.</error>');
124
125
            return false;
126
        }
127
128
        $this->IO->write('<info>The drupal-debug configuration file has been successfully deleted.</info>');
129
130
        return true;
131
    }
132
133
    /**
134
     * @param bool $enabled
135
     */
136
    public function toggleOriginalDrupalKernelSubstitution(bool $enabled): bool
137
    {
138
        list($configurationFilePath, $configurationFilePathExists) = ConfigurationManager::getConfigurationFilePathInfo();
139
        if ($configurationFilePathExists) {
140
            $configurationFileContent = $this->getCurrentConfigurationContent($configurationFilePath);
141
            if (\is_array($configurationFileContent) && isset($configurationFileContent['drupal-debug'])) {
142
                if (isset($configurationFileContent['drupal-debug']['substitute_original_drupal_kernel']) && \is_array($configurationFileContent['drupal-debug']['substitute_original_drupal_kernel'])) {
143
                    $configurationFileContent['drupal-debug']['substitute_original_drupal_kernel']['enabled'] = $enabled;
144
                } else {
145
                    $configurationFileContent['drupal-debug']['substitute_original_drupal_kernel'] = array(
146
                        'enabled' => $enabled,
147
                    );
148
                }
149
            } else {
150
                $configurationFileContent = null;
151
            }
152
        }
153
154
        if (!isset($configurationFileContent)) {
155
            $configurationFileContent = array(
156
                'drupal-debug' => array(
157
                    'substitute_original_drupal_kernel' => array(
158
                        'enabled' => $enabled,
159
                    ),
160
                ),
161
            );
162
        }
163
164
        return $this->dumpConfigurationFile($configurationFilePath, $configurationFileContent, true);
165
    }
166
167
    /**
168
     * @return array
169
     */
170
    private function getReferenceConfigurationContent(): array
171
    {
172
        return (new Parser())->parse((new YamlReferenceDumper())->dump(new Configuration()));
173
    }
174
175
    /**
176
     * @param string $configurationFilePath
177
     *
178
     * @return mixed
179
     */
180
    private function getCurrentConfigurationContent(string $configurationFilePath)
181
    {
182
        return (new Parser())->parseFile($configurationFilePath);
183
    }
184
185
    /**
186
     * @param string $configurationFilePath
187
     * @param array  $configurationFileContent
188
     * @param bool   $displayLocation
189
     */
190
    private function dumpConfigurationFile(string $configurationFilePath, array $configurationFileContent, bool $displayLocation): bool
191
    {
192
        try {
193
            (new Filesystem())->dumpFile($configurationFilePath, (new Dumper())->dump($configurationFileContent, 4));
194
        } catch (IOException $e) {
195
            $this->IO->writeError('<error>The drupal-debug configuration file could not be dumped.</error>');
196
197
            return false;
198
        }
199
200
        if ($displayLocation) {
201
            $this->IO->write(array(
202
                '<info>The drupal-debug configuration file has been successfully dumped at the following location:</info>',
203
                \sprintf('<info>--> "%s"</info>', \realpath($configurationFilePath)),
204
            ));
205
        } else {
206
            $this->IO->write('<info>The drupal-debug configuration file has been successfully dumped.</info>');
207
        }
208
209
        return true;
210
    }
211
}
212