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

ForeignKeys::run()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 19
rs 9.9
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