Passed
Push — master ( afe250...86c80a )
by Mr
08:24
created

MigrationTrait::toNative()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 14
ccs 0
cts 12
cp 0
crap 6
rs 10
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/dbal project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Dbal\Migration;
10
11
use Assert\Assertion;
12
use Daikon\Dbal\Connector\ConnectorInterface;
13
use Daikon\Interop\RuntimeException;
14
use DateTimeImmutable;
15
use ReflectionClass;
16
17
trait MigrationTrait
18
{
19
    private ?DateTimeImmutable $executedAt;
20
21
    private ConnectorInterface $connector;
22
23
    public function __construct(DateTimeImmutable $executedAt = null)
24
    {
25
        $this->executedAt = $executedAt;
26
    }
27
28
    public function execute(ConnectorInterface $connector, string $direction = MigrationInterface::MIGRATE_UP): void
29
    {
30
        $this->connector = $connector;
31
32
        if ($direction === MigrationInterface::MIGRATE_DOWN) {
33
            Assertion::true($this->isReversible(), 'Migration cannot be reversed');
0 ignored issues
show
Bug introduced by
It seems like isReversible() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
            Assertion::true($this->/** @scrutinizer ignore-call */ isReversible(), 'Migration cannot be reversed');
Loading history...
34
            Assertion::true($this->hasExecuted(), 'Migration has not previously been executed');
35
            $this->down();
0 ignored issues
show
Bug introduced by
It seems like down() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
            $this->/** @scrutinizer ignore-call */ 
36
                   down();
Loading history...
36
            $this->executedAt = null;
37
        } else {
38
            Assertion::false($this->hasExecuted(), 'Migration has already been executed');
39
            $this->up();
0 ignored issues
show
Bug introduced by
It seems like up() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
            $this->/** @scrutinizer ignore-call */ 
40
                   up();
Loading history...
40
            $this->executedAt = new DateTimeImmutable;
41
        }
42
    }
43
44
    public function getName(): string
45
    {
46
        $shortName = (new ReflectionClass(static::class))->getShortName();
47
        if (!preg_match('#^(?<name>.+?)\d+$#', $shortName, $matches)) {
48
            throw new RuntimeException('Unexpected migration name in '.$shortName);
49
        }
50
        return $matches['name'];
51
    }
52
53
    public function getVersion(): int
54
    {
55
        $shortName= (new ReflectionClass(static::class))->getShortName();
56
        if (!preg_match('#(?<version>\d{14})$#', $shortName, $matches)) {
57
            throw new RuntimeException('Unexpected migration version in '.$shortName);
58
        }
59
        return intval($matches['version']);
60
    }
61
62
    public function hasExecuted(): bool
63
    {
64
        return $this->executedAt instanceof DateTimeImmutable;
65
    }
66
67
    public function toNative(): array
68
    {
69
        $state = [
70
            '@type' => static::class,
71
            'name' => $this->getName(),
72
            'version' => $this->getVersion(),
73
            'description' => $this->getDescription()
0 ignored issues
show
Bug introduced by
It seems like getDescription() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            'description' => $this->/** @scrutinizer ignore-call */ getDescription()
Loading history...
74
        ];
75
76
        if ($this->hasExecuted()) {
77
            $state['executedAt']  = $this->executedAt->format('c');
78
        }
79
80
        return $state;
81
    }
82
}
83