Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractMigration
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
1 / 1
 runMigration
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
5
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Dev Commands Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\CLI\Commands;
11
12use Framework\CLI\CLI;
13use Framework\CLI\Command;
14use Framework\MVC\App;
15
16/**
17 * Class AbstractMigration.
18 *
19 * @package dev-commands
20 */
21abstract class AbstractMigration extends Command
22{
23    protected string $migratorInstance = 'default';
24
25    protected function runMigration(string $direction, int | string $arg = null) : void
26    {
27        // @phpstan-ignore-next-line
28        $this->migratorInstance = $this->getConsole()->getOption('instance') ?? 'default';
29        $direction = \ucfirst(\strtolower($direction));
30        $arg ??= $this->getConsole()->getArgument(0);
31        if ($direction !== 'To') {
32            $arg = (int) $arg;
33        }
34        $method = 'migrate' . $direction;
35        CLI::write(
36            CLI::style('Migrator Instance:', CLI::FG_YELLOW, formats: [CLI::FM_BOLD])
37            . ' ' . $this->migratorInstance
38        );
39        CLI::newLine();
40        $count = 0;
41        $time = $start = \microtime(true);
42        // @phpstan-ignore-next-line
43        foreach (App::migrator($this->migratorInstance)->{$method}($arg) as $item) {
44            CLI::write('- Migrated to ' . CLI::style($item, CLI::FG_GREEN)
45                . ' in ' . CLI::style((string) \round(\microtime(true) - $time, 6), CLI::FG_YELLOW) . ' seconds.');
46            $time = \microtime(true);
47            $count++;
48        }
49        if ($count) {
50            CLI::newLine();
51            CLI::write('Ran ' . $count . ' migration' . ($count !== 1 ? 's' : '')
52                . ' in ' . \round(\microtime(true) - $start, 6) . ' seconds.');
53            return;
54        }
55        CLI::write('Did not run any migration.');
56    }
57}