Completed
Push — master ( 19bd7c...c28f13 )
by Asmir
04:59 queued 02:14
created

findMigrationsToExecute()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 11
c 0
b 0
f 0
nc 8
nop 4
dl 0
loc 20
ccs 12
cts 12
cp 1
crap 10
rs 7.6666

How to fix   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\Version;
6
7
use Doctrine\Migrations\Exception\NoMigrationsFoundWithCriteria;
8
use Doctrine\Migrations\Exception\NoMigrationsToExecute;
9
use Doctrine\Migrations\Metadata;
10
use Doctrine\Migrations\Metadata\AvailableMigration;
11
use Doctrine\Migrations\Metadata\MigrationPlan;
12
use Doctrine\Migrations\Metadata\MigrationPlanList;
13
use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
14
use Doctrine\Migrations\MigrationRepository;
15
use function array_map;
16
use function array_reverse;
17
18
/**
19
 * The MigrationPlanCalculator is responsible for calculating the plan for migrating from the current
20
 * version to another version.
21
 *
22
 * @internal
23
 */
24
final class SortedMigrationPlanCalculator implements MigrationPlanCalculator
25
{
26
    /** @var MigrationRepository */
27
    private $migrationRepository;
28
29
    /** @var MetadataStorage */
30
    private $metadataStorage;
31
32 26
    public function __construct(MigrationRepository $migrationRepository, MetadataStorage $metadataStorage)
33
    {
34 26
        $this->migrationRepository = $migrationRepository;
35 26
        $this->metadataStorage     = $metadataStorage;
36 26
    }
37
38 5
    public function getPlanForExactVersion(Version $version, string $direction) : MigrationPlanList
39
    {
40 5
        $migration = $this->migrationRepository->getMigration($version);
41
42 5
        $planItem = new MigrationPlan($migration->getVersion(), $migration->getMigration(), $direction);
43
44 5
        return new MigrationPlanList([$planItem], $direction);
45
    }
46
47 20
    public function getPlanUntilVersion(?Version $to = null) : MigrationPlanList
48
    {
49 20
        $availableMigrations = $this->migrationRepository->getMigrations();
50 20
        $executedMigrations  = $this->metadataStorage->getExecutedMigrations();
51
52
        try {
53 20
            $to = $to ?: $availableMigrations->getLast()->getVersion();
54 1
        } catch (NoMigrationsFoundWithCriteria $e) {
55 1
            throw NoMigrationsToExecute::new($e);
56
        }
57
58 19
        $direction = $this->findDirection($to, $executedMigrations);
59
60 19
        $migrationsToCheck = $this->arrangeMigrationsForDirection($direction, $availableMigrations);
61
62 19
        $toExecute = $this->findMigrationsToExecute($to, $migrationsToCheck, $direction, $executedMigrations);
63
64
        return new MigrationPlanList(array_map(static function (AvailableMigration $migration) use ($direction) {
65 15
            return new MigrationPlan($migration->getVersion(), $migration->getMigration(), $direction);
66 19
        }, $toExecute), $direction);
67
    }
68
69 19
    private function findDirection(Version $to, Metadata\ExecutedMigrationsSet $executedMigrations) : string
70
    {
71 19
        if ((string) $to === '0' || ($executedMigrations->hasMigration($to) && ! $executedMigrations->getLast()->getVersion()->equals($to))) {
72 2
            return Direction::DOWN;
73
        }
74
75 17
        return Direction::UP;
76
    }
77
78
    /**
79
     * @return  AvailableMigration[]
80
     */
81 19
    private function arrangeMigrationsForDirection(string $direction, Metadata\AvailableMigrationsList $availableMigrations) : array
82
    {
83 19
        return $direction === Direction::UP ? $availableMigrations->getItems() : array_reverse($availableMigrations->getItems());
84
    }
85
86
    /**
87
     * @param AvailableMigration[] $migrationsToCheck
88
     *
89
     * @return AvailableMigration[]
90
     */
91 19
    private function findMigrationsToExecute(Version $to, array $migrationsToCheck, string $direction, Metadata\ExecutedMigrationsSet $executedMigrations) : array
92
    {
93 19
        $toExecute = [];
94 19
        foreach ($migrationsToCheck as $availableMigration) {
95 19
            if ($direction === Direction::DOWN && $availableMigration->getVersion()->equals($to)) {
96 1
                break;
97
            }
98
99 19
            if ($direction === Direction::UP && ! $executedMigrations->hasMigration($availableMigration->getVersion())) {
100 13
                $toExecute[] = $availableMigration;
101 8
            } elseif ($direction === Direction::DOWN && $executedMigrations->hasMigration($availableMigration->getVersion())) {
102 2
                $toExecute[] = $availableMigration;
103
            }
104
105 19
            if ($direction === Direction::UP && $availableMigration->getVersion()->equals($to)) {
106 17
                break;
107
            }
108
        }
109
110 19
        return $toExecute;
111
    }
112
}
113