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

AliasResolver::resolveVersionAlias()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8

Importance

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