InstallCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 5
dl 0
loc 127
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 10 1
A initialize() 0 4 1
A execute() 0 30 3
A getProperFinalMessage() 0 8 2
A getLogo() 0 18 1
1
<?php
2
3
namespace App\Command\Installer;
4
5
use App\Command\Helper\DirectoryChecker;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Style\SymfonyStyle;
10
use Symfony\Component\Process\Exception\RuntimeException;
11
12
class InstallCommand extends Command
13
{
14
    /** @var DirectoryChecker */
15
    private $directoryChecker;
16
17
    /** @var string */
18
    private $cacheDir;
19
20
    /** @var CommandExecutor */
21
    private $commandExecutor;
22
23
    /** @var array */
24
    private $commands = [
25
        [
26
            'command' => 'database',
27
            'message' => 'Setting up the database.',
28
        ],
29
        [
30
            'command' => 'setup',
31
            'message' => 'Website configuration.',
32
        ],
33
        [
34
            'command' => 'assets',
35
            'message' => 'Installing assets.',
36
        ],
37
    ];
38
39
    public function __construct(DirectoryChecker $directoryChecker, string $cacheDir)
40
    {
41
        $this->directoryChecker = $directoryChecker;
42
        $this->cacheDir = $cacheDir;
43
44
        parent::__construct();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function configure()
51
    {
52
        $this
53
            ->setName('app:install')
54
            ->setDescription('Installs AppName in your preferred environment.')
55
            ->setHelp(<<<EOT
56
The <info>%command.name%</info> command installs AppName.
57
EOT
58
            );
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected function initialize(InputInterface $input, OutputInterface $output)
65
    {
66
        $this->commandExecutor = new CommandExecutor($input, $output, $this->getApplication());
0 ignored issues
show
Bug introduced by
It seems like $this->getApplication() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     *
72
     * @throws \Exception
73
     */
74
    protected function execute(InputInterface $input, OutputInterface $output)
75
    {
76
        $outputStyle = new SymfonyStyle($input, $output);
77
        $outputStyle->writeln('<info>Installing AppName...</info>');
78
        $outputStyle->writeln($this->getLogo());
79
80
        $this->directoryChecker->ensureDirectoryExistsAndIsWritable($this->cacheDir, $output, $this->getName());
81
82
        $errored = false;
83
        foreach ($this->commands as $step => $command) {
84
            try {
85
                $outputStyle->newLine();
86
                $outputStyle->section(sprintf(
87
                    'Step %d of %d. <info>%s</info>',
88
                    $step + 1,
89
                    count($this->commands),
90
                    $command['message']
91
                ));
92
                $this->commandExecutor->runCommand('app:install:'.$command['command'], [], $output);
93
                $output->writeln('');
94
            } catch (RuntimeException $exception) {
95
                $errored = true;
96
            }
97
        }
98
99
        $output->writeln($this->getProperFinalMessage($errored));
100
        $output->writeln('You can now open your website at the following path under the website root.');
101
102
        return 0;
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    private function getProperFinalMessage($errored)
109
    {
110
        if ($errored) {
111
            return '<info>AppName has been installed, but some error occurred.</info>';
112
        }
113
114
        return '<info>AppName has been successfully installed.</info>';
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    private function getLogo()
121
    {
122
        return '
123
        
124
$$\      $$\                                $$$$$$\                               
125
$$$\    $$$ |                              $$  __$$\                              
126
$$$$\  $$$$ | $$$$$$\  $$$$$$$\   $$$$$$\  $$ /  \__|$$$$$$\  $$$$$$$\  $$\   $$\ 
127
$$\$$\$$ $$ |$$  __$$\ $$  __$$\ $$  __$$\ $$$$\    $$  __$$\ $$  __$$\ $$ |  $$ |
128
$$ \$$$  $$ |$$ /  $$ |$$ |  $$ |$$ /  $$ |$$  _|   $$ /  $$ |$$ |  $$ |$$ |  $$ |
129
$$ |\$  /$$ |$$ |  $$ |$$ |  $$ |$$ |  $$ |$$ |     $$ |  $$ |$$ |  $$ |$$ |  $$ |
130
$$ | \_/ $$ |\$$$$$$  |$$ |  $$ |\$$$$$$  |$$ |     \$$$$$$  |$$ |  $$ |\$$$$$$$ |
131
\__|     \__| \______/ \__|  \__| \______/ \__|      \______/ \__|  \__| \____$$ |
132
                                                                        $$\   $$ |
133
                                                                        \$$$$$$  |
134
                                                                         \______/ 
135
                                                                         
136
        ';
137
    }
138
}
139