Passed
Push — master ( 229462...d74785 )
by Andreas
02:20 queued 14s
created

MigrationStatusInfosHelper::showMigrationsInfo()   C

Complexity

Conditions 10
Paths 160

Size

Total Lines 84
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 10.0077

Importance

Changes 0
Metric Value
cc 10
eloc 51
nc 160
nop 5
dl 0
loc 84
ccs 45
cts 47
cp 0.9574
crap 10.0077
rs 6.8024
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Helper;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\Migrations\Configuration\Configuration;
9
use Doctrine\Migrations\Metadata\AvailableMigrationsList;
10
use Doctrine\Migrations\Metadata\ExecutedMigrationsSet;
11
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
12
use Doctrine\Migrations\Version\AliasResolver;
13
use Symfony\Component\Console\Helper\Table;
14
use Symfony\Component\Console\Helper\TableCell;
15
use Symfony\Component\Console\Helper\TableSeparator;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Throwable;
18
use function array_unshift;
19
use function count;
20
use function get_class;
21
use function sprintf;
22
23
/**
24
 * The MigrationStatusInfosHelper class is responsible for building the array of information used when displaying
25
 * the status of your migrations.
26
 *
27
 * @internal
28
 *
29
 * @see Doctrine\Migrations\Tools\Console\Command\StatusCommand
30
 */
31
class MigrationStatusInfosHelper
32
{
33
    /** @var Configuration */
34
    private $configuration;
35
36
    /** @var Connection */
37
    private $connection;
38
39
    /** @var AliasResolver */
40
    private $aliasResolver;
41
42 2
    public function __construct(
43
        Configuration $configuration,
44
        Connection $connection,
45
        AliasResolver $aliasResolver
46
    ) {
47 2
        $this->configuration = $configuration;
48 2
        $this->connection    = $connection;
49 2
        $this->aliasResolver = $aliasResolver;
50 2
    }
51
52 2
    public function showMigrationsInfo(
53
        OutputInterface $output,
54
        AvailableMigrationsList $availableMigrations,
55
        ExecutedMigrationsSet $executedMigrations,
56
        AvailableMigrationsList $newMigrations,
57
        ExecutedMigrationsSet $executedUnavailableMigrations
58
    ) : void {
59 2
        $storage = $this->configuration->getMetadataStorageConfiguration();
60
61 2
        $table = new Table($output);
62 2
        $table->setHeaders(
63
            [
64 2
                [new TableCell('Configuration', ['colspan' => 3])],
65
            ]
66
        );
67
        $data = [
68 2
            'Project' => $this->configuration->getName() ?? 'Doctrine Database Migrations',
69
        ];
70 2
        foreach ($data as $k => $v) {
71 2
            $table->addRow([
72 2
                '<info>' . $k . '</info>',
73 2
                new TableCell($v, ['colspan' => 2]),
74
            ]);
75
        }
76
        $dataGroup = [
77 2
            'Storage' => [
78 2
                'Type' => $storage!== null ? get_class($storage) : null,
79
            ],
80
            'Database' => [
81 2
                'Driver' => $this->connection->getDriver()->getName(),
82 2
                'Host' => $this->connection->getHost(),
83 2
                'Name' => $this->connection->getDatabase(),
84
            ],
85
            'Versions' => [
86 2
                'Previous' => $this->getFormattedVersionAlias('prev', $executedMigrations),
87 2
                'Current' => $this->getFormattedVersionAlias('current', $executedMigrations),
88 2
                'Next' => $this->getFormattedVersionAlias('next', $executedMigrations),
89 2
                'Latest' => $this->getFormattedVersionAlias('latest', $executedMigrations),
90
            ],
91
92
            'Migrations' => [
93 2
                'Executed' => count($executedMigrations),
94 2
                'Executed Unavailable' => count($executedUnavailableMigrations) > 0 ? ('<error>' . count($executedUnavailableMigrations) . '</error>') : '0',
95 2
                'Available' => count($availableMigrations),
96 2
                'New' => count($newMigrations) > 0 ? ('<question>' . count($newMigrations) . '</question>') : '0',
97
            ],
98 2
            'Migration Namespaces' => $this->configuration->getMigrationDirectories(),
99
100
        ];
101 2
        if ($storage instanceof TableMetadataStorageConfiguration) {
102
            $dataGroup['Storage'] += [
103 2
                'Table Name' => $storage->getTableName(),
104 2
                'Column Name' => $storage->getVersionColumnName(),
105
            ];
106 2
            $table->addRow([new TableSeparator(['colspan' => 3])]);
107 2
            foreach ($data as $k => $v) {
108 2
                $table->addRow([
109 2
                    '<info>' . $k . '</info>',
110 2
                    new TableCell($v, ['colspan' => 2]),
111
                ]);
112
            }
113
        }
114
115 2
        foreach ($dataGroup as $group => $dataValues) {
116 2
            $nsRows = [];
117 2
            foreach ($dataValues as $k => $v) {
118 2
                $nsRows[] = [
119 2
                    $k,
120 2
                    $v,
121
                ];
122
            }
123 2
            if (count($nsRows) <= 0) {
124
                continue;
125
            }
126
127 2
            $table->addRow([new TableSeparator(['colspan' => 3])]);
128 2
            array_unshift(
129 2
                $nsRows[0],
130 2
                new TableCell('<info>' . $group . '</info>', ['rowspan' => count($dataValues)])
131
            );
132 2
            $table->addRows($nsRows);
133
        }
134
135 2
        $table->render();
136 2
    }
137
138 2
    private function getFormattedVersionAlias(string $alias, ExecutedMigrationsSet $executedMigrationsSet) : string
139
    {
140
        try {
141 2
            $version = $this->aliasResolver->resolveVersionAlias($alias);
142 1
        } catch (Throwable $e) {
143 1
            $version = null;
144
        }
145
146
        // No version found
147 2
        if ($version === null) {
148 1
            if ($alias === 'next') {
149 1
                return 'Already at latest version';
150
            }
151
152 1
            if ($alias === 'prev') {
153
                return 'Already at first version';
154
            }
155
        }
156 2
        if ($alias === 'latest' && $version!== null && $executedMigrationsSet->hasMigration($version)) {
157
            return 'Already at latest version';
158
        }
159
        // Before first version "virtual" version number
160 2
        if ((string) $version === '0') {
161
            return '<comment>0</comment>';
162
        }
163
164
        // Show normal version number
165 2
        return sprintf(
166 2
            '<comment>%s </comment>',
167 2
            (string) $version
168
        );
169
    }
170
}
171