Completed
Pull Request — master (#9)
by Rail
04:09
created

DynamicRelations::morph_to_many()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 9
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Imanghafoori\Relativity;
4
5
use Illuminate\Support\Str;
6
use Imanghafoori\Relativity\Exceptions;
7
8
trait DynamicRelations
9
{
10
    use BaseEloquentOverrides;
11
12
    protected static $dynamicRelations = [];
13
14 1
    public function getDynamicRelations()
15
    {
16 1
        return static::$dynamicRelations;
17
    }
18
19 1
    public function hasDynamicRelation(string $relation)
20
    {
21 1
        return isset(static::$dynamicRelations[$relation]);
22
    }
23
24 2
    public static function removeRelation(string $relationName)
25
    {
26 2
        if (!isset(static::$dynamicRelations[$relationName])) {
27 1
            throw new Exceptions\UndefinedDynamicRelationException($relationName);
28
        }
29
30 1
        unset(static::$dynamicRelations[$relationName]);
31 1
    }
32
33 8
    public static function defineRelation($relationType, $relationName, $data, $constraints)
34
    {
35
        $method = function () use ($relationType, $data, $constraints) {
36 7
            $relation = $this->{$relationType} (...$data);
37 7
            foreach ($constraints as $cons) {
38 1
                $relation = $relation->{$cons[0]}(...$cons[1]);
39
            }
40
41 7
            return $relation;
42 8
        };
43
44 8
        static::$dynamicRelations[$relationName] = $method;
45 8
    }
46
47
    /**
48
     * Dynamically handle calls to the class.
49
     *
50
     * @param  string  $method
51
     * @param  array   $parameters
52
     * @return mixed
53
     *
54
     * @throws \BadMethodCallException
55
     */
56 7
    public function __call($method, $parameters)
57
    {
58 7
        $dynamicRelation = static::$dynamicRelations[$method] ?? null;
59 7
        if (! $dynamicRelation) {
60 7
            return parent::__call($method, $parameters);
61
        }
62
63 7
        return call_user_func_array($dynamicRelation->bindTo($this, static::class), $parameters);
64
    }
65
66
    /**
67
     * Define a polymorphic, inverse many-to-many relationship.
68
     *
69
     * @param  string  $relationName
70
     * @param  string  $related
71
     * @param  string  $name
72
     * @param  string  $table
73
     * @param  string  $foreignPivotKey
74
     * @param  string  $relatedPivotKey
75
     * @param  string  $parentKey
76
     * @param  string  $relatedKey
77
     *
78
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
79
     */
80 1
    public static function morphed_by_many($relationName, $related, $name, $table = null, $foreignPivotKey = null,
81
        $relatedPivotKey = null, $parentKey = null, $relatedKey = null)
82
    {
83 1
        return new AbstractRelation(['morphedByMany', static::class, $relationName, [$related, $name, $table, $foreignPivotKey,
84 1
            $relatedPivotKey, $parentKey, $relatedKey, $relationName, ]]);
85
    }
86
87
    /**
88
     * @param string $relationName
89
     * @param string $related
90
     * @param null $foreignKey
91
     * @param null $localKey
92
     *
93
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
94
     */
95 3
    public static function has_many($relationName, $related, $foreignKey = null, $localKey = null)
96
    {
97 3
        return new AbstractRelation(['hasMany', static::class, $relationName, [$related, $foreignKey, $localKey]]);
98
    }
99
100
    /**
101
     * Define a one-to-one relationship.
102
     *
103
     * @param  string  $relationName
104
     * @param  string  $related
105
     * @param  string  $foreignKey
106
     * @param  string  $localKey
107
     *
108
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
109
     */
110 1
    public static function has_one($relationName, $related, $foreignKey = null, $localKey = null)
111
    {
112 1
        return new AbstractRelation(['hasOne', static::class, $relationName, [$related, $foreignKey, $localKey]]);
113
    }
114
115
    /**
116
     * @param string $relationName
117
     * @param string $related
118
     * @param null $foreignKey
119
     * @param null $ownerKey
120
     *
121
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
122
     */
123 1
    public static function belongs_to($relationName, string $related, $foreignKey = null, $ownerKey = null)
124
    {
125 1
        if (is_null($foreignKey)) {
126
            $foreignKey = Str::snake($relationName).'_id';
127
        }
128
129 1
        return new AbstractRelation(['belongsTo', static::class, $relationName, [$related, $foreignKey, $ownerKey, $relationName]]);
130
    }
131
132
    /**
133
     * Define a many-to-many relationship.
134
     *
135
     * @param  string  $relationName
136
     * @param  string  $related
137
     * @param  string  $table
138
     * @param  string  $foreignPivotKey
139
     * @param  string  $relatedPivotKey
140
     * @param  string  $parentKey
141
     * @param  string  $relatedKey
142
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
143
     */
144 1
    public static function belongs_to_many($relationName, $related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null,
145
        $parentKey = null, $relatedKey = null)
146
    {
147 1
        $params = [$related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName];
148
149 1
        return new AbstractRelation(['belongsToMany', static::class, $relationName, $params]);
150
    }
151
152
    /**
153
     * Define a polymorphic many-to-many relationship.
154
     *
155
     * @param  string  $relationName
156
     * @param  string  $related
157
     * @param  string  $name
158
     * @param  string  $table
159
     * @param  string  $foreignPivotKey
160
     * @param  string  $relatedPivotKey
161
     * @param  string  $parentKey
162
     * @param  string  $relatedKey
163
     * @param  bool  $inverse
164
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
165
     */
166 1
    public static function morph_to_many($relationName, $related, $name, $table = null, $foreignPivotKey = null,
167
        $relatedPivotKey = null, $parentKey = null,
168
        $relatedKey = null, $inverse = false)
169
    {
170 1
        $params = [$related, $name, $table, $foreignPivotKey,
171 1
            $relatedPivotKey, $parentKey, $relatedKey, $inverse, $relationName, ];
172
173 1
        return new AbstractRelation(['morphToMany', static::class, $relationName, $params]);
174
    }
175
176
    /**
177
     * Define a polymorphic one-to-many relationship.
178
     *
179
     * @param  string  $relationName
180
     * @param  string  $related
181
     * @param  string  $name
182
     * @param  string  $type
183
     * @param  string  $id
184
     * @param  string  $localKey
185
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
186
     */
187 1
    public static function morph_many($relationName, $related, $name, $type = null, $id = null, $localKey = null)
188
    {
189 1
        $params = [$related, $name, $type, $id, $localKey];
190
191 1
        return new AbstractRelation(['morphMany', static::class, $relationName, $params]);
192
    }
193
194
    /**
195
     * Define a polymorphic one-to-one relationship.
196
     *
197
     * @param  string  $relationName
198
     * @param  string  $related
199
     * @param  string  $name
200
     * @param  string  $type
201
     * @param  string  $id
202
     * @param  string  $localKey
203
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
204
     */
205
    public static function morph_one($relationName, $related, $name, $type = null, $id = null, $localKey = null)
206
    {
207
        $params = [$related, $name, $type, $id, $localKey];
208
209
        return new AbstractRelation(['morphOne', static::class, $relationName, $params]);
210
    }
211
212
    /**
213
     * Define a polymorphic, inverse one-to-one or many relationship.
214
     *
215
     * @param  string  $relationName
216
     * @param  string  $type
217
     * @param  string  $id
218
     * @param  string  $ownerKey
219
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
220
     */
221 1
    public static function morph_to($relationName, $type = null, $id = null, $ownerKey = null)
222
    {
223 1
        $params = [$relationName, $type, $id, $ownerKey];
224
225 1
        return new AbstractRelation(['morphTo', static::class, $relationName, $params]);
226
    }
227
228
    /**
229
     * Define a has-many-through relationship.
230
     *
231
     * @param  string  $relationName
232
     * @param  string  $related
233
     * @param  string  $through
234
     * @param  string|null  $firstKey
235
     * @param  string|null  $secondKey
236
     * @param  string|null  $localKey
237
     * @param  string|null  $secondLocalKey
238
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
239
     */
240 1
    public static function has_many_through($relationName, $related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null)
241
    {
242 1
        $params = [$related, $through, $firstKey, $secondKey, $localKey, $secondLocalKey];
243
244 1
        return new AbstractRelation(['hasManyThrough', static::class, $relationName, $params]);
245
    }
246
247 1
    public static function forceEagerLoading(...$relation)
248
    {
249
        static::registerModelEvent('booting', function ($model) use ($relation) {
250 1
            $model->with = $model->with + $relation;
251 1
        });
252 1
    }
253
}
254