Passed
Push — master ( 6ac325...8081f8 )
by Anton
01:32
created

src/Relation/HasOne.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Cycle\Schema\Relation;
10
11
use Cycle\ORM\Relation;
12
use Cycle\Schema\Exception\RelationException;
13
use Cycle\Schema\InversableInterface;
14
use Cycle\Schema\Registry;
15
use Cycle\Schema\Relation\Traits\FieldTrait;
16
use Cycle\Schema\Relation\Traits\ForeignKeyTrait;
17
use Cycle\Schema\RelationInterface;
18
19
final class HasOne extends RelationSchema implements InversableInterface
20
{
21
    use FieldTrait, ForeignKeyTrait;
22
23
    // internal relation type
24
    protected const RELATION_TYPE = Relation::HAS_ONE;
25
26
    // relation schema options
27
    protected const RELATION_SCHEMA = [
28
        // save with parent
29
        Relation::CASCADE            => true,
30
31
        // not nullable by default
32
        Relation::NULLABLE           => false,
33
34
        // link to parent entity primary key by default
35
        Relation::INNER_KEY          => '{source:primaryKey}',
36
37
        // default field name for inner key
38
        Relation::OUTER_KEY          => '{source:role}_{innerKey}',
39
40
        // rendering options
41
        RelationSchema::INDEX_CREATE => true,
42
        RelationSchema::FK_CREATE    => true,
43
        RelationSchema::FK_ACTION    => 'CASCADE'
44
    ];
45
46
    /**
47
     * @param Registry $registry
48
     */
49
    public function compute(Registry $registry)
50
    {
51
        parent::compute($registry);
52
53
        $source = $registry->getEntity($this->source);
54
        $target = $registry->getEntity($this->target);
55
56
        // create target outer field
57
        $this->ensureField(
58
            $target,
59
            $this->options->get(Relation::OUTER_KEY),
60
            $this->getField($source, Relation::INNER_KEY),
61
            $this->options->get(Relation::NULLABLE)
0 ignored issues
show
It seems like $this->options->get(Cycle\ORM\Relation::NULLABLE) can also be of type string; however, parameter $nullable of Cycle\Schema\Relation\HasOne::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

61
            /** @scrutinizer ignore-type */ $this->options->get(Relation::NULLABLE)
Loading history...
62
        );
63
    }
64
65
    /**
66
     * @param Registry $registry
67
     */
68
    public function render(Registry $registry)
69
    {
70
        $source = $registry->getEntity($this->source);
71
        $target = $registry->getEntity($this->target);
72
73
        $innerField = $this->getField($source, Relation::INNER_KEY);
74
        $outerField = $this->getField($target, Relation::OUTER_KEY);
75
76
        $table = $registry->getTableSchema($target);
77
78
        if ($this->options->get(self::INDEX_CREATE)) {
79
            $table->index([$outerField->getColumn()]);
80
        }
81
82
        if ($this->options->get(self::FK_CREATE)) {
83
            $this->createForeignKey($registry, $source, $target, $innerField, $outerField);
84
        }
85
    }
86
87
    /**
88
     * @param Registry $registry
89
     * @return array
90
     */
91
    public function inverseTargets(Registry $registry): array
92
    {
93
        return [
94
            $registry->getEntity($this->target)
95
        ];
96
    }
97
98
    /**
99
     * @param RelationInterface $relation
100
     * @param string            $into
101
     * @return RelationInterface
102
     *
103
     * @throws RelationException
104
     */
105
    public function inverseRelation(RelationInterface $relation, string $into): RelationInterface
106
    {
107
        if (!$relation instanceof BelongsTo && !$relation instanceof RefersTo) {
108
            throw new RelationException("HasOne relation can only be inversed into BelongsTo or RefersTo");
109
        }
110
111
        return $relation->withContext(
112
            $into,
113
            $this->target,
114
            $this->source,
115
            $this->options->withOptions([
116
                Relation::INNER_KEY => $this->options->get(Relation::OUTER_KEY),
117
                Relation::OUTER_KEY => $this->options->get(Relation::INNER_KEY),
118
            ])
119
        );
120
    }
121
}