RebuildSettingsCommand::mergeSettings()   C
last analyzed

Complexity

Conditions 9
Paths 256

Size

Total Lines 56
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 5.3148
c 0
b 0
f 0
cc 9
eloc 37
nc 256
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Taisiya\CoreBundle\Console\Command\Config;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Finder\Finder;
8
use Symfony\Component\Finder\SplFileInfo;
9
use Taisiya\CoreBundle\Console\Command\Command;
10
use Taisiya\CoreBundle\Console\Style\TaisiyaStyle;
11
use Taisiya\CoreBundle\Exception\RuntimeException;
12
13
final class RebuildSettingsCommand extends Command
14
{
15
    const NAME = 'config:rebuild-settings';
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected function configure()
21
    {
22
        $this->setName(self::NAME)
23
            ->setDescription('Rebuild project settings');
24
    }
25
26
    /**
27
     * @param InputInterface  $input
28
     * @param OutputInterface $output
29
     */
30
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32
        $io = new TaisiyaStyle($input, $output);
33
34
        $this->copySettings($io);
35
        $this->mergeSettings($io);
36
    }
37
38
    /**
39
     * @param TaisiyaStyle $io
40
     *
41
     * @throws RuntimeException
42
     */
43
    final protected function copySettings(TaisiyaStyle $io): void
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
44
    {
45
        $io->isVerbose() && $io->writeln('Copy bundles settings to project');
46
47
        $finder = Finder::create()
48
            ->in(TAISIYA_ROOT)
49
            ->path('vendor')
50
            ->files()
51
            ->name('settings.default.php')
52
            ->filter(function (SplFileInfo $file) {
53
                return (bool) preg_match('/\/app\/config\//', $file->getPathname());
54
            });
55
56
        foreach ($finder as $file) {
57
            $dest = preg_replace('/^.+?\/vendor\/(.+?)\/(.+?)\/.+?$/', TAISIYA_ROOT.'/app/config/\1.\2.settings.default.php', $file->getPathname());
58
            if (!copy($file->getPathname(), $dest)) {
59
                throw new RuntimeException('Couldn\'t copy '.str_replace(TAISIYA_ROOT.'/', '', $file->getPathname()).' to '.str_replace(TAISIYA_ROOT.'/', '', $dest));
60
            } else {
61
                $io->writeln('  - '.str_replace(TAISIYA_ROOT.'/', '', $file->getPathname()).' is copied to <info>'.str_replace(TAISIYA_ROOT.'/', '', $dest).'</info>');
62
            }
63
        }
64
    }
65
66
    /**
67
     * @param TaisiyaStyle $io
68
     *
69
     * @throws RuntimeException
70
     */
71
    final protected function mergeSettings(TaisiyaStyle $io): void
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
72
    {
73
        $io->isVerbose() && $io->writeln('Merge settings into one file');
74
75
        $uname = php_uname('n');
76
        $files = [];
77
78
        $finder = Finder::create()
79
            ->in(TAISIYA_ROOT)
80
            ->path('app/config')
81
            ->files()
82
            ->name('/\.settings\.default\.php$/');
83
84
        foreach ($finder as $file) {
85
            $files[] = $file->getPathname();
86
        }
87
88
        $finder = Finder::create()
89
            ->in(TAISIYA_ROOT)
90
            ->path('app/config')
91
            ->files()
92
            ->name('/\.settings\.local\.php$/');
93
94
        foreach ($finder as $file) {
95
            $files[] = $file->getPathname();
96
        }
97
98
        $finder = Finder::create()
99
            ->in(TAISIYA_ROOT)
100
            ->path('app/config')
101
            ->files()
102
            ->name('/\.settings\.'.preg_quote($uname, '/').'\.local\.php$/');
103
104
        foreach ($finder as $file) {
105
            $files[] = $file->getPathname();
106
        }
107
108
        $files[] = TAISIYA_ROOT.'/app/config/settings.default.php';
109
        if (file_exists(TAISIYA_ROOT.'/app/config/settings.local.php')) {
110
            $files[] = TAISIYA_ROOT.'/app/config/settings.local.php';
111
        }
112
        if (file_exists(TAISIYA_ROOT.'/app/config/settings.'.$uname.'.local.php')) {
113
            $files[] = TAISIYA_ROOT.'/app/config/settings.'.$uname.'.local.php';
114
        }
115
116
        $settings = [];
117
        foreach ($files as $file) {
118
            $settings = array_merge_recursive($settings, require $file);
119
        }
120
121
        if (!file_put_contents(TAISIYA_ROOT.'/app/config/settings.php', "<?php\n\nreturn ".var_export($settings, true).";\n")) {
122
            throw new RuntimeException('Couldn\'t write to the file '.TAISIYA_ROOT.'/app/config/settings.php');
123
        } else {
124
            $io->writeln('  - writed to <info>app/config/settings.php</info>');
125
        }
126
    }
127
}
128