Passed
Pull Request — master (#26)
by butschster
03:01
created

ForeignKeyTrait::createForeignKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 3
b 0
f 0
nc 2
nop 5
dl 0
loc 16
rs 10
1
<?php
2
3
/**
4
 * Cycle ORM Schema Builder.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Schema\Relation\Traits;
13
14
use Cycle\Schema\Definition\Entity;
15
use Cycle\Schema\Definition\Field;
16
use Cycle\Schema\Registry;
17
use Cycle\Schema\Relation\OptionSchema;
18
use Cycle\Schema\Relation\RelationSchema;
19
20
trait ForeignKeyTrait
21
{
22
    /**
23
     * Create foreign key between two entities. Only when both entities are located
24
     * in a same database.
25
     *
26
     * @param Registry $registry
27
     * @param Entity   $source
28
     * @param Entity   $target
29
     * @param Field    $innerField
30
     * @param Field    $outerField
31
     */
32
    protected function createForeignKey(
33
        Registry $registry,
34
        Entity $source,
35
        Entity $target,
36
        Field $innerField,
37
        Field $outerField
38
    ): void {
39
        if ($registry->getDatabase($source) !== $registry->getDatabase($target)) {
40
            return;
41
        }
42
43
        $registry->getTableSchema($target)
44
                 ->foreignKey([$outerField->getColumn()])
45
                 ->references($registry->getTable($source), [$innerField->getColumn()])
46
                 ->onUpdate($this->getOptions()->get(RelationSchema::FK_ACTION))
0 ignored issues
show
Bug introduced by
It seems like $this->getOptions()->get...ationSchema::FK_ACTION) can also be of type array; however, parameter $rule of Spiral\Database\Schema\A...tForeignKey::onUpdate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
                 ->onUpdate(/** @scrutinizer ignore-type */ $this->getOptions()->get(RelationSchema::FK_ACTION))
Loading history...
47
                 ->onDelete($this->getOptions()->get(RelationSchema::FK_ACTION));
0 ignored issues
show
Bug introduced by
It seems like $this->getOptions()->get...ationSchema::FK_ACTION) can also be of type array; however, parameter $rule of Spiral\Database\Schema\A...tForeignKey::onDelete() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
                 ->onDelete(/** @scrutinizer ignore-type */ $this->getOptions()->get(RelationSchema::FK_ACTION));
Loading history...
48
    }
49
50
51
    /**
52
     * Create foreign key between two entities. Only when both entities are located
53
     * in a same database.
54
     *
55
     * @param Registry $registry
56
     * @param Entity   $source
57
     * @param Entity   $target
58
     * @param Field[]  $innerFields
59
     * @param Field[]  $outerFields
60
     */
61
    protected function createForeignCompositeKey(
62
        Registry $registry,
63
        Entity $source,
64
        Entity $target,
65
        array $innerFields,
66
        array $outerFields
67
    ): void {
68
        if ($registry->getDatabase($source) !== $registry->getDatabase($target)) {
69
            return;
70
        }
71
72
        $registry->getTableSchema($target)
73
            ->foreignKey(array_map(function (Field $field) {
74
                return $field->getColumn();
75
            }, $outerFields))
76
            ->references($registry->getTable($source), array_map(function (Field $field) {
77
                return $field->getColumn();
78
            }, $innerFields))
79
            ->onUpdate($this->getOptions()->get(RelationSchema::FK_ACTION))
0 ignored issues
show
Bug introduced by
It seems like $this->getOptions()->get...ationSchema::FK_ACTION) can also be of type array; however, parameter $rule of Spiral\Database\Schema\A...tForeignKey::onUpdate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
            ->onUpdate(/** @scrutinizer ignore-type */ $this->getOptions()->get(RelationSchema::FK_ACTION))
Loading history...
80
            ->onDelete($this->getOptions()->get(RelationSchema::FK_ACTION));
0 ignored issues
show
Bug introduced by
It seems like $this->getOptions()->get...ationSchema::FK_ACTION) can also be of type array; however, parameter $rule of Spiral\Database\Schema\A...tForeignKey::onDelete() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            ->onDelete(/** @scrutinizer ignore-type */ $this->getOptions()->get(RelationSchema::FK_ACTION));
Loading history...
81
    }
82
83
    /**
84
     * @return OptionSchema
85
     */
86
    abstract protected function getOptions(): OptionSchema;
87
}
88