Completed
Push — master ( 58b4f5...228133 )
by Mike
07:57 queued 05:12
created

getFormattedVersionAlias()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0188

Importance

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