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

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

Labels
Severity
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
        // nullable by default
31
        Relation::NULLABLE               => true,
32
33
        // default field name for inner key
34
        Relation::OUTER_KEY              => '{relation}_{source:primaryKey}',
35
36
        // link to parent entity primary key by default
37
        Relation::INNER_KEY              => '{source:primaryKey}',
38
39
        // link to parent entity primary key by default
40
        Relation::MORPH_KEY              => '{relation}_role',
41
42
        // rendering options
43
        RelationSchema::INDEX_CREATE     => true,
44
        RelationSchema::MORPH_KEY_LENGTH => 32
45
    ];
46
47
    /**
48
     * @param Registry $registry
49
     */
50
    public function compute(Registry $registry)
51
    {
52
        parent::compute($registry);
53
54
        $source = $registry->getEntity($this->source);
55
        $target = $registry->getEntity($this->target);
56
57
        // create target outer field
58
        $this->ensureField(
59
            $target,
60
            $this->options->get(Relation::OUTER_KEY),
61
            $this->getField($source, Relation::INNER_KEY),
62
            $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

62
            /** @scrutinizer ignore-type */ $this->options->get(Relation::NULLABLE)
Loading history...
63
        );
64
65
        // create target outer field
66
        $this->ensureMorphField(
67
            $target,
68
            $this->options->get(Relation::MORPH_KEY),
69
            $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

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

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