Passed
Push — master ( 0a3787...bc905b )
by Mihail
06:05
created

Apps/Console/MigrationDownCommand.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Apps\Console;
4
5
6
use Apps\ActiveRecord\Migration;
7
use Ffcms\Console\Command;
8
use Ffcms\Core\Helper\FileSystem\File;
9
use Ffcms\Core\Helper\Type\Any;
10
use Ffcms\Core\Helper\Type\Obj;
11
use Ffcms\Core\Helper\Type\Str;
12
use Ffcms\Core\Managers\MigrationsManager;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Class MigrationRevertCommand. Revert installed migrations
19
 * @package Apps\Console
20
 */
21
class MigrationDownCommand extends Command
22
{
23
    /**
24
     * Register command
25
     */
26
    public function configure()
27
    {
28
        $this->setName('migration:down')
29
            ->setDescription('Revert single migration by name or date or search query')
30
            ->addArgument('name', InputArgument::OPTIONAL, 'Migration name or date(Y-m-d format) to find all installed migrations');
31
    }
32
33
    /**
34
     * Search all installed migrations by passed argument and ask to revert it
35
     * @param InputInterface $input
36
     * @param OutputInterface $output
37
     * @return void
38
     */
39
    public function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        // get search migration name
42
        $name = $input->getArgument('name');
43
        // initialize migration manager
44
        $manager = new MigrationsManager(null, $this->dbConnection);
45
        $search = $manager->search($name, true);
0 ignored issues
show
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

45
        $search = $manager->search(/** @scrutinizer ignore-type */ $name, true);
Loading history...
46
        if (!Any::isArray($search) || count($search) < 1) {
47
            $output->writeln('No migrations found');
48
            return;
49
        }
50
51
        // list found migrations and ask to revert each one
52
        $fired = false;
53
        foreach ($search as $migration) {
54
            if (!$this->confirm('Are you sure to revert: ' . $migration)) {
55
                continue;
56
            }
57
            // run down migration
58
            $manager->makeDown($migration);
59
            $fired = true;
60
        }
61
62
        // check if anyone executed
63
        if ($fired) {
64
            $output->writeln('Migrations are successful revert');
65
        } else {
66
            $output->writeln('No migrations to revert');
67
        }
68
    }
69
}