Completed
Push — master ( 178c75...b82d68 )
by Luís
11s
created

MigrationStatusInfosHelper::getMigrationsInfos()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 26
ccs 20
cts 20
cp 1
rs 8.8571
c 2
b 0
f 0
cc 3
eloc 21
nc 4
nop 0
crap 3
1
<?php
2
3
namespace Doctrine\DBAL\Migrations\Tools\Console\Helper;
4
5
6
use Doctrine\DBAL\Migrations\Configuration\AbstractFileConfiguration;
7
use Doctrine\DBAL\Migrations\Configuration\Configuration;
8
use Doctrine\DBAL\Migrations\Version;
9
10
class MigrationStatusInfosHelper
11
{
12
    /** @var Version[] */
13
    private $executedMigrations;
14
15
    /** @var Version[] */
16
    private $availableMigrations;
17
18
    /** @var Version[] */
19
    private $executedUnavailableMigrations;
20
21
    /** @var Configuration  */
22
    private $configuration;
23
24 9
    public function __construct(Configuration $configuration)
25
    {
26 9
        $this->configuration = $configuration;
27 9
        $this->executedMigrations = $this->configuration->getMigratedVersions();
28 9
        $this->availableMigrations = $this->configuration->getAvailableVersions();
29 9
        $this->executedUnavailableMigrations = array_diff($this->executedMigrations, $this->availableMigrations);
30 9
    }
31
32 9
    public function getMigrationsInfos()
33
    {
34 9
        $numExecutedUnavailableMigrations = count($this->executedUnavailableMigrations);
35 9
        $numNewMigrations = count(array_diff($this->availableMigrations, $this->executedMigrations));
36
37
        $infos = [
38 9
            'Name'                              => $this->configuration->getName() ? $this->configuration->getName() : 'Doctrine Database Migrations',
39 9
            'Database Driver'                   => $this->configuration->getConnection()->getDriver()->getName(),
40 9
            'Database Name'                     => $this->configuration->getConnection()->getDatabase(),
41 9
            'Configuration Source'              => $this->configuration instanceof AbstractFileConfiguration ? $this->configuration->getFile() : 'manually configured',
42 9
            'Version Table Name'                => $this->configuration->getMigrationsTableName(),
43 9
            'Version Column Name'               => $this->configuration->getMigrationsColumnName(),
44 9
            'Migrations Namespace'              => $this->configuration->getMigrationsNamespace(),
45 9
            'Migrations Directory'              => $this->configuration->getMigrationsDirectory(),
46 9
            'Previous Version'                  => $this->getFormattedVersionAlias('prev'),
47 9
            'Current Version'                   => $this->getFormattedVersionAlias('current'),
48 9
            'Next Version'                      => $this->getFormattedVersionAlias('next'),
49 9
            'Latest Version'                    => $this->getFormattedVersionAlias('latest'),
50 9
            'Executed Migrations'               => count($this->executedMigrations),
51 9
            'Executed Unavailable Migrations'   => $numExecutedUnavailableMigrations,
52 9
            'Available Migrations'              => count($this->availableMigrations),
53 9
            'New Migrations'                    => $numNewMigrations,
54
        ];
55
56 9
        return $infos;
57
    }
58
59 9
    private function getFormattedVersionAlias($alias)
60
    {
61 9
        $version = $this->configuration->resolveVersionAlias($alias);
62
        //No version found
63 9
        if ($version === null) {
64 8
            if ($alias === 'next') {
65 3
                return 'Already at latest version';
66
            }
67
68 8
            if ($alias === 'prev') {
69 8
                return 'Already at first version';
70
            }
71
        }
72
        //Before first version "virtual" version number
73 9
        if ($version === '0') {
74 8
            return '<comment>0</comment>';
75
        }
76
77
        //Show normal version number
78 9
        return $this->configuration->getDateTime($version) . ' (<comment>' . $version . '</comment>)';
79
    }
80
81
    /** @var Version[] */
82 1
    public function getExecutedUnavailableMigrations()
83
    {
84 1
        return $this->executedUnavailableMigrations;
85
    }
86
}
87