Failed Conditions
Push — master ( 51f284...368556 )
by Jonathan
02:26
created

VersionAliasResolver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C resolveVersionAlias() 0 28 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations;
6
7
use function substr;
8
9
/**
10
 * @internal
11
 */
12
final class VersionAliasResolver
13
{
14
    private const ALIAS_FIRST   = 'first';
15
    private const ALIAS_CURRENT = 'current';
16
    private const ALIAS_PREV    = 'prev';
17
    private const ALIAS_NEXT    = 'next';
18
    private const ALIAS_LATEST  = 'latest';
19
20
    /** @var MigrationRepository */
21
    private $migrationRepository;
22
23 17
    public function __construct(MigrationRepository $migrationRepository)
24
    {
25 17
        $this->migrationRepository = $migrationRepository;
26 17
    }
27
28
    /**
29
     * Returns the version number from an alias.
30
     *
31
     * Supported aliases are:
32
     *
33
     * - first: The very first version before any migrations have been run.
34
     * - current: The current version.
35
     * - prev: The version prior to the current version.
36
     * - next: The version following the current version.
37
     * - latest: The latest available version.
38
     *
39
     * If an existing version number is specified, it is returned verbatimly.
40
     */
41 17
    public function resolveVersionAlias(string $alias) : ?string
42
    {
43 17
        if ($this->migrationRepository->hasVersion($alias)) {
44 2
            return $alias;
45
        }
46
47
        switch ($alias) {
48 16
            case self::ALIAS_FIRST:
49 2
                return '0';
50
51 15
            case self::ALIAS_CURRENT:
52 10
                return $this->migrationRepository->getCurrentVersion();
53
54 14
            case self::ALIAS_PREV:
55 10
                return $this->migrationRepository->getPrevVersion();
56
57 13
            case self::ALIAS_NEXT:
58 10
                return $this->migrationRepository->getNextVersion();
59
60 12
            case self::ALIAS_LATEST:
61 10
                return $this->migrationRepository->getLatestVersion();
62
63
            default:
64 3
                if (substr($alias, 0, 7) === self::ALIAS_CURRENT) {
65 1
                    return $this->migrationRepository->getDeltaVersion(substr($alias, 7));
66
                }
67
68 2
                return null;
69
        }
70
    }
71
}
72