Completed
Push — master ( e1ce3b...f144d1 )
by Anton
14s queued 12s
created

src/Relation/RefersTo.php (2 issues)

Labels
1
<?php
2
3
/**
4
 * Cycle DataMapper ORM
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\ORM\Relation;
13
14
use Cycle\ORM\Command\Branch\Nil;
15
use Cycle\ORM\Command\CommandInterface;
16
use Cycle\ORM\Command\ContextCarrierInterface as CC;
17
use Cycle\ORM\Command\Database\Update;
18
use Cycle\ORM\Heap\Node;
19
use Cycle\ORM\Relation\Traits\PromiseOneTrait;
20
use Cycle\ORM\Schema;
21
22
/**
23
 * Variation of belongs-to relation which provides the ability to be self linked. Relation can be used
24
 * to create cyclic references. Relation does not trigger store operation of referenced object!
25
 */
26
class RefersTo extends AbstractRelation implements DependencyInterface
27
{
28
    use PromiseOneTrait;
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public function queue(CC $store, $entity, Node $node, $related, $original): CommandInterface
34
    {
35
        // refers-to relation is always nullable (as opposite to belongs-to)
36
        if ($related === null) {
37
            if ($original !== null) {
38
                $store->register($this->innerKey, null, true);
39
            }
40
41
            return new Nil();
42
        }
43
44
        $rNode = $this->getNode($related);
45
        $this->assertValid($rNode);
0 ignored issues
show
It seems like $rNode can also be of type null; however, parameter $related of Cycle\ORM\Relation\AbstractRelation::assertValid() does only seem to accept Cycle\ORM\Heap\Node, 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

45
        $this->assertValid(/** @scrutinizer ignore-type */ $rNode);
Loading history...
46
47
        // related object exists, we can update key immediately
48
        $outerKey = $this->fetchKey($rNode, $this->outerKey);
49
        if ($outerKey !== null) {
50
            if ($outerKey != $this->fetchKey($node, $this->innerKey)) {
51
                $store->register($this->innerKey, $outerKey, true);
52
            }
53
54
            return new Nil();
55
        }
56
57
        // update parent entity once related instance is able to provide us context key
58
        $update = new Update(
59
            $this->getSource($node->getRole())->getDatabase(),
60
            $this->getSource($node->getRole())->getTable()
61
        );
62
63
        // fastest way to identify the entity
64
        $pk = $this->orm->getSchema()->define($node->getRole(), Schema::PRIMARY_KEY);
0 ignored issues
show
The method getSchema() 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

64
        $pk = $this->orm->/** @scrutinizer ignore-call */ getSchema()->define($node->getRole(), Schema::PRIMARY_KEY);
Loading history...
65
66
        $this->forwardContext(
67
            $rNode,
68
            $this->outerKey,
69
            $update,
70
            $node,
71
            $this->innerKey
72
        );
73
74
        // set where condition for update query
75
        $this->forwardScope(
76
            $node,
77
            $pk,
78
            $update,
79
            $pk
80
        );
81
82
        return $update;
83
    }
84
}
85