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

src/Relation/HasMany.php (1 issue)

Labels
Severity
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;
11
12
use Cycle\ORM\Command\Branch\Condition;
13
use Cycle\ORM\Command\Branch\Sequence;
14
use Cycle\ORM\Command\CommandInterface;
15
use Cycle\ORM\Command\ContextCarrierInterface as CC;
16
use Cycle\ORM\Heap\Node;
17
use Cycle\ORM\Promise\Collection\CollectionPromise;
18
use Cycle\ORM\Promise\PromiseInterface;
19
use Cycle\ORM\Promise\PromiseMany;
20
use Cycle\ORM\Promise\ReferenceInterface;
21
use Cycle\ORM\Relation;
22
use Doctrine\Common\Collections\ArrayCollection;
23
use Doctrine\Common\Collections\Collection;
24
25
/**
26
 * Provides the ability to own the collection of entities.
27
 */
28
class HasMany extends AbstractRelation
29
{
30
    /**
31
     * Init relation state and entity collection.
32
     *
33
     * @param array $data
34
     * @return array
35
     */
36
    public function init(array $data): array
37
    {
38
        $elements = [];
39
        foreach ($data as $item) {
40
            $elements[] = $this->orm->make($this->target, $item, Node::MANAGED);
41
        }
42
43
        return [new ArrayCollection($elements), $elements];
44
    }
45
46
    /**
47
     * Convert entity data into array.
48
     *
49
     * @param mixed $data
50
     * @return array|PromiseInterface
51
     */
52
    public function extract($data)
53
    {
54
        if ($data instanceof CollectionPromise && !$data->isInitialized()) {
55
            return $data->getPromise();
56
        }
57
58
        if ($data instanceof Collection) {
59
            return $data->toArray();
60
        }
61
62
        return is_array($data) ? $data : [];
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function initPromise(Node $node): array
69
    {
70
        if (empty($innerKey = $this->fetchKey($node, $this->innerKey))) {
71
            return [new ArrayCollection(), null];
72
        }
73
74
        $p = new PromiseMany(
75
            $this->orm,
76
            $this->target,
77
            [
78
                $this->outerKey => $innerKey
79
            ],
80
            $this->schema[Relation::WHERE] ?? []
81
        );
82
83
        return [new CollectionPromise($p), $p];
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function queue(CC $store, $entity, Node $node, $related, $original): CommandInterface
90
    {
91
        if ($related instanceof ReferenceInterface) {
92
            $related = $this->resolve($related);
93
        }
94
95
        if ($original instanceof ReferenceInterface) {
96
            $original = $this->resolve($original);
97
        }
98
99
        $sequence = new Sequence();
100
101
        foreach ($related as $item) {
102
            $sequence->addCommand($this->queueStore($node, $item));
103
        }
104
105
        foreach ($this->calcDeleted($related, $original ?? []) as $item) {
106
            $sequence->addCommand($this->queueDelete($item));
107
        }
108
109
        return $sequence;
110
    }
111
112
    /**
113
     * Return objects which are subject of removal.
114
     *
115
     * @param array $related
116
     * @param array $original
117
     * @return array
118
     */
119
    protected function calcDeleted(array $related, array $original)
120
    {
121
        return array_udiff($original ?? [], $related, function ($a, $b) {
122
            return strcmp(spl_object_hash($a), spl_object_hash($b));
123
        });
124
    }
125
126
    /**
127
     * Persist related object.
128
     *
129
     * @param Node   $node
130
     * @param object $related
131
     * @return CC
132
     */
133
    protected function queueStore(Node $node, $related): CC
134
    {
135
        $relStore = $this->orm->queueStore($related);
136
        $relNode = $this->getNode($related, +1);
137
        $this->assertValid($relNode);
0 ignored issues
show
It seems like $relNode 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

137
        $this->assertValid(/** @scrutinizer ignore-type */ $relNode);
Loading history...
138
139
        $this->forwardContext(
140
            $node,
141
            $this->innerKey,
142
            $relStore,
143
            $relNode,
144
            $this->outerKey
145
        );
146
147
        return $relStore;
148
    }
149
150
    /**
151
     * Remove one of related objects.
152
     *
153
     * @param object $related
154
     * @return CommandInterface
155
     */
156
    protected function queueDelete($related): CommandInterface
157
    {
158
        $rNode = $this->getNode($related);
159
160
        if (!$this->isNullable()) {
161
            $store = $this->orm->queueStore($related);
162
            $store->register($this->outerKey, null, true);
163
            $rNode->getState()->decClaim();
164
165
            return new Condition($store, function () use ($rNode) {
166
                return !$rNode->getState()->hasClaims();
167
            });
168
        }
169
170
        return new Condition($this->orm->queueDelete($related), function () use ($rNode) {
171
            return !$rNode->getState()->hasClaims();
172
        });
173
    }
174
}