Failed Conditions
Pull Request — master (#707)
by Jonathan
03:12
created

AliasResolver::resolveVersionAlias()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8

Importance

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