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

Alter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 11
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 5
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\Operation\ForeignKey;
13
14
use Cycle\Database\ForeignKeyInterface;
15
use Cycle\Migrations\CapsuleInterface;
16
use Cycle\Migrations\Exception\Operation\ForeignKeyException;
17
use Cycle\Migrations\Operation\Traits\OptionsTrait;
18
19
final class Alter extends ForeignKey
20
{
21
    use OptionsTrait;
22
23
    /** @var string */
24
    protected $foreignTable;
25
26
    /** @var array */
27
    protected $foreignKeys;
28
29
    /**
30
     * @param string $table
31
     * @param array  $columns
32
     * @param string $foreignTable
33
     * @param array  $foreignKeys
34
     * @param array  $options
35
     */
36
    public function __construct(
37
        string $table,
38
        array $columns,
39
        string $foreignTable,
40
        array $foreignKeys,
41
        array $options
42
    ) {
43
        parent::__construct($table, $columns);
44
        $this->foreignTable = $foreignTable;
45
        $this->foreignKeys = $foreignKeys;
46
        $this->options = $options;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function execute(CapsuleInterface $capsule): void
53
    {
54
        $schema = $capsule->getSchema($this->getTable());
55
56
        if (!$schema->hasForeignKey($this->columns)) {
57
            throw new ForeignKeyException(
58
                "Unable to alter foreign key '{$schema->getName()}'.({$this->columnNames()}), "
59
                . 'key does not exists'
60
            );
61
        }
62
63
        $outerSchema = $capsule->getSchema($this->foreignTable);
64
65
        if ($this->foreignTable != $this->table && !$outerSchema->exists()) {
66
            throw new ForeignKeyException(
67
                "Unable to alter foreign key '{$schema->getName()}'.'{$this->columnNames()}', "
68
                . "foreign table '{$this->foreignTable}' does not exists"
69
            );
70
        }
71
72
73
        foreach ($this->foreignKeys as $fk) {
74
            if ($this->foreignTable != $this->table && !$outerSchema->hasColumn($fk)) {
75
                throw new ForeignKeyException(
76
                    "Unable to alter foreign key '{$schema->getName()}'.'{$this->columnNames()}',"
77
                    . " foreign column '{$this->foreignTable}'.'{$fk}' does not exists"
78
                );
79
            }
80
        }
81
82
        $foreignKey = $schema->foreignKey($this->columns)->references(
83
            $this->foreignTable,
84
            $this->foreignKeys
85
        );
86
87
        /*
88
         * We are allowing both formats "NO_ACTION" and "NO ACTION".
89
         */
90
91
        $foreignKey->onDelete(
92
            str_replace(
93
                '_',
94
                ' ',
95
                $this->getOption('delete', ForeignKeyInterface::NO_ACTION)
96
            )
97
        );
98
99
        $foreignKey->onUpdate(
100
            str_replace(
101
                '_',
102
                ' ',
103
                $this->getOption('update', ForeignKeyInterface::NO_ACTION)
104
            )
105
        );
106
    }
107
}
108