Passed
Push — master ( e08e26...a5588d )
by Anton
01:48
created

ForeignKeyTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A createForeignKey() 0 16 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
namespace Cycle\Schema\Relation\Traits;
11
12
use Cycle\Schema\Definition\Entity;
13
use Cycle\Schema\Definition\Field;
14
use Cycle\Schema\Registry;
15
use Cycle\Schema\Relation\Util\OptionSchema;
16
17
trait ForeignKeyTrait
18
{
19
    /**
20
     * Create foreign key between two entities. Only when both entities are located
21
     * in a same database.
22
     *
23
     * @param Registry $registry
24
     * @param Entity   $source
25
     * @param Entity   $target
26
     * @param Field    $innerField
27
     * @param Field    $outerField
28
     */
29
    protected function createForeignKey(
30
        Registry $registry,
31
        Entity $source,
32
        Entity $target,
33
        Field $innerField,
34
        Field $outerField
35
    ) {
36
        if ($registry->getDatabase($source) !== $registry->getDatabase($target)) {
37
            return;
38
        }
39
40
        $registry->getTableSchema($target)
41
            ->foreignKey($outerField->getColumn())
42
            ->references($registry->getTable($source), $innerField->getColumn())
43
            ->onUpdate($this->getOptions()->get(self::FK_ACTION))
0 ignored issues
show
Bug introduced by
The constant Cycle\Schema\Relation\Tr...eignKeyTrait::FK_ACTION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
44
            ->onDelete($this->getOptions()->get(self::FK_ACTION));
45
    }
46
47
    /**
48
     * @return OptionSchema
49
     */
50
    abstract protected function getOptions(): OptionSchema;
51
}