Completed
Push — master ( ef5d35...9af83e )
by Thomas
12s queued 10s
created

getReferenceConfigurationContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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