MorphedHasOne::render()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Relation\Morphed;
6
7
use Cycle\ORM\Relation;
8
use Cycle\Schema\Registry;
9
use Cycle\Schema\Relation\RelationSchema;
10
use Cycle\Schema\Relation\Traits\FieldTrait;
11
use Cycle\Schema\Relation\Traits\MorphTrait;
12
13
final class MorphedHasOne extends RelationSchema
14
{
15
    use FieldTrait;
16
    use MorphTrait;
17
18
    // internal relation type
19
    protected const RELATION_TYPE = Relation::MORPHED_HAS_ONE;
20
21
    // relation schema options
22
    protected const RELATION_SCHEMA = [
23
        // save with parent
24
        Relation::CASCADE => true,
25
26
        // do not pre-load relation by default
27
        Relation::LOAD => Relation::LOAD_PROMISE,
28
29
        // nullable by default
30
        Relation::NULLABLE => false,
31
32
        // default field name for inner key
33
        Relation::OUTER_KEY => '{relation}_{source:primaryKey}',
34
35
        // link to parent entity primary key by default
36
        Relation::INNER_KEY => '{source:primaryKey}',
37
38
        // link to parent entity primary key by default
39
        Relation::MORPH_KEY => '{relation}_role',
40
41
        // rendering options
42
        RelationSchema::INDEX_CREATE => true,
43
        RelationSchema::MORPH_KEY_LENGTH => 32,
44
    ];
45
46
    public function compute(Registry $registry): void
47
    {
48
        parent::compute($registry);
49 48
50
        $source = $registry->getEntity($this->source);
51 48
        $target = $registry->getEntity($this->target);
52
53 48
        // create target outer field
54 48
        $this->createRelatedFields(
55
            $source,
56
            Relation::INNER_KEY,
57 48
            $target,
58
            Relation::OUTER_KEY,
59 48
        );
60
61 48
        // create target outer field
62
        $this->ensureMorphField(
63
            $target,
64
            $this->options->get(Relation::MORPH_KEY),
65 48
            $this->options->get(RelationSchema::MORPH_KEY_LENGTH),
66
            $this->options->get(Relation::NULLABLE),
67 48
        );
68 48
    }
69 48
70
    public function render(Registry $registry): void
71 48
    {
72
        $target = $registry->getEntity($this->target);
73 16
        $outerFields = $this->getFields($target, Relation::OUTER_KEY);
74
        $morphFields = $this->getFields($target, Relation::MORPH_KEY);
75 16
76 16
        $this->mergeIndex($registry, $target, $outerFields, $morphFields);
77 16
    }
78
}
79