Passed
Pull Request — master (#26)
by Aleksei
02:07
created

ForeignKeyTrait::createForeignCompositeKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 5
dl 0
loc 16
rs 10
c 0
b 0
f 0
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\Definition\Map\FieldMap;
17
use Cycle\Schema\Registry;
18
use Cycle\Schema\Relation\OptionSchema;
19
use Cycle\Schema\Relation\RelationSchema;
20
21
trait ForeignKeyTrait
22
{
23
    /**
24
     * Create foreign key between two entities. Only when both entities are located in a same database.
25
     */
26
    protected function createForeignKey(
27
        Registry $registry,
28
        Entity $source,
29
        Entity $target,
30
        Field $innerField,
31
        Field $outerField
32
    ): void {
33
        if ($registry->getDatabase($source) !== $registry->getDatabase($target)) {
34
            return;
35
        }
36
37
        $outerFields = (new FieldMap())->set($outerField->getColumn(), $outerField);
38
        $innerFields = (new FieldMap())->set($innerField->getColumn(), $innerField);
39
40
        $this->createForeignCompositeKey($registry, $source, $target, $outerFields, $innerFields);
41
    }
42
43
44
    /**
45
     * Create foreign key between two entities with composite fields. Only when both entities are located
46
     * in a same database.
47
     */
48
    protected function createForeignCompositeKey(
49
        Registry $registry,
50
        Entity $source,
51
        Entity $target,
52
        FieldMap $innerFields,
53
        FieldMap $outerFields
54
    ): void {
55
        if ($registry->getDatabase($source) !== $registry->getDatabase($target)) {
56
            return;
57
        }
58
59
        $registry->getTableSchema($target)
60
            ->foreignKey($outerFields->getColumnNames())
61
            ->references($registry->getTable($source), $innerFields->getColumnNames())
62
            ->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

62
            ->onUpdate(/** @scrutinizer ignore-type */ $this->getOptions()->get(RelationSchema::FK_ACTION))
Loading history...
63
            ->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

63
            ->onDelete(/** @scrutinizer ignore-type */ $this->getOptions()->get(RelationSchema::FK_ACTION));
Loading history...
64
    }
65
66
    abstract protected function getOptions(): OptionSchema;
67
}
68