ScriptHandler::createInitialConfig()   B
last analyzed

Complexity

Conditions 8
Paths 37

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 0
cts 32
cp 0
rs 7.6937
c 0
b 0
f 0
cc 8
nc 37
nop 1
crap 72

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 SumoCoders\FrameworkCoreBundle\Composer;
4
5
use Composer\Script\Event;
6
use Composer\IO\IOInterface;
7
use SumoCoders\FrameworkCoreBundle\Installer\Installer;
8
9
class ScriptHandler
10
{
11
    /**
12
     * Create the initial configuration for our project
13
     *
14
     * @param Event $event
15
     */
16
    public static function createInitialConfig(Event $event)
17
    {
18
        $installer = new Installer();
19
        $io = $event->getIO();
20
21
        // check if parameters.yml exists
22
        $rootDir = realpath(__DIR__ . '/../../../../');
23
        if (file_exists($rootDir . '/app/config/parameters.yml')) {
24
            $io->write(
25
                $installer->getDecoratedMessage(
26
                    'Skipping creating the initial config as parameters.yml already exists',
27
                    'info',
28
                    $io->isDecorated()
29
                )
30
            );
31
        } else {
32
            $information = $installer->extractInformationFromPath($rootDir);
33
34
            // ask all the information we need
35
            $config = [];
36
            $config['client'] = $installer->ask($io, 'client name', $information['client']);
37
            $config['project'] = $installer->ask($io, 'project name', $information['project']);
38
39
            $config['database_name'] = substr($config['client'], 0, 8) . '_' . substr($config['project'], 0, 7);
40
            $config['database_user'] = $config['database_name'];
41
42
            if ($information['is_local']) {
43
                // this is the ip-address of our Vagrantbox
44
                $config['database_host'] = '10.11.12.13';
45
                $config['database_user'] = 'root';
46
                $config['database_password'] = 'root';
47
            }
48
49
            $config['secret'] = md5(uniqid());
50
51
            // create the database if requested
52
            if ($installer->askConfirmation($io, 'Should I create the database?')) {
53
                passthru('mysqladmin create ' . $config['database_name']);
54
            }
55
56
            // alter the Capfile if requested
57
            $capfilePath = $rootDir . '/Capfile';
58
            if (file_exists($capfilePath)) {
59
                if ($installer->askConfirmation($io, 'Should I alter the Capfile?')) {
60
                    $installer->updateCapfile($capfilePath, $config);
61
                }
62
            }
63
64
            // alter the dist file if requested
65
            $parameterYmlDistPath = $rootDir . '/app/config/parameters.yml.dist';
66
            if (file_exists($parameterYmlDistPath)) {
67
                if ($installer->askConfirmation($io, 'Should I alter parameters.yml.dist?')) {
68
                    $installer->updateYmlFile($parameterYmlDistPath, $config);
69
                }
70
            }
71
        }
72
    }
73
74
    /**
75
     * Install NPM
76
     *
77
     * @param Event $event
78
     */
79
    public static function installNPM(Event $event)
80
    {
81
        static::runCommandOnlyInDevMode('yarn', $event->getIO(), $event->isDevMode());
82
    }
83
84
    /**
85
     * Run gulp build
86
     *
87
     * @param Event $event
88
     */
89
    public static function gulpBuild(Event $event)
90
    {
91
        static::runCommandOnlyInDevMode('gulp build', $event->getIO(), $event->isDevMode());
92
    }
93
94
    /**
95
     * Running the given command only when we are in dev-mode
96
     * The output will be send directly to the output buffer
97
     *
98
     * @param string      $command
99
     * @param IOInterface $io
100
     * @param boolean     $isDevMode
101
     *
102
     */
103 1
    public static function runCommandOnlyInDevMode($command, IOInterface $io, $isDevMode)
104
    {
105
        // make our command look nice
106 1
        if ($io->isDecorated()) {
107 1
            $formattedCommand = '<comment>' . $command . '</comment>';
108
        } else {
109
            $formattedCommand = $command;
110
        }
111
112
        // in production mode?
113 1
        if (!$isDevMode) {
114
            $io->write(
115
                sprintf(
116
                    'Skipping %1$s as we are in production mode',
117
                    $formattedCommand
118
                )
119
            );
120
        } else {
121 1
            $io->write(
122 1
                sprintf(
123 1
                    'Running %1$s',
124 1
                    $formattedCommand
125
                )
126
            );
127 1
            passthru($command);
128
        }
129 1
    }
130
}
131