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

RelationManager::morph_many()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 7
crap 1
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
    public function morphed_by_many(Model $model, $relationName, $related, $name, $table = null, $foreignPivotKey = null,
103
        $relatedPivotKey = null, $parentKey = null, $relatedKey = null)
104
    {
105 1
        return new AbstractRelation(['morphedByMany', $model, $relationName, [$related, $name, $table, $foreignPivotKey,
106 1
            $relatedPivotKey, $parentKey, $relatedKey, $relationName, ]]);
107
    }
108
109
    /**
110
     * @param string $relationName
111
     * @param string $related
112
     * @param null $foreignKey
113
     * @param null $localKey
114
     *
115
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
116
     */
117 3
    public function has_many(Model $model, $relationName, $related, $foreignKey = null, $localKey = null)
118
    {
119 3
        return new AbstractRelation(['hasMany', $model, $relationName, [$related, $foreignKey, $localKey]]);
120
    }
121
122
    /**
123
     * Define a one-to-one relationship.
124
     *
125
     * @param  string  $relationName
126
     * @param  string  $related
127
     * @param  string  $foreignKey
128
     * @param  string  $localKey
129
     *
130
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
131
     */
132 1
    public function has_one(Model $model, $relationName, $related, $foreignKey = null, $localKey = null)
133
    {
134 1
        return new AbstractRelation(['hasOne', $model, $relationName, [$related, $foreignKey, $localKey]]);
135
    }
136
137
    /**
138
     * @param string $relationName
139
     * @param string $related
140
     * @param null $foreignKey
141
     * @param null $ownerKey
142
     *
143
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
144
     */
145 1
    public function belongs_to(Model $model, $relationName, string $related, $foreignKey = null, $ownerKey = null)
146
    {
147 1
        if (is_null($foreignKey)) {
148
            $foreignKey = Str::snake($relationName).'_id';
149
        }
150
151 1
        return new AbstractRelation(['belongsTo', $model, $relationName, [$related, $foreignKey, $ownerKey, $relationName]]);
152
    }
153
154
    /**
155
     * Define a many-to-many relationship.
156
     *
157
     * @param  string  $relationName
158
     * @param  string  $related
159
     * @param  string  $table
160
     * @param  string  $foreignPivotKey
161
     * @param  string  $relatedPivotKey
162
     * @param  string  $parentKey
163
     * @param  string  $relatedKey
164
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
165
     */
166 1
    public function belongs_to_many(Model $model, $relationName, $related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null,
167
        $parentKey = null, $relatedKey = null)
168
    {
169 1
        $params = [$related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName];
170
171 1
        return new AbstractRelation(['belongsToMany', $model, $relationName, $params]);
172
    }
173
174
    /**
175
     * Define a polymorphic many-to-many relationship.
176
     *
177
     * @param  string  $relationName
178
     * @param  string  $related
179
     * @param  string  $name
180
     * @param  string  $table
181
     * @param  string  $foreignPivotKey
182
     * @param  string  $relatedPivotKey
183
     * @param  string  $parentKey
184
     * @param  string  $relatedKey
185
     * @param  bool  $inverse
186
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
187
     */
188 1
    public function morph_to_many(Model $model, $relationName, $related, $name, $table = null, $foreignPivotKey = null,
189
        $relatedPivotKey = null, $parentKey = null,
190
        $relatedKey = null, $inverse = false)
191
    {
192 1
        $params = [$related, $name, $table, $foreignPivotKey,
193 1
            $relatedPivotKey, $parentKey, $relatedKey, $inverse, $relationName, ];
194
195 1
        return new AbstractRelation(['morphToMany', $model, $relationName, $params]);
196
    }
197
198
    /**
199
     * Define a polymorphic one-to-many relationship.
200
     *
201
     * @param  string  $relationName
202
     * @param  string  $related
203
     * @param  string  $name
204
     * @param  string  $type
205
     * @param  string  $id
206
     * @param  string  $localKey
207
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
208
     */
209 1
    public function morph_many(Model $model, $relationName, $related, $name, $type = null, $id = null, $localKey = null)
210
    {
211 1
        $params = [$related, $name, $type, $id, $localKey];
212
213 1
        return new AbstractRelation(['morphMany', $model, $relationName, $params]);
214
    }
215
216
    /**
217
     * Define a polymorphic one-to-one relationship.
218
     *
219
     * @param  string  $relationName
220
     * @param  string  $related
221
     * @param  string  $name
222
     * @param  string  $type
223
     * @param  string  $id
224
     * @param  string  $localKey
225
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
226
     */
227
    public function morph_one(Model $model, $relationName, $related, $name, $type = null, $id = null, $localKey = null)
228
    {
229
        $params = [$related, $name, $type, $id, $localKey];
230
231
        return new AbstractRelation(['morphOne', $model, $relationName, $params]);
232
    }
233
234
    /**
235
     * Define a polymorphic, inverse one-to-one or many relationship.
236
     *
237
     * @param  string  $relationName
238
     * @param  string  $type
239
     * @param  string  $id
240
     * @param  string  $ownerKey
241
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
242
     */
243 1
    public function morph_to(Model $model, $relationName, $type = null, $id = null, $ownerKey = null)
244
    {
245 1
        $params = [$relationName, $type, $id, $ownerKey];
246
247 1
        return new AbstractRelation(['morphTo', $model, $relationName, $params]);
248
    }
249
250
    /**
251
     * Define a has-many-through relationship.
252
     *
253
     * @param  string  $relationName
254
     * @param  string  $related
255
     * @param  string  $through
256
     * @param  string|null  $firstKey
257
     * @param  string|null  $secondKey
258
     * @param  string|null  $localKey
259
     * @param  string|null  $secondLocalKey
260
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
261
     */
262 1
    public function has_many_through(Model $model, $relationName, $related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null)
263
    {
264 1
        $params = [$related, $through, $firstKey, $secondKey, $localKey, $secondLocalKey];
265
266 1
        return new AbstractRelation(['hasManyThrough', $model, $relationName, $params]);
267
    }
268
}
269