Completed
Branch feature/pre-split (67216b)
by Anton
03:28
created

Migration   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 8
lcom 2
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A withCapsule() 0 7 1
A withState() 0 7 1
A getState() 0 8 2
A database() 0 8 2
A table() 0 8 2
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Migrations;
9
10
use Spiral\Database\Entities\Database;
11
use Spiral\Migrations\Exceptions\MigrationException;
12
use Spiral\Migrations\Migration\State;
13
14
/**
15
 * Simple migration class with shortcut for database and blueprint instances.
16
 */
17
abstract class Migration implements MigrationInterface
18
{
19
    /**
20
     * @var State|null
21
     */
22
    private $state = null;
23
24
    /**
25
     * @var MigrationCapsule
26
     */
27
    private $capsule = null;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function withCapsule(CapsuleInterface $capsule): MigrationInterface
33
    {
34
        $migration = clone $this;
35
        $migration->capsule = $capsule;
0 ignored issues
show
Documentation Bug introduced by
$capsule is of type object<Spiral\Migrations\CapsuleInterface>, but the property $capsule was declared to be of type object<Spiral\Migrations\MigrationCapsule>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
36
37
        return $migration;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function withState(State $state): MigrationInterface
44
    {
45
        $migration = clone $this;
46
        $migration->state = $state;
47
48
        return $migration;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getState(): State
55
    {
56
        if (empty($this->state)) {
57
            throw new MigrationException("Unable to get migration state, no state are set");
58
        }
59
60
        return $this->state;
61
    }
62
63
    /**
64
     * @param string $database
65
     *
66
     * @return Database
67
     */
68
    protected function database(string $database = null): Database
69
    {
70
        if (empty($this->capsule)) {
71
            throw new MigrationException("Unable to get database, no capsule are set");
72
        }
73
74
        return $this->capsule->getDatabase($database);
75
    }
76
77
    /**
78
     * Get table schema builder (blueprint).
79
     *
80
     * @param string      $table
81
     * @param string|null $database
82
     *
83
     * @return TableBlueprint
84
     */
85
    protected function table(string $table, string $database = null): TableBlueprint
86
    {
87
        if (empty($this->capsule)) {
88
            throw new MigrationException("Unable to get table blueprint, no capsule are set");
89
        }
90
91
        return new TableBlueprint($this->capsule, $table, $database);
92
    }
93
}