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

src/Relation/RefersTo.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\Registry;
13
use Cycle\Schema\Relation\Traits\FieldTrait;
14
use Cycle\Schema\Relation\Traits\ForeignKeyTrait;
15
16
/**
17
 * Similar to BelongsTo relation but does not force external object to always exists.
18
 */
19
class RefersTo extends RelationSchema
20
{
21
    use FieldTrait, ForeignKeyTrait;
22
23
    // internal relation type
24
    protected const RELATION_TYPE = Relation::REFERS_TO;
25
26
    // relation schema options
27
    protected const RELATION_SCHEMA = [
28
        // save with parent
29
        Relation::CASCADE            => true,
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
    ];
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
            $source,
59
            $this->options->get(Relation::INNER_KEY),
60
            $this->getField($target, Relation::OUTER_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\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

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($source);
77
78
        if ($this->options->get(self::INDEX_CREATE)) {
79
            $table->index([$innerField->getColumn()]);
80
        }
81
82
        if ($this->options->get(self::FK_CREATE)) {
83
            $this->createForeignKey($registry, $target, $source, $outerField, $innerField);
84
        }
85
    }
86
}