Passed
Push — master ( 384538...4f7dc7 )
by Anton
02:27
created

RefersTo::render()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 1
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Cycle ORM Schema Builder.
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\FieldTrait;
15
use Cycle\Schema\Relation\Traits\ForeignKeyTrait;
16
17
/**
18
 * Similar to BelongsTo relation but does not force external object to always exists.
19
 */
20
final class RefersTo extends RelationSchema
21
{
22
    use FieldTrait, ForeignKeyTrait;
23
24
    // internal relation type
25
    protected const RELATION_TYPE = Relation::REFERS_TO;
26
27
    // relation schema options
28
    protected const RELATION_SCHEMA = [
29
        // save with parent
30
        Relation::CASCADE            => true,
31
32
        // do not pre-load relation by default
33
        Relation::LOAD               => null,
34
35
        // nullable by default
36
        Relation::NULLABLE           => true,
37
38
        // link to parent entity primary key by default
39
        Relation::INNER_KEY          => '{relation}_{outerKey}',
40
41
        // default field name for inner key
42
        Relation::OUTER_KEY          => '{target:primaryKey}',
43
44
        // rendering options
45
        RelationSchema::INDEX_CREATE => true,
46
        RelationSchema::FK_CREATE    => true,
47
        RelationSchema::FK_ACTION    => 'SET NULL'
48
    ];
49
50
    /**
51
     * @param Registry $registry
52
     */
53
    public function compute(Registry $registry)
54
    {
55
        parent::compute($registry);
56
57
        $source = $registry->getEntity($this->source);
58
        $target = $registry->getEntity($this->target);
59
60
        // create target outer field
61
        $this->ensureField(
62
            $source,
63
            $this->options->get(Relation::INNER_KEY),
64
            $this->getField($target, Relation::OUTER_KEY),
65
            $this->options->get(Relation::NULLABLE)
0 ignored issues
show
Bug introduced by
It seems like $this->options->get(Cycle\ORM\Relation::NULLABLE) can also be of type string; however, parameter $nullable of Cycle\Schema\Relation\RefersTo::ensureField() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
            /** @scrutinizer ignore-type */ $this->options->get(Relation::NULLABLE)
Loading history...
66
        );
67
    }
68
69
    /**
70
     * @param Registry $registry
71
     */
72
    public function render(Registry $registry)
73
    {
74
        $source = $registry->getEntity($this->source);
75
        $target = $registry->getEntity($this->target);
76
77
        $innerField = $this->getField($source, Relation::INNER_KEY);
78
        $outerField = $this->getField($target, Relation::OUTER_KEY);
79
80
        $table = $registry->getTableSchema($source);
81
82
        if ($this->options->get(self::INDEX_CREATE)) {
83
            $table->index([$innerField->getColumn()]);
84
        }
85
86
        if ($this->options->get(self::FK_CREATE)) {
87
            $this->createForeignKey($registry, $target, $source, $outerField, $innerField);
88
        }
89
    }
90
}