Passed
Push — master ( 42cf54...db577f )
by Anton
01:58
created

BelongsToMorphed::compute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 1
dl 0
loc 27
rs 9.7666
c 0
b 0
f 0
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\Morphed;
10
11
use Cycle\ORM\Relation;
12
use Cycle\Schema\Registry;
13
use Cycle\Schema\Relation\RelationSchema;
14
use Cycle\Schema\Relation\Traits\FieldTrait;
15
use Cycle\Schema\Relation\Traits\MorphTrait;
16
17
class BelongsToMorphed extends RelationSchema
18
{
19
    use FieldTrait, MorphTrait;
20
21
    // internal relation type
22
    protected const RELATION_TYPE = Relation::BELONGS_TO_MORPHED;
23
24
    // relation schema options
25
    protected const RELATION_SCHEMA = [
26
        // nullable by default
27
        Relation::NULLABLE               => true,
28
29
        // default field name for inner key
30
        Relation::OUTER_KEY              => '{target:primaryKey}',
31
32
        // link to parent entity primary key by default
33
        Relation::INNER_KEY              => '{relation}_{outerKey}',
34
35
        // link to parent entity primary key by default
36
        Relation::MORPH_KEY              => '{relation}_role',
37
38
        // rendering options
39
        RelationSchema::INDEX_CREATE     => true,
40
        RelationSchema::MORPH_KEY_LENGTH => 32
41
    ];
42
43
    /**
44
     * @param Registry $registry
45
     */
46
    public function compute(Registry $registry)
47
    {
48
        // compute local key
49
        $this->options = $this->options->withContext([
50
            'source:primaryKey' => $this->getPrimary($registry->getEntity($this->source))
51
        ]);
52
53
        $source = $registry->getEntity($this->source);
54
55
        list($outerKey, $outerField) = $this->findOuterKey($registry, $this->target);
56
57
        // register primary key reference
58
        $this->options = $this->options->withContext(['target:primaryKey' => $outerKey]);
59
60
        // create target outer field
61
        $this->ensureField(
62
            $source,
63
            $this->options->get(Relation::INNER_KEY),
64
            $outerField,
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\Mo...oMorphed::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
        $this->ensureMorphField(
69
            $source,
70
            $this->options->get(Relation::MORPH_KEY),
71
            $this->options->get(RelationSchema::MORPH_KEY_LENGTH),
0 ignored issues
show
Bug introduced by
It seems like $this->options->get(Cycl...hema::MORPH_KEY_LENGTH) can also be of type string; however, parameter $lenght of Cycle\Schema\Relation\Mo...hed::ensureMorphField() does only seem to accept integer, 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

71
            /** @scrutinizer ignore-type */ $this->options->get(RelationSchema::MORPH_KEY_LENGTH),
Loading history...
72
            $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\Mo...hed::ensureMorphField() 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

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