Passed
Push — master ( 9676b0...44a032 )
by Anton
02:07
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
        $result = [];
39
        foreach ($data as $item) {
40
            $result[] = $this->orm->make($this->target, $item, Node::MANAGED);
41
        }
42
43
        return [new ArrayCollection($result), $result];
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 $parentNode): array
69
    {
70
        if (empty($innerKey = $this->fetchKey($parentNode, $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 $parentStore, $parentEntity, Node $parentNode, $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($parentNode, $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   $parentNode
130
     * @param object $related
131
     * @return CC
132
     */
133
    protected function queueStore(Node $parentNode, $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 $relNode 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($parentNode, $this->innerKey, $relStore, $relNode, $this->outerKey);
140
141
        return $relStore;
142
    }
143
144
    /**
145
     * Remove one of related objects.
146
     *
147
     * @param object $related
148
     * @return CommandInterface
149
     */
150
    protected function queueDelete($related): CommandInterface
151
    {
152
        $relNode = $this->getNode($related);
153
154
        if (!$this->isRequired()) {
155
            $store = $this->orm->queueStore($related);
156
            $store->register($this->outerKey, null, true);
157
            $relNode->getState()->decClaim();
158
159
            return new Condition($store, function () use ($relNode) {
160
                return !$relNode->getState()->hasClaims();
161
            });
162
        }
163
164
        return new Condition($this->orm->queueDelete($related), function () use ($relNode) {
165
            return !$relNode->getState()->hasClaims();
166
        });
167
    }
168
}