Test Failed
Pull Request — master (#13)
by Rail
04:55
created

RelationManager::removeRelation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Imanghafoori\Relativity;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Database\Eloquent\Model;
7
8
class RelationManager
9
{
10
    /**
11
     * @var RelationStore
12
     */
13
    protected $store;
14
15
    /**
16
     * Create a new instance
17
     */
18 9
    public function __construct(RelationStore $store)
19
    {
20 9
        $this->store = $store;
21 9
    }
22
23
    /**
24
     * @return RelationStore
25
     */
26
    public function getStore()
27
    {
28
        return $this->store;
29
    }
30
31
    /**
32
     * Retrieve all relations
33
     *
34
     * @return array
35
     */
36 1
    public function getDynamicRelations(Model $model)
37
    {
38 1
        return $this->store->all($model);
39
    }
40
41
    /**
42
     * Has the model the relation
43
     *
44
     * @param string
45
     *
46
     * @return bool
47
     */
48 1
    public function hasDynamicRelation(Model $model, string $relation)
49
    {
50 1
        return $this->store->has($model, $relation);
51
    }
52
53
    /**
54
     * Remove a relation from the model
55
     *
56
     * @param string $relation
57
     */
58 2
    public function removeRelation(Model $model, string $relation)
59
    {
60 2
        if (!$this->store->has($model, $relation)) {
61 1
            throw new Exceptions\UndefinedDynamicRelationException($relation);
62
        }
63
64 1
        $this->store->unset($model, $relation);
65 1
    }
66
67
    /**
68
     * Define a new relation
69
     *
70
     * @param string $relationType
71
     * @param string $relationName
72
     * @param array $data
73
     * @param array $constraints
74
     */
75 8
    public function defineRelation(Model $model, $relationType, $relationName, $data, $constraints)
76
    {
77
        $method = function () use ($relationType, $data, $constraints) {
78 6
            $relation = $this->{$relationType} (...$data);
79 6
            foreach ($constraints as $cons) {
80 1
                $relation = $relation->{$cons[0]}(...$cons[1]);
81
            }
82
83 6
            return $relation;
84 8
        };
85
86 8
        $this->store->set($model, $relationName, $method);
87 8
    }
88
    /**
89
     * Define a polymorphic, inverse many-to-many relationship.
90
     *
91
     * @param  string  $relationName
92
     * @param  string  $related
93
     * @param  string  $name
94
     * @param  string  $table
95
     * @param  string  $foreignPivotKey
96
     * @param  string  $relatedPivotKey
97
     * @param  string  $parentKey
98
     * @param  string  $relatedKey
99
     *
100
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
101
     */
102 1 View Code Duplication
    public function morphed_by_many(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
        Model $model,
104
        $relationName,
105
        $related,
106
        $name,
107
        $table = null,
108
        $foreignPivotKey = null,
109
        $relatedPivotKey = null,
110
        $parentKey = null,
111
        $relatedKey = null
112
    ) {
113 1
        return new AbstractRelation(['morphedByMany', $model, $relationName, [$related, $name, $table, $foreignPivotKey,
114 1
            $relatedPivotKey, $parentKey, $relatedKey, $relationName, ]]);
115
    }
116
117
    /**
118
     * @param string $relationName
119
     * @param string $related
120
     * @param null $foreignKey
121
     * @param null $localKey
122
     *
123
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
124
     */
125 3
    public function has_many(Model $model, $relationName, $related, $foreignKey = null, $localKey = null)
126
    {
127 3
        return new AbstractRelation(['hasMany', $model, $relationName, [$related, $foreignKey, $localKey]]);
128
    }
129
130
    /**
131
     * Define a one-to-one relationship.
132
     *
133
     * @param  string  $relationName
134
     * @param  string  $related
135
     * @param  string  $foreignKey
136
     * @param  string  $localKey
137
     *
138
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
139
     */
140 1
    public function has_one(Model $model, $relationName, $related, $foreignKey = null, $localKey = null)
141
    {
142 1
        return new AbstractRelation(['hasOne', $model, $relationName, [$related, $foreignKey, $localKey]]);
143
    }
144
145
    /**
146
     * @param string $relationName
147
     * @param string $related
148
     * @param null $foreignKey
149
     * @param null $ownerKey
150
     *
151
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
152
     */
153 1
    public function belongs_to(Model $model, $relationName, $related, $foreignKey = null, $ownerKey = null)
154
    {
155 1
        if (is_null($foreignKey)) {
156
            $foreignKey = Str::snake($relationName).'_id';
157
        }
158
159 1
        return new AbstractRelation(['belongsTo', $model, $relationName, [$related, $foreignKey, $ownerKey, $relationName]]);
160
    }
161
162
    /**
163
     * Define a many-to-many relationship.
164
     *
165
     * @param  string  $relationName
166
     * @param  string  $related
167
     * @param  string  $table
168
     * @param  string  $foreignPivotKey
169
     * @param  string  $relatedPivotKey
170
     * @param  string  $parentKey
171
     * @param  string  $relatedKey
172
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
173
     */
174 1 View Code Duplication
    public function belongs_to_many(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
        Model $model,
176
        $relationName,
177
        $related,
178
        $table = null,
179
        $foreignPivotKey = null,
180
        $relatedPivotKey = null,
181
        $parentKey = null,
182
        $relatedKey = null
183
    ) {
184 1
        $params = [$related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName];
185
186 1
        return new AbstractRelation(['belongsToMany', $model, $relationName, $params]);
187
    }
188
189
    /**
190
     * Define a polymorphic many-to-many relationship.
191
     *
192
     * @param  string  $relationName
193
     * @param  string  $related
194
     * @param  string  $name
195
     * @param  string  $table
196
     * @param  string  $foreignPivotKey
197
     * @param  string  $relatedPivotKey
198
     * @param  string  $parentKey
199
     * @param  string  $relatedKey
200
     * @param  bool  $inverse
201
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
202
     */
203 1
    public function morph_to_many(
204
        Model $model,
205
        $relationName,
206
        $related,
207
        $name,
208
        $table = null,
209
        $foreignPivotKey = null,
210
        $relatedPivotKey = null,
211
        $parentKey = null,
212
        $relatedKey = null,
213
        $inverse = false
214
    ) {
215 1
        $params = [$related, $name, $table, $foreignPivotKey,
216 1
            $relatedPivotKey, $parentKey, $relatedKey, $inverse, $relationName, ];
217
218 1
        return new AbstractRelation(['morphToMany', $model, $relationName, $params]);
219
    }
220
221
    /**
222
     * Define a polymorphic one-to-many relationship.
223
     *
224
     * @param  string  $relationName
225
     * @param  string  $related
226
     * @param  string  $name
227
     * @param  string  $type
228
     * @param  string  $id
229
     * @param  string  $localKey
230
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
231
     */
232 1
    public function morph_many(Model $model, $relationName, $related, $name, $type = null, $id = null, $localKey = null)
233
    {
234 1
        $params = [$related, $name, $type, $id, $localKey];
235
236 1
        return new AbstractRelation(['morphMany', $model, $relationName, $params]);
237
    }
238
239
    /**
240
     * Define a polymorphic one-to-one relationship.
241
     *
242
     * @param  string  $relationName
243
     * @param  string  $related
244
     * @param  string  $name
245
     * @param  string  $type
246
     * @param  string  $id
247
     * @param  string  $localKey
248
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
249
     */
250
    public function morph_one(Model $model, $relationName, $related, $name, $type = null, $id = null, $localKey = null)
251
    {
252
        $params = [$related, $name, $type, $id, $localKey];
253
254
        return new AbstractRelation(['morphOne', $model, $relationName, $params]);
255
    }
256
257
    /**
258
     * Define a polymorphic, inverse one-to-one or many relationship.
259
     *
260
     * @param  string  $relationName
261
     * @param  string  $type
262
     * @param  string  $id
263
     * @param  string  $ownerKey
264
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
265
     */
266 1
    public function morph_to(Model $model, $relationName, $type = null, $id = null, $ownerKey = null)
267
    {
268 1
        $params = [$relationName, $type, $id, $ownerKey];
269
270 1
        return new AbstractRelation(['morphTo', $model, $relationName, $params]);
271
    }
272
273
    /**
274
     * Define a has-many-through relationship.
275
     *
276
     * @param  string  $relationName
277
     * @param  string  $related
278
     * @param  string  $through
279
     * @param  string|null  $firstKey
280
     * @param  string|null  $secondKey
281
     * @param  string|null  $localKey
282
     * @param  string|null  $secondLocalKey
283
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
284
     */
285 1
    public function has_many_through(Model $model, $relationName, $related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null)
286
    {
287 1
        $params = [$related, $through, $firstKey, $secondKey, $localKey, $secondLocalKey];
288
289 1
        return new AbstractRelation(['hasManyThrough', $model, $relationName, $params]);
290
    }
291
}
292