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

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

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\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 MorphedHasOne extends RelationSchema
18
{
19
    use FieldTrait, MorphTrait;
20
21
    // internal relation type
22
    protected const RELATION_TYPE = Relation::MORPHED_HAS_ONE;
23
24
    // relation schema options
25
    protected const RELATION_SCHEMA = [
26
        // save with parent
27
        Relation::CASCADE                => true,
28
        
29
        // nullable by default
30
        Relation::NULLABLE               => false,
31
32
        // default field name for inner key
33
        Relation::OUTER_KEY              => '{relation}_{source:primaryKey}',
34
35
        // link to parent entity primary key by default
36
        Relation::INNER_KEY              => '{source:primaryKey}',
37
38
        // link to parent entity primary key by default
39
        Relation::MORPH_KEY              => '{relation}_role',
40
41
        // rendering options
42
        RelationSchema::INDEX_CREATE     => true,
43
        RelationSchema::MORPH_KEY_LENGTH => 32
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
            $target,
59
            $this->options->get(Relation::OUTER_KEY),
60
            $this->getField($source, Relation::INNER_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\Mo...edHasOne::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
        // create target outer field
65
        $this->ensureMorphField(
66
            $target,
67
            $this->options->get(Relation::MORPH_KEY),
68
            $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...One::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

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

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