Completed
Branch feature/pre-split (e801ec)
by Anton
03:11
created

Migration::table()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
rs 9.4285
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 $status = 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->status = $state;
47
48
        return $migration;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getState()
55
    {
56
        return $this->status;
57
    }
58
59
    /**
60
     * @param string $database
61
     *
62
     * @return Database
63
     */
64
    public function database(string $database = null): Database
65
    {
66
        if (!empty($this->capsule)) {
67
            throw new MigrationException("Unable to get database, no capsule are set");
68
        }
69
70
        return $this->capsule->getDatabase($database);
71
    }
72
73
    /**
74
     * Get table schema builder (blueprint).
75
     *
76
     * @param string      $table
77
     * @param string|null $database
78
     *
79
     * @return TableBlueprint
80
     */
81
    public function table(string $table, string $database = null): TableBlueprint
82
    {
83
        if (!empty($this->capsule)) {
84
            throw new MigrationException("Unable to get table blueprint, no capsule are set");
85
        }
86
87
        return new TableBlueprint($this->capsule, $database, $table);
88
    }
89
}