Completed
Pull Request — master (#138)
by Loïc
04:36 queued 01:35
created

InstallCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace App\Command\Installer;
4
5
use App\Command\Helper\EnsureDirectoryExistsAndIsWritable;
6
use App\Installer\Checker\CommandDirectoryChecker;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Process\Exception\RuntimeException;
11
12
class InstallCommand extends Command
13
{
14
    use EnsureDirectoryExistsAndIsWritable {
15
        __construct as private initializeEnsureDirectoryExistsAndIsWritable;
16
    }
17
18
    /**
19
     * @var CommandDirectoryChecker
20
     */
21
    private $commandDirectoryChecker;
22
23
    /**
24
     * @var string
25
     */
26
    private $cacheDir;
27
28
    /**
29
     * @var CommandExecutor
30
     */
31
    private $commandExecutor;
32
33
    /**
34
     * @var array
35
     */
36
    private $commands = [
37
        [
38
            'command' => 'database',
39
            'message' => 'Setting up the database.',
40
        ],
41
        [
42
            'command' => 'assets',
43
            'message' => 'Installing assets.',
44
        ],
45
    ];
46
47
    /**
48
     * @param CommandDirectoryChecker $commandDirectoryChecker
49
     * @param string                  $cacheDir
50
     */
51
    public function __construct(CommandDirectoryChecker $commandDirectoryChecker, string $cacheDir)
52
    {
53
        $this->commandDirectoryChecker = $commandDirectoryChecker;
54
        $this->cacheDir = $cacheDir;
55
56
        parent::__construct();
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function initialize(InputInterface $input, OutputInterface $output)
63
    {
64
        $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...
65
66
        $this->initializeEnsureDirectoryExistsAndIsWritable($this->commandDirectoryChecker, $this->getName());
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function configure()
73
    {
74
        $this
75
            ->setName('app:install')
76
            ->setDescription('Installs AppName in your preferred environment.')
77
            ->setHelp(<<<EOT
78
The <info>%command.name%</info> command installs AppName.
79
EOT
80
            );
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected function execute(InputInterface $input, OutputInterface $output)
87
    {
88
        $output->writeln('<info>Installing AppName...</info>');
89
        $output->writeln($this->getLogo());
90
91
        $this->ensureDirectoryExistsAndIsWritable($this->cacheDir, $output);
92
93
        $errored = false;
94
        foreach ($this->commands as $step => $command) {
95
            try {
96
                $output->writeln(sprintf('<comment>Step %d of %d.</comment> <info>%s</info>', $step + 1, count($this->commands), $command['message']));
97
                $this->commandExecutor->runCommand('app:install:'.$command['command'], [], $output);
98
                $output->writeln('');
99
            } catch (RuntimeException $exception) {
100
                $errored = true;
101
            }
102
        }
103
104
        $output->writeln($this->getProperFinalMessage($errored));
105
        $output->writeln('You can now open your website at the following path under the website root.');
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    private function getProperFinalMessage($errored)
112
    {
113
        if ($errored) {
114
            return '<info>AppName has been installed, but some error occurred.</info>';
115
        }
116
117
        return '<info>AppName has been successfully installed.</info>';
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    private function getLogo()
124
    {
125
        return '
126
        
127
$$\      $$\                                $$$$$$\                               
128
$$$\    $$$ |                              $$  __$$\                              
129
$$$$\  $$$$ | $$$$$$\  $$$$$$$\   $$$$$$\  $$ /  \__|$$$$$$\  $$$$$$$\  $$\   $$\ 
130
$$\$$\$$ $$ |$$  __$$\ $$  __$$\ $$  __$$\ $$$$\    $$  __$$\ $$  __$$\ $$ |  $$ |
131
$$ \$$$  $$ |$$ /  $$ |$$ |  $$ |$$ /  $$ |$$  _|   $$ /  $$ |$$ |  $$ |$$ |  $$ |
132
$$ |\$  /$$ |$$ |  $$ |$$ |  $$ |$$ |  $$ |$$ |     $$ |  $$ |$$ |  $$ |$$ |  $$ |
133
$$ | \_/ $$ |\$$$$$$  |$$ |  $$ |\$$$$$$  |$$ |     \$$$$$$  |$$ |  $$ |\$$$$$$$ |
134
\__|     \__| \______/ \__|  \__| \______/ \__|      \______/ \__|  \__| \____$$ |
135
                                                                        $$\   $$ |
136
                                                                        \$$$$$$  |
137
                                                                         \______/ 
138
                                                                         
139
        ';
140
    }
141
}
142