InstallSampleDataCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
nc 1
cc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of AppName.
5
 *
6
 * (c) Monofony
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace App\Command\Installer;
15
16
use App\Command\Helper\CommandsRunner;
17
use App\Command\Helper\DirectoryChecker;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Helper\QuestionHelper;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Console\Question\ConfirmationQuestion;
23
use Symfony\Component\Console\Style\SymfonyStyle;
24
25
final class InstallSampleDataCommand extends Command
26
{
27
    /** @var DirectoryChecker */
28
    private $directoryChecker;
29
30
    /** @var CommandsRunner */
31
    private $commandsRunner;
32
33
    /** @var string */
34
    private $publicDir;
35
36
    /** @var string */
37
    private $environment;
38
39
    public function __construct(
40
        DirectoryChecker $directoryChecker,
41
        CommandsRunner $commandsRunner,
42
        string $publicDir,
43
        string $environment
44
    ) {
45
        $this->directoryChecker = $directoryChecker;
46
        $this->commandsRunner = $commandsRunner;
47
        $this->publicDir = $publicDir;
48
        $this->environment = $environment;
49
50
        parent::__construct();
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function configure()
57
    {
58
        $this
59
            ->setName('app:install:sample-data')
60
            ->setDescription('Install sample data into AppName.')
61
            ->setHelp(<<<EOT
62
The <info>%command.name%</info> command loads the sample data for AppName.
63
EOT
64
            )
65
        ;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected function execute(InputInterface $input, OutputInterface $output)
72
    {
73
        /** @var QuestionHelper $questionHelper */
74
        $questionHelper = $this->getHelper('question');
75
76
        $outputStyle = new SymfonyStyle($input, $output);
77
        $outputStyle->newLine();
78
        $outputStyle->writeln(sprintf(
79
            'Loading sample data for environment <info>%s</info>.',
80
            $this->environment
81
        ));
82
83
        $outputStyle->writeln('<error>Warning! This action will erase your database.</error>');
84
85
        if (!$questionHelper->ask($input, $output, new ConfirmationQuestion('Continue? (y/N) ', false))) {
86
            $outputStyle->writeln('Cancelled loading sample data.');
87
88
            return 0;
89
        }
90
91
        try {
92
            $this->directoryChecker->ensureDirectoryExistsAndIsWritable($this->publicDir.'/media/', $output, $this->getName());
93
            $this->directoryChecker->ensureDirectoryExistsAndIsWritable($this->publicDir.'/media/image/', $output, $this->getName());
94
        } catch (\RuntimeException $exception) {
95
            $outputStyle->writeln($exception->getMessage());
96
97
            return 1;
98
        }
99
100
        $commands = [
101
            'sylius:fixtures:load' => ['--no-interaction' => true],
102
        ];
103
104
        $this->commandsRunner->run($commands, $input, $output, $this->getApplication());
0 ignored issues
show
Bug introduced by
It seems like $this->getApplication() can be null; however, run() 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...
105
        $outputStyle->newLine(2);
106
107
        return 0;
108
    }
109
}
110