SetDevelopmentMode::disableDevelopmentMode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 11
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 15
ccs 0
cts 11
cp 0
crap 6
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\DevTools\Application\Command;
6
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\ArrayInput;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Finder\Finder;
13
use Symfony\Component\Finder\SplFileInfo;
14
15
class SetDevelopmentMode extends Command
16
{
17
    public const NAME = 'config:development-mode';
18
    private const TRUE_OPTIONS = [
19
        1,
20
        '1',
21
        true,
22
        'true'
23
    ];
24
    private const FALSE_OPTIONS = [
25
        0,
26
        '0',
27
        false,
28
        'false'
29
    ];
30
    /** @var array<mixed>  */
31
    private array $config;
32
33
    /**
34
     * @param array<mixed> $config
35
     */
36
    public function __construct(array $config)
37
    {
38
        parent::__construct();
39
        $this->config = $config;
40
    }
41
42
    protected function configure(): void
43
    {
44
        $this->setName(self::NAME)
45
            ->setDescription('Enables development mode.')
46
            ->addOption(
47
                'disable',
48
                'dis',
49
                InputArgument::OPTIONAL,
50
                'Disables development mode.',
51
                false
52
            );
53
    }
54
55
    protected function execute(InputInterface $input, OutputInterface $output): ?int
56
    {
57
        $needToDisableDevelopmentMode = $this->disableDevelopmentModeInputFormatter($input->getOption('disable'));
58
        if ($needToDisableDevelopmentMode) {
59
            $this->disableDevelopmentMode($output);
60
            return 0;
61
        }
62
        $this->enableDevelopmentMode($output);
63
64
        return 0;
65
    }
66
67
    /**
68
     * @param mixed $getOption
69
     */
70
    private function disableDevelopmentModeInputFormatter($getOption): bool
71
    {
72
        if (in_array($getOption, self::TRUE_OPTIONS, true)) {
73
            return true;
74
        }
75
76
        if (in_array($getOption, self::FALSE_OPTIONS, true)) {
77
            return false;
78
        }
79
80
        throw new \InvalidArgumentException(sprintf(
81
            'Invalid option %s given, dissable option value should be one of: %s, %s',
82
            (string)$getOption,
83
            implode(',', self::TRUE_OPTIONS),
84
            implode(',', self::FALSE_OPTIONS)
85
        ));
86
    }
87
88
    private function enableDevelopmentMode(OutputInterface $output): void
89
    {
90
        $finder = new Finder();
91
        $finder->files()->in($this->config['config_dir'])->name('*.dist');
92
        /** @var SplFileInfo $file */
93
        foreach ($finder as $file) {
94
            $newFile = sprintf(
95
                '%s/%s',
96
                $file->getPath(),
97
                str_replace('.dist', '', $file->getFilename())
98
            );
99
            if (file_exists($newFile)) {
100
                continue;
101
            }
102
103
            file_put_contents($newFile, $file->getContents());
104
            $output->writeln(sprintf(
105
                '<info>Development mode config file %s was successfully created</info>',
106
                $newFile
107
            ));
108
        }
109
110
        $application = $this->getApplication();
111
        if (null === $application) {
112
            throw new \RuntimeException('Cli application should be initialized.');
113
        }
114
        $command = $application->find(ClearConfigCache::NAME);
115
        $arguments = [
116
            'command' => ClearConfigCache::NAME
117
        ];
118
        $command->run(new ArrayInput($arguments), $output);
119
    }
120
121
    private function disableDevelopmentMode(OutputInterface $output): void
122
    {
123
        $finder = new Finder();
124
        $finder->files()->in($this->config['config_dir'])->name('*.dev.{yaml,xml,php}');
125
        /** @var SplFileInfo $file */
126
        foreach ($finder as $file) {
127
            $fileToRemove = sprintf(
128
                '%s/%s',
129
                $file->getPath(),
130
                $file->getFilename()
131
            );
132
            unlink($fileToRemove);
133
            $output->writeln(sprintf(
134
                '<info>Development mode config file %s was successfully removed.</info>',
135
                $fileToRemove
136
            ));
137
        }
138
    }
139
}
140