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

getFormattedVersionAlias()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 1
dl 0
loc 25
ccs 13
cts 13
cp 1
crap 5
rs 9.2088
c 0
b 0
f 0
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