RefersTo   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 72
ccs 20
cts 20
cp 1
rs 10
c 2
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compute() 0 15 1
A render() 0 22 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Relation;
6
7
use Cycle\ORM\Relation;
8
use Cycle\Schema\Registry;
9
use Cycle\Schema\Relation\Traits\FieldTrait;
10
use Cycle\Schema\Relation\Traits\ForeignKeyTrait;
11
12
/**
13
 * Similar to BelongsTo relation but does not force external object to always exists.
14
 */
15
final class RefersTo extends RelationSchema
16
{
17
    use FieldTrait;
18
    use ForeignKeyTrait;
19
20
    // internal relation type
21
    protected const RELATION_TYPE = Relation::REFERS_TO;
22
23
    // relation schema options
24
    protected const RELATION_SCHEMA = [
25
        // save with parent
26
        Relation::CASCADE => true,
27
28
        // do not pre-load relation by default
29
        Relation::LOAD => Relation::LOAD_PROMISE,
30
31
        // nullable by default
32
        Relation::NULLABLE => true,
33
34
        // link to parent entity primary key by default
35
        Relation::INNER_KEY => '{relation}_{outerKey}',
36
37
        // default field name for inner key
38
        Relation::OUTER_KEY => '{target:primaryKey}',
39
40
        // rendering options
41
        RelationSchema::INDEX_CREATE => true,
42
        RelationSchema::FK_CREATE => true,
43
        RelationSchema::FK_ACTION => 'SET NULL',
44
        RelationSchema::FK_ON_DELETE => null,
45
    ];
46
47 104
    public function compute(Registry $registry): void
48
    {
49 104
        parent::compute($registry);
50
51 88
        $source = $registry->getEntity($this->source);
52 88
        $target = $registry->getEntity($this->target);
53
54 88
        $this->normalizeContextFields($source, $target);
55
56
        // create target outer field
57 88
        $this->createRelatedFields(
58
            $target,
59 88
            Relation::OUTER_KEY,
60
            $source,
61 88
            Relation::INNER_KEY,
62
        );
63 80
    }
64
65 32
    public function render(Registry $registry): void
66
    {
67 32
        $source = $registry->getEntity($this->source);
68 32
        $target = $registry->getEntity($this->target);
69
70 32
        $innerFields = $this->getFields($source, Relation::INNER_KEY);
71 32
        $outerFields = $this->getFields($target, Relation::OUTER_KEY);
72
73 32
        $table = $registry->getTableSchema($source);
74
75 32
        if ($this->options->get(self::INDEX_CREATE) && $innerFields->count() > 0) {
76 32
            $table->index($innerFields->getColumnNames());
77
        }
78
79 32
        if ($this->options->get(self::FK_CREATE)) {
80 16
            $this->createForeignCompositeKey(
81
                $registry,
82 32
                $target,
83
                $source,
84
                $outerFields,
85
                $innerFields,
86
                $this->options->get(self::INDEX_CREATE),
87
            );
88
        }
89
    }
90
}
91