Completed
Push — 2.x ( db763c...24284c )
by Aleksei
05:34 queued 05:33
created

ForeignKeys   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 21
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 19 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Generator;
6
7
use Cycle\Schema\GeneratorInterface;
8
use Cycle\Schema\Registry;
9
10
final class ForeignKeys implements GeneratorInterface
11
{
12
    public function run(Registry $registry): Registry
13
    {
14
        foreach ($registry as $entity) {
15
            foreach ($entity->getForeignKeys() as $fk) {
16
                $target = $registry->getEntity($fk->getTarget());
17
18
                if (!$registry->getTableSchema($target)->hasIndex($fk->getOuterColumns())) {
19
                    $registry->getTableSchema($target)->index($fk->getOuterColumns())->unique();
20
                }
21
22
                $registry->getTableSchema($entity)
23
                    ->foreignKey($fk->getInnerColumns(), $fk->isCreateIndex())
24
                    ->references($registry->getTable($target), $fk->getOuterColumns())
25
                    ->onUpdate($fk->getAction())
26
                    ->onDelete($fk->getAction());
27
            }
28
        }
29
30
        return $registry;
31
    }
32
}
33