Passed
Push — master ( 1fa407...e70c1d )
by Anton
04:02
created

Embedded::compute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 1
dl 0
loc 24
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
9
10
namespace Cycle\Schema\Relation;
11
12
use Cycle\ORM\Relation;
13
use Cycle\Schema\Registry;
14
use Cycle\Schema\Relation\Traits\ForeignKeyTrait;
15
16
final class Embedded extends RelationSchema
17
{
18
    use  ForeignKeyTrait;
19
20
    // internal relation type
21
    protected const RELATION_TYPE = Relation::EMBEDDED;
22
23
    // relation schema options
24
    protected const RELATION_SCHEMA = [
25
        Relation::LOAD => Relation::LOAD_EAGER,
26
    ];
27
28
    /**
29
     * @param Registry $registry
30
     */
31
    public function compute(Registry $registry)
32
    {
33
        $source = $registry->getEntity($this->source);
34
        $target = $registry->getEntity($this->target);
35
36
        // each embedded entity must isolated
37
        $target = clone $target;
38
        $target->setRole($source->getRole() . '.' . $target->getRole());
39
40
        // embedded entity must point to the same table as parent entity
41
        $registry->register($target);
42
        $registry->linkTable($target, $registry->getDatabase($source), $registry->getTable($source));
43
44
        // isolated
45
        $this->target = $target->getRole();
46
47
        foreach ($source->getFields() as $name => $field) {
48
            if ($field->isPrimary()) {
49
                // sync primary keys
50
                $target->getFields()->set($name, $field);
51
            }
52
        }
53
54
        parent::compute($registry);
55
    }
56
57
    /**
58
     * @param Registry $registry
59
     */
60
    public function render(Registry $registry)
61
    {
62
        // relation does not require any column rendering besides actual tables
63
    }
64
}