Passed
Push — master ( 10a192...087136 )
by Aleksei
07:38 queued 05:42
created

Migration::database()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license MIT
7
 * @author  Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Migrations;
13
14
use Cycle\Database\Database;
15
use Cycle\Database\DatabaseInterface;
16
use Cycle\Migrations\Exception\MigrationException;
17
18
/**
19
 * Simple migration class with shortcut for database and blueprint instances.
20
 */
21
abstract class Migration implements MigrationInterface
22
{
23
    // Target migration database
24
    protected const DATABASE = null;
25
26
    /** @var State|null */
27
    private $state;
28
29
    /** @var CapsuleInterface */
30
    private $capsule;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getDatabase(): ?string
36
    {
37
        return static::DATABASE;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function withCapsule(CapsuleInterface $capsule): MigrationInterface
44
    {
45
        $migration = clone $this;
46
        $migration->capsule = $capsule;
47
48
        return $migration;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function withState(State $state): MigrationInterface
55
    {
56
        $migration = clone $this;
57
        $migration->state = $state;
58
59
        return $migration;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getState(): State
66
    {
67
        if (empty($this->state)) {
68
            throw new MigrationException('Unable to get migration state, no state are set');
69
        }
70
71
        return $this->state;
72
    }
73
74
    /**
75
     * @return Database
76
     */
77
    protected function database(): DatabaseInterface
78
    {
79
        if (empty($this->capsule)) {
80
            throw new MigrationException('Unable to get database, no capsule are set');
81
        }
82
83
        return $this->capsule->getDatabase();
84
    }
85
86
    /**
87
     * Get table schema builder (blueprint).
88
     *
89
     * @param string $table
90
     *
91
     * @return TableBlueprint
92
     */
93
    protected function table(string $table): TableBlueprint
94
    {
95
        if (empty($this->capsule)) {
96
            throw new MigrationException('Unable to get table blueprint, no capsule are set');
97
        }
98
99
        return new TableBlueprint($this->capsule, $table);
100
    }
101
}
102