BelongsTo::inverseTargets()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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\Exception\RelationException;
14
use Cycle\Schema\InversableInterface;
15
use Cycle\Schema\Registry;
16
use Cycle\Schema\Relation\Traits\FieldTrait;
17
use Cycle\Schema\Relation\Traits\ForeignKeyTrait;
18
use Cycle\Schema\RelationInterface;
19
20
final class BelongsTo extends RelationSchema implements InversableInterface
21
{
22
    use FieldTrait, ForeignKeyTrait;
23
24
    // internal relation type
25
    protected const RELATION_TYPE = Relation::BELONGS_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               => Relation::LOAD_PROMISE,
34
35
        // nullable by default
36
        Relation::NULLABLE           => false,
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    => 'CASCADE'
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 Wolfy-J
It seems like $this->options->get(Cycle\ORM\Relation::NULLABLE) can also be of type string; however, parameter $nullable of Cycle\Schema\Relation\BelongsTo::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
91
    /**
92
     * @param Registry $registry
93
     * @return array
94
     */
95
    public function inverseTargets(Registry $registry): array
96
    {
97
        return [
98
            $registry->getEntity($this->target)
99
        ];
100
    }
101
102
    /**
103
     * @param RelationInterface $relation
104
     * @param string            $into
105
     * @param int|null          $load
106
     * @return RelationInterface
107
     *
108
     * @throws RelationException
109
     */
110
    public function inverseRelation(RelationInterface $relation, string $into, ?int $load = null): RelationInterface
111
    {
112
        if (!$relation instanceof HasOne && !$relation instanceof HasMany) {
113
            throw new RelationException("BelongsTo relation can only be inversed into HasOne or HasMany");
114
        }
115
116
        return $relation->withContext(
117
            $into,
118
            $this->target,
119
            $this->source,
120
            $this->options->withOptions([
121
                Relation::LOAD      => $load,
122
                Relation::INNER_KEY => $this->options->get(Relation::OUTER_KEY),
123
                Relation::OUTER_KEY => $this->options->get(Relation::INNER_KEY),
124
            ])
125
        );
126
    }
127
}
128