Completed
Push — master ( 368556...6bf94b )
by Jonathan
11s
created

ations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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