Passed
Pull Request — master (#739)
by Michael
02:38
created

MigrationStatusInfosHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 72
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

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