Passed
Push — master ( d5b6e7...4445f2 )
by Jonas
12:45 queued 12s
created

hasOneOrManyDeepFromHasOneOrMany()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 22
ccs 11
cts 11
cp 1
crap 2
rs 9.9666
1
<?php
2
3
namespace Staudenmeir\EloquentHasManyDeep\Eloquent\Traits;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
8
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
9
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
10
use Illuminate\Database\Eloquent\Relations\MorphToMany;
11
use Illuminate\Database\Eloquent\Relations\Relation;
12
use RuntimeException;
13
use Staudenmeir\EloquentHasManyDeep\Eloquent\CompositeKey;
14
15
trait ConcatenatesNativeRelationships
16
{
17
    /**
18
     * Prepare a has-one-deep or has-many-deep relationship from an existing belongs-to relationship.
19
     *
20
     * @param \Illuminate\Database\Eloquent\Relations\BelongsTo $relation
21
     * @param \Illuminate\Database\Eloquent\Model[] $through
22
     * @param array $foreignKeys
23
     * @param array $localKeys
24
     * @return array
25
     */
26 6
    protected function hasOneOrManyDeepFromBelongsTo(
27
        BelongsTo $relation,
28
        array $through,
29
        array $foreignKeys,
30
        array $localKeys
31
    ) {
32 6
        if (is_array($relation->getOwnerKeyName())) {
33
            // https://github.com/topclaudy/compoships
34 2
            $foreignKeys[] = new CompositeKey(
35 2
                ...(array)$relation->getOwnerKeyName()
36 2
            );
37
38 2
            $localKeys[] = new CompositeKey(
39 2
                ...(array)$relation->getForeignKeyName()
40 2
            );
41
        } else {
42 4
            $foreignKeys[] = $relation->getOwnerKeyName();
43
44 4
            $localKeys[] = $relation->getForeignKeyName();
45
        }
46
47 6
        return [$through, $foreignKeys, $localKeys];
48
    }
49
50
    /**
51
     * Prepare a has-one-deep or has-many-deep relationship from an existing belongs-to-many relationship.
52
     *
53
     * @param \Illuminate\Database\Eloquent\Relations\BelongsToMany $relation
54
     * @param \Illuminate\Database\Eloquent\Model[] $through
55
     * @param array $foreignKeys
56
     * @param array $localKeys
57
     * @return array
58
     */
59 2
    protected function hasOneOrManyDeepFromBelongsToMany(
60
        BelongsToMany $relation,
61
        array $through,
62
        array $foreignKeys,
63
        array $localKeys
64
    ) {
65 2
        $through[] = $relation->getTable();
66
67 2
        $foreignKeys[] = $relation->getForeignPivotKeyName();
68 2
        $foreignKeys[] = $relation->getRelatedKeyName();
69
70 2
        $localKeys[] = $relation->getParentKeyName();
71 2
        $localKeys[] = $relation->getRelatedPivotKeyName();
72
73 2
        return [$through, $foreignKeys, $localKeys];
74
    }
75
76
    /**
77
     * Prepare a has-one-deep or has-many-deep relationship from an existing has-one or has-many relationship.
78
     *
79
     * @param \Illuminate\Database\Eloquent\Relations\HasOneOrMany $relation
80
     * @param \Illuminate\Database\Eloquent\Model[] $through
81
     * @param array $foreignKeys
82
     * @param array $localKeys
83
     * @return array
84
     */
85 78
    protected function hasOneOrManyDeepFromHasOneOrMany(
86
        HasOneOrMany $relation,
87
        array $through,
88
        array $foreignKeys,
89
        array $localKeys
90
    ) {
91 78
        if (is_array($relation->getForeignKeyName())) {
92
            // https://github.com/topclaudy/compoships
93 2
            $foreignKeys[] = new CompositeKey(
94 2
                ...(array)$relation->getForeignKeyName()
95 2
            );
96
97 2
            $localKeys[] = new CompositeKey(
98 2
                ...(array)$relation->getLocalKeyName()
99 2
            );
100
        } else {
101 76
            $foreignKeys[] = $relation->getForeignKeyName();
102
103 76
            $localKeys[] = $relation->getLocalKeyName();
104
        }
105
106 78
        return [$through, $foreignKeys, $localKeys];
107
    }
108
109
    /**
110
     * Prepare a has-one-deep or has-many-deep relationship from an existing has-many-through relationship.
111
     *
112
     * @param \Illuminate\Database\Eloquent\Relations\HasManyThrough $relation
113
     * @param \Illuminate\Database\Eloquent\Model[] $through
114
     * @param array $foreignKeys
115
     * @param array $localKeys
116
     * @return array
117
     */
118 22
    protected function hasOneOrManyDeepFromHasManyThrough(
119
        HasManyThrough $relation,
120
        array $through,
121
        array $foreignKeys,
122
        array $localKeys
123
    ) {
124 22
        $through[] = get_class($relation->getParent());
125
126 22
        $foreignKeys[] = $relation->getFirstKeyName();
127 22
        $foreignKeys[] = $relation->getForeignKeyName();
128
129 22
        $localKeys[] = $relation->getLocalKeyName();
130 22
        $localKeys[] = $relation->getSecondLocalKeyName();
131
132 22
        return [$through, $foreignKeys, $localKeys];
133
    }
134
135
    /**
136
     * Prepare a has-one-deep or has-many-deep relationship from an existing morph-one or morph-many relationship.
137
     *
138
     * @param \Illuminate\Database\Eloquent\Relations\MorphOneOrMany $relation
139
     * @param \Illuminate\Database\Eloquent\Model[] $through
140
     * @param array $foreignKeys
141
     * @param array $localKeys
142
     * @return array
143
     */
144 2
    protected function hasOneOrManyDeepFromMorphOneOrMany(
145
        MorphOneOrMany $relation,
146
        array $through,
147
        array $foreignKeys,
148
        array $localKeys
149
    ) {
150 2
        $foreignKeys[] = [$relation->getQualifiedMorphType(), $relation->getForeignKeyName()];
151
152 2
        $localKeys[] = $relation->getLocalKeyName();
153
154 2
        return [$through, $foreignKeys, $localKeys];
155
    }
156
157
    /**
158
     * Prepare a has-one-deep or has-many-deep relationship from an existing morph-to-many relationship.
159
     *
160
     * @param \Illuminate\Database\Eloquent\Relations\MorphToMany $relation
161
     * @param \Illuminate\Database\Eloquent\Model[] $through
162
     * @param array $foreignKeys
163
     * @param array $localKeys
164
     * @return array
165
     */
166 4
    protected function hasOneOrManyDeepFromMorphToMany(
167
        MorphToMany $relation,
168
        array $through,
169
        array $foreignKeys,
170
        array $localKeys
171
    ) {
172 4
        $through[] = $relation->getTable();
173
174 4
        if ($relation->getInverse()) {
175 2
            $foreignKeys[] = $relation->getForeignPivotKeyName();
176 2
            $foreignKeys[] = $relation->getRelatedKeyName();
177
178 2
            $localKeys[] = $relation->getParentKeyName();
179 2
            $localKeys[] = [$relation->getMorphType(), $relation->getRelatedPivotKeyName()];
180
        } else {
181 2
            $foreignKeys[] = [$relation->getMorphType(), $relation->getForeignPivotKeyName()];
182 2
            $foreignKeys[] = $relation->getRelatedKeyName();
183
184 2
            $localKeys[] = $relation->getParentKeyName();
185 2
            $localKeys[] = $relation->getRelatedPivotKeyName();
186
        }
187
188 4
        return [$through, $foreignKeys, $localKeys];
189
    }
190
191
    /**
192
     * Get the relationship method name.
193
     *
194
     * @param \Illuminate\Database\Eloquent\Relations\Relation $relation
195
     * @return string
196
     */
197 80
    protected function hasOneOrManyDeepRelationMethod(Relation $relation)
198
    {
199 80
        $classes = [
200 80
            BelongsTo::class,
201 80
            HasManyThrough::class,
202 80
            MorphOneOrMany::class,
203 80
            HasOneOrMany::class,
204 80
            MorphToMany::class,
205 80
            BelongsToMany::class,
206 80
        ];
207
208 80
        foreach ($classes as $class) {
209 80
            if ($relation instanceof $class) {
210 80
                return 'hasOneOrManyDeepFrom' . class_basename($class);
211
            }
212
        }
213
214
        throw new RuntimeException('This relationship is not supported.'); // @codeCoverageIgnore
215
    }
216
}
217