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

InstallSampleDataCommand::configure()   A

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\EnsureDirectoryExistsAndIsWritable;
17
use App\Command\Helper\RunCommands;
18
use App\Installer\Checker\CommandDirectoryChecker;
19
use Doctrine\ORM\EntityManagerInterface;
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Helper\QuestionHelper;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Symfony\Component\Console\Question\ConfirmationQuestion;
25
use Symfony\Component\Console\Style\SymfonyStyle;
26
27
final class InstallSampleDataCommand extends Command
28
{
29
    const WEB_MEDIA_DIRECTORY = 'public/media/';
30
    const WEB_MEDIA_IMAGE_DIRECTORY = 'public/media/image/';
31
32
    use EnsureDirectoryExistsAndIsWritable {
33
        EnsureDirectoryExistsAndIsWritable::__construct as private initializeEnsureDirectoryExistsAndIsWritable;
34
    }
35
    use RunCommands {
36
        RunCommands::__construct as private initializeRunCommands;
37
    }
38
39
    /**
40
     * @var CommandDirectoryChecker
41
     */
42
    private $commandDirectoryChecker;
43
44
    /**
45
     * @var EntityManagerInterface
46
     */
47
    private $entityManager;
48
49
    /**
50
     * @var string
51
     */
52
    private $rootDir;
53
54
    /**
55
     * @var string
56
     */
57
    private $environment;
58
59
    /**
60
     * @param CommandDirectoryChecker $commandDirectoryChecker
61
     * @param EntityManagerInterface  $entityManager
62
     * @param string                  $rootDir
63
     * @param string                  $environment
64
     */
65
    public function __construct(CommandDirectoryChecker $commandDirectoryChecker, EntityManagerInterface $entityManager, string $rootDir, string $environment)
66
    {
67
        $this->commandDirectoryChecker = $commandDirectoryChecker;
68
        $this->entityManager = $entityManager;
69
        $this->rootDir = $rootDir;
70
        $this->environment = $environment;
71
72
        parent::__construct();
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function configure()
79
    {
80
        $this
81
            ->setName('app:install:sample-data')
82
            ->setDescription('Install sample data into AppName.')
83
            ->setHelp(<<<EOT
84
The <info>%command.name%</info> command loads the sample data for AppName.
85
EOT
86
            )
87
        ;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected function initialize(InputInterface $input, OutputInterface $output)
94
    {
95
        $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...
96
97
        $this->initializeEnsureDirectoryExistsAndIsWritable($this->commandDirectoryChecker, $this->getName());
98
        $this->initializeRunCommands($commandExecutor, $this->entityManager);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    protected function execute(InputInterface $input, OutputInterface $output)
105
    {
106
        /** @var QuestionHelper $questionHelper */
107
        $questionHelper = $this->getHelper('question');
108
109
        $outputStyle = new SymfonyStyle($input, $output);
110
        $outputStyle->newLine();
111
        $outputStyle->writeln(sprintf(
112
            'Loading sample data for environment <info>%s</info>.',
113
            $this->environment
114
        ));
115
        $outputStyle->writeln('<error>Warning! This action will erase your database.</error>');
116
117
        if (!$questionHelper->ask($input, $output, new ConfirmationQuestion('Continue? (y/N) ', false))) {
118
            $outputStyle->writeln('Cancelled loading sample data.');
119
120
            return 0;
121
        }
122
123
        try {
124
            $rootDir = $this->rootDir.'/../';
125
            $this->ensureDirectoryExistsAndIsWritable($rootDir.self::WEB_MEDIA_DIRECTORY, $output);
126
            $this->ensureDirectoryExistsAndIsWritable($rootDir.self::WEB_MEDIA_IMAGE_DIRECTORY, $output);
127
        } catch (\RuntimeException $exception) {
128
            $outputStyle->writeln($exception->getMessage());
129
130
            return 1;
131
        }
132
133
        $commands = [
134
            'sylius:fixtures:load' => ['--no-interaction' => true],
135
        ];
136
137
        $this->runCommands($commands, $output);
138
        $outputStyle->newLine(2);
139
    }
140
}
141