Completed
Pull Request — master (#138)
by Loïc
02:53
created

InstallDatabaseCommand::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
namespace App\Command\Installer;
4
5
use App\Command\Helper\RunCommands;
6
use App\Installer\Provider\DatabaseSetupCommandsProviderInterface;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class InstallDatabaseCommand extends Command
13
{
14
    use RunCommands {
15
        __construct as private initializeRunCommands;
16
    }
17
18
    /**
19
     * @var DatabaseSetupCommandsProviderInterface
20
     */
21
    private $databaseSetupCommandsProvider;
22
23
    /**
24
     * @var EntityManagerInterface
25
     */
26
    private $entityManager;
27
28
    /**
29
     * @var string
30
     */
31
    private $environment;
32
33
    /**
34
     * @var CommandExecutor
35
     */
36
    private $commandExecutor;
37
38
    /**
39
     * @param DatabaseSetupCommandsProviderInterface $databaseSetupCommandsProvider
40
     * @param EntityManagerInterface                 $entityManager
41
     * @param string                                 $environment
42
     */
43
    public function __construct(
44
        DatabaseSetupCommandsProviderInterface $databaseSetupCommandsProvider,
45
        EntityManagerInterface $entityManager,
46
        string $environment
47
    ) {
48
        $this->databaseSetupCommandsProvider = $databaseSetupCommandsProvider;
49
        $this->entityManager = $entityManager;
50
        $this->environment = $environment;
51
52
        parent::__construct();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function initialize(InputInterface $input, OutputInterface $output)
59
    {
60
        $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...
61
        $this->initializeRunCommands($this->commandExecutor, $this->entityManager);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function configure()
68
    {
69
        $this
70
            ->setName('app:install:database')
71
            ->setDescription('Install AppName database.')
72
            ->setHelp(<<<EOT
73
The <info>%command.name%</info> command creates AppName database.
74
EOT
75
            )
76
        ;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    protected function execute(InputInterface $input, OutputInterface $output)
83
    {
84
        $output->writeln(sprintf('Creating AppName database for environment <info>%s</info>.', $this->environment));
85
86
        $commands = $this->databaseSetupCommandsProvider->getCommands($input, $output, $this->getHelper('question'));
0 ignored issues
show
Compatibility introduced by
$this->getHelper('question') of type object<Symfony\Component...Helper\HelperInterface> is not a sub-type of object<Symfony\Component...\Helper\QuestionHelper>. It seems like you assume a concrete implementation of the interface Symfony\Component\Console\Helper\HelperInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
87
88
        $this->runCommands($commands, $output);
89
        $output->writeln('');
90
91
        $this->commandExecutor->runCommand('app:install:sample-data', [], $output);
92
    }
93
}
94