Add::execute()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 55
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 8

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 55
ccs 26
cts 26
cp 1
rs 8.1954
c 0
b 0
f 0
cc 8
nc 7
nop 1
crap 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Migrations\Operation\ForeignKey;
6
7
use Cycle\Database\ForeignKeyInterface;
8
use Cycle\Migrations\CapsuleInterface;
9
use Cycle\Migrations\Exception\Operation\ForeignKeyException;
10
use Cycle\Migrations\Operation\Traits\OptionsTrait;
11
12
final class Add extends ForeignKey
13
{
14
    use OptionsTrait;
15
16 80
    public function __construct(
17
        string $table,
18
        array $columns,
19
        private string $foreignTable,
20
        private array $foreignKeys,
21
        array $options
22
    ) {
23 80
        $this->options = $options;
24 80
        parent::__construct($table, $columns);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 80
    public function execute(CapsuleInterface $capsule): void
31
    {
32 80
        $schema = $capsule->getSchema($this->getTable());
33
34 80
        if ($schema->hasForeignKey($this->columns)) {
35 8
            throw new ForeignKeyException(
36 8
                "Unable to add foreign key '{$schema->getName()}'.({$this->columnNames()}), "
37
                . 'foreign key already exists'
38
            );
39
        }
40
41 72
        $foreignSchema = $capsule->getSchema($this->foreignTable);
42
43 72
        if ($this->foreignTable !== $this->table && !$foreignSchema->exists()) {
44 8
            throw new ForeignKeyException(
45 8
                "Unable to add foreign key '{$schema->getName()}'.'{$this->columnNames()}', "
46 8
                . "foreign table '{$this->foreignTable}' does not exists"
47
            );
48
        }
49
50 64
        foreach ($this->foreignKeys as $fk) {
51 64
            if ($this->foreignTable !== $this->table && !$foreignSchema->hasColumn($fk)) {
52 8
                throw new ForeignKeyException(
53 8
                    "Unable to add foreign key '{$schema->getName()}'.'{$this->columnNames()}',"
54 8
                    . " foreign column '{$this->foreignTable}'.'{$fk}' does not exists"
55
                );
56
            }
57
        }
58
59 56
        $foreignKey = $schema->foreignKey($this->columns)->references(
60 56
            $this->foreignTable,
61 56
            $this->foreignKeys
62
        );
63
64 56
        if ($this->hasOption('name')) {
65 40
            $foreignKey->setName($this->getOption('name'));
66
        }
67
68
        /*
69
         * We are allowing both formats "NO_ACTION" and "NO ACTION".
70
         */
71
72 56
        $foreignKey->onDelete(
73 56
            str_replace(
74
                '_',
75
                ' ',
76 56
                $this->getOption('delete', ForeignKeyInterface::NO_ACTION)
77
            )
78
        );
79
80 56
        $foreignKey->onUpdate(
81 56
            str_replace(
82
                '_',
83
                ' ',
84 56
                $this->getOption('update', ForeignKeyInterface::NO_ACTION)
85
            )
86
        );
87
    }
88
}
89