Passed
Pull Request — master (#34)
by Vincent
07:01
created

MorphTo::associate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Bdf\Prime\Relations;
4
5
use Bdf\Prime\Collection\Indexer\EntityIndexer;
6
use Bdf\Prime\Collection\Indexer\EntityIndexerInterface;
7
use Bdf\Prime\Query\Contract\EntityJoinable;
8
use Bdf\Prime\Query\Contract\ReadOperation;
9
use Bdf\Prime\Query\Contract\WriteOperation;
10
11
/**
12
 * MorphTo
13
 */
14
class MorphTo extends BelongsTo
15
{
16
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $alias should have a doc-comment as per coding-style.
Loading history...
17
     * {@inheritdoc}
18
     *
19
     * The alias should have #[sub entity] at its end
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
20
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
21 6
    public function join($query, $alias = null)
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 0 found
Loading history...
22
    {
23 6
        $parts = explode('#', (string)$alias);
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after cast statement; 0 found
Loading history...
24
25 6
        if (!isset($parts[1])) {
26 1
            throw new \LogicException('Joins are not supported on polymorph without discriminator');
27
        }
28
29 5
        $this->loadDistantFromType(end($parts));
30
31
        //TODO should be in join clause
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
Coding Style introduced by
No space found before comment text; expected "// TODO should be in join clause" but found "//TODO should be in join clause"
Loading history...
32 5
        $query->where($this->getLocalAlias($query).$this->discriminator, $this->discriminatorValue);
33
34
        // Join to the real alias (i.e. without the discriminator)
35 5
        parent::join($query, $parts[0]);
36 5
    }
37
38
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $alias should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $discriminatorValue should have a doc-comment as per coding-style.
Loading history...
39
     * {@inheritdoc}
40
     */
41 5
    public function joinRepositories(EntityJoinable $query, $alias = null, $discriminatorValue = null)
0 ignored issues
show
Coding Style introduced by
The method parameter $query is never used
Loading history...
42
    {
43 5
        $this->loadDistantFromType($discriminatorValue);
44
45
        return [
46 5
            $alias => $this->relationRepository()
0 ignored issues
show
introduced by
A comma should follow the last multiline array item. Found: )
Loading history...
47
        ];
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    #[ReadOperation]
54 44
    public function load(EntityIndexerInterface $collection, array $with = [], $constraints = [], array $without = [])
55
    {
56 44
        if ($collection->empty()) {
57 9
            return;
58
        }
59
60 39
        $with = $this->rearrangeWith($with);
61 39
        $without = $this->rearrangeWithout($without);
62
63 39
        foreach ($collection->by($this->discriminator) as $type => $chunk) {
64 39
            $this->loadDistantFromType($type);
65
66 39
            parent::load(
67 39
                EntityIndexer::fromArray($this->local->mapper(), $chunk),
68 39
                $with[$type],
69 39
                $constraints,
70 39
                $without[$type]
71
            );
72
        }
73 38
    }
74
75
    /**
76
     * {@inheritdoc}
77
     *
78
     * @fixme Do not works with EntityCollection
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task
Loading history...
79
     *
80
     * @param object $owner
0 ignored issues
show
Coding Style introduced by
Parameter tags must be defined first in a doc comment
Loading history...
81
     */
82 2
    public function link($owner)
83
    {
84 2
        $this->loadDistantFrom($owner);
85
86 2
        return parent::link($owner);
87
    }
88
89
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $owner should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $entity should have a doc-comment as per coding-style.
Loading history...
90
     * {@inheritdoc}
91
     */
92 2
    public function associate($owner, $entity)
93
    {
94 2
        $this->loadDistantFromType($this->discriminator(get_class($entity)));
95
96 2
        return parent::associate($owner, $entity);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    #[WriteOperation]
103 4
    public function saveAll($owner, array $relations = [])
104
    {
105 4
        $relations = $this->rearrangeWith($relations);
106 4
        $this->loadDistantFrom($owner);
107
108 4
        return parent::saveAll($owner, $relations[$this->discriminatorValue]);
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    #[WriteOperation]
115 4
    public function deleteAll($owner, array $relations = [])
116
    {
117 4
        $relations = $this->rearrangeWith($relations);
118 4
        $this->loadDistantFrom($owner);
119
120 4
        return parent::deleteAll($owner, $relations[$this->discriminatorValue]);
121
    }
122
123
    /**
124
     * Change the current discriminator value 
125
     * and update the distant repository of this relation
126
     * 
127
     * @param mixed $type
128
     */
129 46
    protected function loadDistantFromType($type)
130
    {
131 46
        $this->discriminatorValue = $type;
132
133 46
        $this->updateDistantInfos();
134 46
    }
135
136
    /**
137
     * Change the current discriminator value from the entity
138
     * and update the distant repository of this relation
139
     * 
140
     * @param object $entity
141
     */
142 10
    protected function loadDistantFrom($entity)
143
    {
144 10
        $this->updateDiscriminatorValue($entity);
145
146 10
        $this->updateDistantInfos();
147 10
    }
148
149
    /**
150
     * Update the distant repository of this relation
151
     */
152 48
    protected function updateDistantInfos()
153
    {
154 48
        $infos = $this->map($this->discriminatorValue);
155
156 48
        $this->distant    = $this->local->repository($infos['entity']);
157 48
        $this->distantKey = $infos['distantKey'];
158
159 48
        $this->setConstraints(isset($infos['constraints']) ? $infos['constraints'] : []);
160 48
    }
161
162
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $keys should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $constraints should have a doc-comment as per coding-style.
Loading history...
163
     * {@inheritdoc}
164
     */
165 39
    protected function relationQuery($keys, $constraints)
166
    {
167 39
        return $this->query($keys, $constraints)->by($this->distantKey);
0 ignored issues
show
Bug introduced by
The method by() does not exist on Bdf\Prime\Query\QueryInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Bdf\Prime\Query\SqlQueryInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

167
        return $this->query($keys, $constraints)->/** @scrutinizer ignore-call */ by($this->distantKey);
Loading history...
168
    }
169
}
170