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

MorphedHasMany   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compute() 0 21 1
A render() 0 11 2
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\Morphed;
11
12
use Cycle\ORM\Relation;
13
use Cycle\Schema\Registry;
14
use Cycle\Schema\Relation\RelationSchema;
15
use Cycle\Schema\Relation\Traits\FieldTrait;
16
use Cycle\Schema\Relation\Traits\MorphTrait;
17
18
class MorphedHasMany extends RelationSchema
19
{
20
    use FieldTrait, MorphTrait;
21
22
    // internal relation type
23
    protected const RELATION_TYPE = Relation::MORPHED_HAS_MANY;
24
25
    // relation schema options
26
    protected const RELATION_SCHEMA = [
27
        // save with parent
28
        Relation::CASCADE                => true,
29
30
        // do not pre-load relation by default
31
        Relation::LOAD                   => null,
32
33
        // nullable by default
34
        Relation::NULLABLE               => true,
35
36
        // default field name for inner key
37
        Relation::OUTER_KEY              => '{relation}_{source:primaryKey}',
38
39
        // link to parent entity primary key by default
40
        Relation::INNER_KEY              => '{source:primaryKey}',
41
42
        // link to parent entity primary key by default
43
        Relation::MORPH_KEY              => '{relation}_role',
44
45
        // rendering options
46
        RelationSchema::INDEX_CREATE     => true,
47
        RelationSchema::MORPH_KEY_LENGTH => 32
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
            $target,
63
            $this->options->get(Relation::OUTER_KEY),
64
            $this->getField($source, Relation::INNER_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\Mo...dHasMany::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
        // create target outer field
69
        $this->ensureMorphField(
70
            $target,
71
            $this->options->get(Relation::MORPH_KEY),
72
            $this->options->get(RelationSchema::MORPH_KEY_LENGTH),
0 ignored issues
show
Bug introduced by Wolfy-J
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...any::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

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

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