Failed Conditions
Pull Request — master (#632)
by Michael
02:44
created

MigrationStatusInfosHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 89
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getExecutedUnavailableMigrations() 0 3 1
B getFormattedVersionAlias() 0 25 5
B getMigrationsInfos() 0 29 3
A __construct() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Helper;
6
7
use Doctrine\Migrations\Configuration\AbstractFileConfiguration;
8
use Doctrine\Migrations\Configuration\Configuration;
9
use function array_diff;
10
use function count;
11
use function sprintf;
12
13
class MigrationStatusInfosHelper
14
{
15
    /** @var string[] */
16
    private $executedMigrations;
17
18
    /** @var string[] */
19
    private $availableMigrations;
20
21
    /** @var string[] */
22
    private $executedUnavailableMigrations;
23
24
    /** @var Configuration  */
25
    private $configuration;
26
27 9
    public function __construct(Configuration $configuration)
28
    {
29 9
        $this->configuration                 = $configuration;
30 9
        $this->executedMigrations            = $this->configuration->getMigratedVersions();
31 9
        $this->availableMigrations           = $this->configuration->getAvailableVersions();
32 9
        $this->executedUnavailableMigrations = array_diff(
33 9
            $this->executedMigrations,
34 9
            $this->availableMigrations
35
        );
36 9
    }
37
38
    /** @return string[]|int[]|null[] */
39 9
    public function getMigrationsInfos() : array
40
    {
41 9
        $numExecutedUnavailableMigrations = count($this->executedUnavailableMigrations);
42
43 9
        $numNewMigrations = count(array_diff(
44 9
            $this->availableMigrations,
45 9
            $this->executedMigrations
46
        ));
47
48
        $infos = [
49 9
            'Name'                              => $this->configuration->getName() ? $this->configuration->getName() : 'Doctrine Database Migrations',
50 9
            'Database Driver'                   => $this->configuration->getConnection()->getDriver()->getName(),
51 9
            'Database Name'                     => $this->configuration->getConnection()->getDatabase(),
52 9
            'Configuration Source'              => $this->configuration instanceof AbstractFileConfiguration ? $this->configuration->getFile() : 'manually configured',
53 9
            'Version Table Name'                => $this->configuration->getMigrationsTableName(),
54 9
            'Version Column Name'               => $this->configuration->getMigrationsColumnName(),
55 9
            'Migrations Namespace'              => $this->configuration->getMigrationsNamespace(),
56 9
            'Migrations Directory'              => $this->configuration->getMigrationsDirectory(),
57 9
            'Previous Version'                  => $this->getFormattedVersionAlias('prev'),
58 9
            'Current Version'                   => $this->getFormattedVersionAlias('current'),
59 9
            'Next Version'                      => $this->getFormattedVersionAlias('next'),
60 9
            'Latest Version'                    => $this->getFormattedVersionAlias('latest'),
61 9
            'Executed Migrations'               => count($this->executedMigrations),
62 9
            'Executed Unavailable Migrations'   => $numExecutedUnavailableMigrations,
63 9
            'Available Migrations'              => count($this->availableMigrations),
64 9
            'New Migrations'                    => $numNewMigrations,
65
        ];
66
67 9
        return $infos;
68
    }
69
70 9
    private function getFormattedVersionAlias(string $alias) : string
71
    {
72 9
        $version = $this->configuration->resolveVersionAlias($alias);
73
74
        // No version found
75 9
        if ($version === null) {
76 8
            if ($alias === 'next') {
77 3
                return 'Already at latest version';
78
            }
79
80 8
            if ($alias === 'prev') {
81 8
                return 'Already at first version';
82
            }
83
        }
84
85
        // Before first version "virtual" version number
86 9
        if ($version === '0') {
87 8
            return '<comment>0</comment>';
88
        }
89
90
        // Show normal version number
91 9
        return sprintf(
92 9
            '%s (<comment>%s</comment>)',
93 9
            $this->configuration->getDateTime((string) $version),
94 9
            $version
95
        );
96
    }
97
98
    /** @return string[] */
99 1
    public function getExecutedUnavailableMigrations() : array
100
    {
101 1
        return $this->executedUnavailableMigrations;
102
    }
103
}
104