Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

MigrationUpCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Apps\Console;
4
5
6
use Ffcms\Console\Command;
7
use Ffcms\Core\Helper\Type\Any;
8
use Ffcms\Core\Managers\MigrationsManager;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * Class MigrationApplyCommand. Apply all migrations in db
15
 * @package Apps\Console
16
 */
17
class MigrationUpCommand extends Command
18
{
19
    /**
20
     * Set command
21
     */
22
    public function configure()
23
    {
24
        $this->setName('migration:up')
25
            ->setDescription('Apply all migrations to database')
26
            ->addArgument('name', InputArgument::OPTIONAL, 'Set search name or date (Y-m-d-H-i-s) for migration file');
27
    }
28
29
    /**
30
     * Apply all migrations into database without always apply'd
31
     * @param InputInterface $input
32
     * @param OutputInterface $output
33
     * @return void
34
     */
35
    public function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        $name = $input->getArgument('name');
38
        // run migration manager - find migrations
39
        $manager = new MigrationsManager(null, $this->dbConnection);
40
        $search = $manager->search($name, false);
0 ignored issues
show
Bug introduced by zenn
It seems like $name can also be of type string[]; however, parameter $query of Ffcms\Core\Managers\MigrationsManager::search() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        $search = $manager->search(/** @scrutinizer ignore-type */ $name, false);
Loading history...
41
        if (!Any::isArray($search) || count($search) < 1) {
0 ignored issues
show
Bug introduced by zenn
$search of type false is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        if (!Any::isArray($search) || count(/** @scrutinizer ignore-type */ $search) < 1) {
Loading history...
42
            $output->writeln('No migrations found');
43
            return;
44
        }
45
46
        // require confirmation from user each ever migration file
47
        $fired = false;
48
        foreach ($search as $migration) {
0 ignored issues
show
Bug introduced by zenn
The expression $search of type false is not traversable.
Loading history...
49
            if (!$this->confirm('Are you sure to apply migration: ' . $migration, true)) {
50
                continue;
51
            }
52
            $manager->makeUp($migration);
53
            $fired =  true;
54
        }
55
56
        if ($fired) {
57
            $output->writeln('All available migrations applied. Done.');
58
        } else {
59
            $output->writeln('No migrations executed.');
60
        }
61
    }
62
}