Completed
Pull Request — master (#84)
by
unknown
06:22
created

warnAboutPotentialConfigurationChanges()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 12
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 18
ccs 0
cts 0
cp 0
crap 6
rs 9.8666
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\ActionMetadata\ActionMetadataManager;
19
use Ekino\Drupal\Debug\Composer\Command\DumpReferenceConfigurationFileCommand;
20
use Ekino\Drupal\Debug\Configuration\ActionsConfiguration;
21
use Ekino\Drupal\Debug\Configuration\ConfigurationManager;
22
use Ekino\Drupal\Debug\Configuration\DefaultsConfiguration;
23
use Ekino\Drupal\Debug\Configuration\Model\DefaultsConfiguration as DefaultsConfigurationModel;
24
use Ekino\Drupal\Debug\Configuration\SubstituteOriginalDrupalKernelConfiguration;
25
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
26
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
27
use Symfony\Component\Config\Definition\ConfigurationInterface;
28
use Symfony\Component\Config\Definition\Dumper\YamlReferenceDumper;
29
use Symfony\Component\Filesystem\Exception\IOException;
30
use Symfony\Component\Filesystem\Filesystem;
31
use Symfony\Component\Yaml\Dumper;
32
use Symfony\Component\Yaml\Parser;
33
34
class ManageConfigurationHelper
35
{
36
    /**
37
     * @var Composer
38
     */
39
    private $composer;
40
41
    /**
42
     * @var IOInterface
43
     */
44
    private $IO;
45
46
    private $configurationManager;
47
48
    /**
49
     * @param Composer    $composer
50
     * @param IOInterface $IO
51
     */
52
    public function __construct(Composer $composer, IOInterface $IO)
53
    {
54
        $this->composer = $composer;
55
        $this->IO = $IO;
56
57
        $this->configurationManager = ConfigurationManager::getInstance();
58
    }
59
60
    public function dumpReferenceConfigurationFile(): bool
61
    {
62
        $configurationFilePath = $this->configurationManager->getConfigurationFilePath();
63
        if ($configurationFilePathExists = $this->configurationManager->doesConfigurationFilePathExists()) {
64
            $this->IO->write(array(
65
                '<comment>An existing drupal-debug configuration file has been found at the following location:</comment>',
66
                \sprintf('<comment>--> "%s"</comment>', \realpath($configurationFilePath)),
67
                '',
68
            ));
69
70
            if (!$this->IO->askConfirmation('<question>Would you like to overwrite it?</question>')) {
71
                $this->IO->write(array(
72
                    '',
73
                    "<info>OK, let's keep it like this then!</info>",
74
                ));
75
76
                return true;
77
            }
78
79
            $this->IO->write('');
80
        }
81
82
        return $this->dumpConfigurationFile($configurationFilePath, $this->getReferenceConfigurationContent(), !$configurationFilePathExists);
83
    }
84
85
    // TODO: a real configuration update path
86
    public function warnAboutPotentialConfigurationChanges(): bool
87
    {
88
        if (!$this->configurationManager->doesConfigurationFilePathExists()) {
89
            return true;
90
        }
91
92
        $this->IO->write(array(
93
            '<comment>A custom drupal-debug configuration file has been found at the following location:</comment>',
94
            \sprintf('<comment>--> "%s"</comment>', \realpath($this->configurationManager->getConfigurationFilePath())),
95
            '',
96
            '<comment>The drupal-debug configuration might have change in the freshly updated code.</comment>',
97
            '',
98
            '<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>',
99
            '',
100
            \sprintf('<comment>You can alternatively dump the reference configuration file with the dedicated command "%s".</comment>', DumpReferenceConfigurationFileCommand::NAME),
101
        ));
102
103
        return true;
104
    }
105
106
    public function askForConfigurationFileDeletion(): bool
107
    {
108
        if (!$this->configurationManager->doesConfigurationFilePathExists()) {
109
            return true;
110
        }
111
112
        $this->IO->write(array(
113
            '<comment>The drupal-debug configuration file is going to be useless: it should be deleted.</comment>',
114
            '',
115
            '<info>It has been found at the following location:</info>',
116
            \sprintf('<info>--> "%s"</info>', \realpath($configurationFilePath = $this->configurationManager->getConfigurationFilePath())),
117
            '',
118
        ));
119
120
        if (!$this->IO->askConfirmation('Would you like to delete it?')) {
121
            $this->IO->write(array(
122
                '',
123
                "<info>OK, let's keep it!</info>",
124
            ));
125
126
            return true;
127
        }
128
129
        $this->IO->write('');
130
131
        if (!@\unlink($configurationFilePath)) {
132
            $this->IO->writeError('<error>The drupal-debug configuration file could not be deleted.</error>');
133
134
            return false;
135
        }
136
137
        $this->IO->write('<info>The drupal-debug configuration file has been successfully deleted.</info>');
138
139
        return true;
140
    }
141
142
    /**
143
     * @param bool $enabled
144
     */
145
    public function toggleOriginalDrupalKernelSubstitution(bool $enabled): bool
146
    {
147
        $configurationFilePath = $this->configurationManager->getConfigurationFilePath();
148
149
        if ($this->configurationManager->doesConfigurationFilePathExists()) {
150
            $configurationFileContent = $this->getCurrentConfigurationContent($configurationFilePath);
151
            if (\is_array($configurationFileContent) && isset($configurationFileContent['drupal-debug'])) {
152
                if (isset($configurationFileContent['drupal-debug']['substitute_original_drupal_kernel']) && \is_array($configurationFileContent['drupal-debug']['substitute_original_drupal_kernel'])) {
153
                    $configurationFileContent['drupal-debug']['substitute_original_drupal_kernel']['enabled'] = $enabled;
154
                } else {
155
                    $configurationFileContent['drupal-debug']['substitute_original_drupal_kernel'] = array(
156
                        'enabled' => $enabled,
157
                    );
158
                }
159
            } else {
160
                $configurationFileContent = null;
161
            }
162
        }
163
164
        if (!isset($configurationFileContent)) {
165
            $configurationFileContent = array(
166
                'drupal-debug' => array(
167
                    'substitute_original_drupal_kernel' => array(
168
                        'enabled' => $enabled,
169
                    ),
170
                ),
171
            );
172
        }
173
174
        return $this->dumpConfigurationFile($configurationFilePath, $configurationFileContent, true);
175
    }
176
177
    /**
178
     * @return array
179
     */
180 4
    private function getReferenceConfigurationContent(): array
181
    {
182
        return (new Parser())->parse((new YamlReferenceDumper())->dump(new class($this->configurationManager) implements ConfigurationInterface {
183
            private $configurationManager;
184
185
            public function __construct(ConfigurationManager $configurationManager)
186
            {
187 4
                $this->configurationManager = $configurationManager;
188 4
            }
189
190
            /**
191
             * {@inheritdoc}
192
             */
193 4
            public function getConfigTreeBuilder(): TreeBuilder
194
            {
195 4
                $treeBuilder = new TreeBuilder();
196
                /** @var ArrayNodeDefinition $rootNode */
197 4
                $rootNode = $treeBuilder->root(ConfigurationManager::ROOT_KEY);
198
                $rootNode
199 4
                    ->children()
200 4
                        ->append((new DefaultsConfiguration())->getArrayNodeDefinition(new TreeBuilder()))
201 4
                        ->append((new ActionsConfiguration((new ActionMetadataManager())->all(), $defaultsConfiguration = new DefaultsConfigurationModel($this->configurationManager->getProcessedDefaultsConfiguration(array()))))->getArrayNodeDefinition(new TreeBuilder()))
202 4
                        ->append((new SubstituteOriginalDrupalKernelConfiguration($defaultsConfiguration))->getArrayNodeDefinition(new TreeBuilder()))
203 4
                    ->end();
204
205 4
                return $treeBuilder;
206
            }
207
        }));
208
    }
209
210
    /**
211
     * @param string $configurationFilePath
212
     *
213
     * @return mixed
214
     */
215 5
    private function getCurrentConfigurationContent(string $configurationFilePath)
216
    {
217 5
        return (new Parser())->parseFile($configurationFilePath);
218
    }
219
220
    /**
221
     * @param string $configurationFilePath
222
     * @param array  $configurationFileContent
223
     * @param bool   $displayLocation
224
     */
225 13
    private function dumpConfigurationFile(string $configurationFilePath, array $configurationFileContent, bool $displayLocation): bool
226
    {
227
        try {
228 13
            (new Filesystem())->dumpFile($configurationFilePath, (new Dumper())->dump($configurationFileContent, 5));
229 4
        } catch (IOException $e) {
230 4
            $this->IO->writeError('<error>The drupal-debug configuration file could not be dumped.</error>');
231
232 4
            return false;
233
        }
234
235 9
        if ($displayLocation) {
236 8
            $this->IO->write(array(
237 8
                '<info>The drupal-debug configuration file has been successfully dumped at the following location:</info>',
238 8
                \sprintf('<info>--> "%s"</info>', \realpath($configurationFilePath)),
239
            ));
240
        } else {
241 1
            $this->IO->write('<info>The drupal-debug configuration file has been successfully dumped.</info>');
242
        }
243
244 9
        return true;
245
    }
246
}
247