Issues (20)

src/Relation/Morphed/MorphedHasMany.php (3 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * Cycle ORM Schema Builder.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Schema\Relation\Morphed;
13
14
use Cycle\ORM\Relation;
15
use Cycle\Schema\Registry;
16
use Cycle\Schema\Relation\RelationSchema;
17
use Cycle\Schema\Relation\Traits\FieldTrait;
18
use Cycle\Schema\Relation\Traits\MorphTrait;
19
20
final class MorphedHasMany extends RelationSchema
21
{
22
    use FieldTrait;
23
    use MorphTrait;
24
25
    // internal relation type
26
    protected const RELATION_TYPE = Relation::MORPHED_HAS_MANY;
27
28
    // relation schema options
29
    protected const RELATION_SCHEMA = [
30
        // save with parent
31
        Relation::CASCADE                => true,
32
33
        // do not pre-load relation by default
34
        Relation::LOAD                   => Relation::LOAD_PROMISE,
35
36
        // nullable by default
37
        Relation::NULLABLE               => true,
38
39
        // default field name for inner key
40
        Relation::OUTER_KEY              => '{relation}_{source:primaryKey}',
41
42
        // link to parent entity primary key by default
43
        Relation::INNER_KEY              => '{source:primaryKey}',
44
45
        // link to parent entity primary key by default
46
        Relation::MORPH_KEY              => '{relation}_role',
47
48
        // rendering options
49
        RelationSchema::INDEX_CREATE     => true,
50
        RelationSchema::MORPH_KEY_LENGTH => 32
51
    ];
52
53
    /**
54
     * @param Registry $registry
55
     */
56
    public function compute(Registry $registry): void
57
    {
58
        parent::compute($registry);
59
60
        $source = $registry->getEntity($this->source);
61
        $target = $registry->getEntity($this->target);
62
63
        // create target outer field
64
        $this->ensureField(
65
            $target,
66
            $this->options->get(Relation::OUTER_KEY),
67
            $this->getField($source, Relation::INNER_KEY),
68
            $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\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

68
            /** @scrutinizer ignore-type */ $this->options->get(Relation::NULLABLE)
Loading history...
69
        );
70
71
        // create target outer field
72
        $this->ensureMorphField(
73
            $target,
74
            $this->options->get(Relation::MORPH_KEY),
75
            $this->options->get(RelationSchema::MORPH_KEY_LENGTH),
0 ignored issues
show
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

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

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