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

ForeignKeyTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 62
rs 10
c 3
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createForeignCompositeKey() 0 16 2
A createForeignKey() 0 15 2
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
25
     * in a same database.
26
     *
27
     * @param Registry $registry
28
     * @param Entity   $source
29
     * @param Entity   $target
30
     * @param Field    $innerField
31
     * @param Field    $outerField
32
     */
33
    protected function createForeignKey(
34
        Registry $registry,
35
        Entity $source,
36
        Entity $target,
37
        Field $innerField,
38
        Field $outerField
39
    ): void {
40
        if ($registry->getDatabase($source) !== $registry->getDatabase($target)) {
41
            return;
42
        }
43
44
        $outerFields = (new FieldMap())->set($outerField->getColumn(), $outerField);
45
        $innerFields = (new FieldMap())->set($innerField->getColumn(), $innerField);
46
47
        $this->createForeignCompositeKey($registry, $source, $target, $outerFields, $innerFields);
48
    }
49
50
51
    /**
52
     * Create foreign key between two entities with composite fields. 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 FieldMap $innerFields
59
     * @param FieldMap $outerFields
60
     */
61
    protected function createForeignCompositeKey(
62
        Registry $registry,
63
        Entity $source,
64
        Entity $target,
65
        FieldMap $innerFields,
66
        FieldMap $outerFields
67
    ): void {
68
        if ($registry->getDatabase($source) !== $registry->getDatabase($target)) {
69
            return;
70
        }
71
72
        $registry->getTableSchema($target)
73
            ->foreignKey($outerFields->getKeys())
74
            ->references($registry->getTable($source), $innerFields->getKeys())
75
            ->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

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

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