Passed
Push — master ( 0a4fc8...34ac19 )
by Anton
04:11
created

src/Relation/Morphed/BelongsToMorphed.php (2 issues)

Labels
1
<?php
2
/**
3
 * Cycle DataMapper ORM
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
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 $node): array
41
    {
42
        if (is_null($innerKey = $this->fetchKey($node, $this->innerKey))) {
43
            return [null, null];
44
        }
45
46
        /** @var string $target */
47
        $target = $this->fetchKey($node, $this->morphKey);
48
        if (is_null($target)) {
0 ignored issues
show
The condition is_null($target) is always false.
Loading history...
49
            return [null, null];
50
        }
51
52
        $e = $this->orm->getHeap()->find($target, $this->outerKey, $innerKey);
0 ignored issues
show
The method getHeap() 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
        $e = $this->orm->/** @scrutinizer ignore-call */ getHeap()->find($target, $this->outerKey, $innerKey);
Loading history...
53
        if ($e !== null) {
54
            return [$e, $e];
55
        }
56
57
        $e = $this->orm->promise($target, [$this->outerKey => $innerKey]);
58
59
        return [$e, $e];
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function queue(CC $store, $entity, Node $node, $related, $original): CommandInterface
66
    {
67
        $wrappedStore = parent::queue($store, $entity, $node, $related, $original);
68
69
        if (is_null($related)) {
70
            if ($this->fetchKey($node, $this->morphKey) !== null) {
71
                $store->register($this->morphKey, null, true);
72
                $node->register($this->morphKey, null, true);
73
            }
74
        } else {
75
            $rNode = $this->getNode($related);
76
            if ($this->fetchKey($node, $this->morphKey) != $rNode->getRole()) {
77
                $store->register($this->morphKey, $rNode->getRole(), true);
78
                $node->register($this->morphKey, $rNode->getRole(), true);
79
            }
80
        }
81
82
        return $wrappedStore;
83
    }
84
85
    /**
86
     * Assert that given entity is allowed for the relation.
87
     *
88
     * @param Node $related
89
     *
90
     * @throws RelationException
91
     */
92
    protected function assertValid(Node $related)
93
    {
94
        // no need to validate morphed relation yet
95
    }
96
}