Passed
Push — master ( a1e60a...ad8bf2 )
by Anton
02:58
created

BelongsToMorphed::assertValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
namespace Cycle\ORM\Relation\Morphed;
11
12
use Cycle\ORM\Command\CommandInterface;
13
use Cycle\ORM\Command\ContextCarrierInterface as CC;
14
use Cycle\ORM\Exception\RelationException;
15
use Cycle\ORM\Heap\Node;
16
use Cycle\ORM\ORMInterface;
17
use Cycle\ORM\Relation;
18
use Cycle\ORM\Relation\BelongsTo;
19
20
class BelongsToMorphed extends BelongsTo
21
{
22
    /** @var string */
23
    private $morphKey;
24
25
    /**
26
     * @param ORMInterface $orm
27
     * @param string       $target
28
     * @param string       $name
29
     * @param array        $schema
30
     */
31
    public function __construct(ORMInterface $orm, string $name, string $target, array $schema)
32
    {
33
        parent::__construct($orm, $name, $target, $schema);
34
        $this->morphKey = $schema[Relation::MORPH_KEY];
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function initPromise(Node $parentNode): array
41
    {
42
        if (is_null($innerKey = $this->fetchKey($parentNode, $this->innerKey))) {
43
            return [null, null];
44
        }
45
46
        /** @var string $target */
47
        $target = $this->fetchKey($parentNode, $this->morphKey);
48
        if (is_null($target)) {
0 ignored issues
show
introduced by
The condition is_null($target) is always false.
Loading history...
49
            return [null, null];
50
        }
51
52
        $r = $this->orm->promise($target, [$this->outerKey => $innerKey]);
0 ignored issues
show
Bug introduced by
The method promise() does not exist on Cycle\ORM\Select\SourceProviderInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Cycle\ORM\Select\SourceProviderInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        /** @scrutinizer ignore-call */ 
53
        $r = $this->orm->promise($target, [$this->outerKey => $innerKey]);
Loading history...
53
54
        return [$r, $r];
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function queue(CC $parentStore, $parentEntity, Node $parentNode, $related, $original): CommandInterface
61
    {
62
        $wrappedStore = parent::queue($parentStore, $parentEntity, $parentNode, $related, $original);
63
64
        if (is_null($related)) {
65
            if ($this->fetchKey($parentNode, $this->morphKey) !== null) {
66
                $parentStore->register($this->morphKey, null, true);
67
                $parentNode->register($this->morphKey, null, true);
68
            }
69
        } else {
70
            $relState = $this->getNode($related);
71
            if ($this->fetchKey($parentNode, $this->morphKey) != $relState->getRole()) {
72
                $parentStore->register($this->morphKey, $relState->getRole(), true);
73
                $parentNode->register($this->morphKey, $relState->getRole(), true);
74
            }
75
        }
76
77
        return $wrappedStore;
78
    }
79
80
    /**
81
     * Assert that given entity is allowed for the relation.
82
     *
83
     * @param Node $relNode
84
     *
85
     * @throws RelationException
86
     */
87
    protected function assertValid(Node $relNode)
88
    {
89
        // no need to validate morphed relation yet
90
    }
91
}