Passed
Pull Request — master (#10)
by Maksim
02:53
created

CompositeRelationships::newMorphOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 5
1
<?php
2
3
namespace MaksimM\CompositePrimaryKeys\Http\Traits;
4
5
use Closure;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
10
use Illuminate\Database\Eloquent\Relations\HasMany;
11
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
12
use Illuminate\Database\Eloquent\Relations\HasOne;
13
use Illuminate\Database\Eloquent\Relations\MorphMany;
14
use Illuminate\Database\Eloquent\Relations\MorphOne;
15
use Illuminate\Database\Eloquent\Relations\MorphTo;
16
use Illuminate\Database\Eloquent\Relations\MorphToMany;
17
use Illuminate\Support\Str;
18
use MaksimM\CompositePrimaryKeys\Eloquent\Relationships\CompositeBelongsTo;
19
20
trait CompositeRelationships
21
{
22
    /**
23
     * @param      $related
24
     * @param null $foreignKey
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $foreignKey is correct as it would always require null to be passed?
Loading history...
25
     * @param null $ownerKey
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $ownerKey is correct as it would always require null to be passed?
Loading history...
26
     * @param null $relation
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $relation is correct as it would always require null to be passed?
Loading history...
27
     *
28
     * @return BelongsTo
29
     */
30
    public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null)
31
    {
32
        // If no relation name was given, we will use this debug backtrace to extract
33
        // the calling method's name and use that as the relationship name as most
34
        // of the time this will be what we desire to use for the relationships.
35
        if (is_null($relation)) {
0 ignored issues
show
introduced by
The condition is_null($relation) is always true.
Loading history...
36
            $relation = $this->guessBelongsToRelation();
0 ignored issues
show
Bug introduced by
It seems like guessBelongsToRelation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

36
            /** @scrutinizer ignore-call */ 
37
            $relation = $this->guessBelongsToRelation();
Loading history...
37
        }
38
39
        /**
40
         * @var Model $instance
41
         */
42
        $instance = $this->newRelatedInstance($related);
0 ignored issues
show
Bug introduced by
It seems like newRelatedInstance() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

42
        /** @scrutinizer ignore-call */ 
43
        $instance = $this->newRelatedInstance($related);
Loading history...
43
44
        // If no foreign key was supplied, we can use a backtrace to guess the proper
45
        // foreign key name by using the name of the relationship function, which
46
        // when combined with an "_id" should conventionally match the columns.
47
        if (is_null($foreignKey)) {
0 ignored issues
show
introduced by
The condition is_null($foreignKey) is always true.
Loading history...
48
            $foreignKey = is_array($instance->getKeyName()) ? array_map(
0 ignored issues
show
introduced by
The condition is_array($instance->getKeyName()) is always false.
Loading history...
49
                function ($key) use ($relation) {
50
                    return Str::snake($relation).'_'.$key;
51
                },
52
                $instance->getKeyName()
53
            ) : Str::snake($relation).'_'.$instance->getKeyName();
54
        }
55
56
        // Once we have the foreign key names, we'll just create a new Eloquent query
57
        // for the related models and returns the relationship instance which will
58
        // actually be responsible for retrieving and hydrating every relations.
59
        $ownerKey = $ownerKey ?: $instance->getKeyName();
0 ignored issues
show
introduced by
$ownerKey is of type null, thus it always evaluated to false.
Loading history...
60
61
        $relationQuery = $this->newBelongsTo(
62
            $instance->newQuery(),
63
            $this,
0 ignored issues
show
Bug introduced by
$this of type MaksimM\CompositePrimary...\CompositeRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $child of MaksimM\CompositePrimary...onships::newBelongsTo(). ( Ignorable by Annotation )

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

63
            /** @scrutinizer ignore-type */ $this,
Loading history...
64
            $foreignKey,
65
            $ownerKey,
66
            $relation
67
        );
68
69
        return $relationQuery;
70
    }
71
72
    protected function executeWithinOptionalBinaryTransformation(Closure $relation, ...$models){
73
        foreach ($models as $model) {
74
            if (method_exists($model, 'disableBinaryMutators')) {
75
                $model->disableBinaryMutators();
76
            }
77
        }
78
        $relationResult = $relation();
79
        foreach ($models as $model) {
80
            if (method_exists($model, 'enableBinaryMutators')) {
81
                $model->enableBinaryMutators();
82
            }
83
        }
84
        return $relationResult;
85
    }
86
87
88
    /**
89
     * Instantiate a new BelongsTo relationship.
90
     *
91
     * @param Builder $query
92
     * @param Model   $child
93
     * @param string  $foreignKey
94
     * @param string  $ownerKey
95
     * @param string  $relation
96
     *
97
     * @return BelongsTo
98
     */
99
    protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
100
    {
101
        return $this->executeWithinOptionalBinaryTransformation(function() use ($query, $child, $foreignKey, $ownerKey, $relation) {
102
            return new CompositeBelongsTo($query, $child, $foreignKey, $ownerKey, $relation);
103
        }, $query->getModel(), $child);
104
    }
105
106
    /**
107
     * Instantiate a new HasOne relationship.
108
     *
109
     * @param Builder $query
110
     * @param Model   $parent
111
     * @param  string $foreignKey
112
     * @param  string $localKey
113
     * @return HasOne
114
     */
115
    protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey)
116
    {
117
        return $this->executeWithinOptionalBinaryTransformation(function() use ($query, $parent, $foreignKey, $localKey) {
118
            return new HasOne($query, $parent, $foreignKey, $localKey);
119
        }, $query->getModel(), $parent);
120
    }
121
122
    /**
123
     * Instantiate a new MorphOne relationship.
124
     *
125
     * @param Builder $query
126
     * @param Model   $parent
127
     * @param  string $type
128
     * @param  string $id
129
     * @param  string $localKey
130
     * @return MorphOne
131
     */
132
    protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey)
133
    {
134
        return $this->executeWithinOptionalBinaryTransformation(function() use ($query, $parent, $type, $id, $localKey) {
135
            return new MorphOne($query, $parent, $type, $id, $localKey);
136
        }, $query->getModel(), $parent);
137
    }
138
139
    /**
140
     * Instantiate a new MorphTo relationship.
141
     *
142
     * @param Builder $query
143
     * @param Model   $parent
144
     * @param  string $foreignKey
145
     * @param  string $ownerKey
146
     * @param  string $type
147
     * @param  string $relation
148
     * @return MorphTo
149
     */
150
    protected function newMorphTo(Builder $query, Model $parent, $foreignKey, $ownerKey, $type, $relation)
151
    {
152
        return $this->executeWithinOptionalBinaryTransformation(function() use ($query, $parent, $foreignKey, $ownerKey, $type, $relation) {
153
            return new MorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation);
154
        }, $query->getModel(), $parent);
155
    }
156
157
    /**
158
     * Instantiate a new HasMany relationship.
159
     *
160
     * @param Builder $query
161
     * @param Model   $parent
162
     * @param  string $foreignKey
163
     * @param  string $localKey
164
     * @return HasMany
165
     */
166
    protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey)
167
    {
168
        return $this->executeWithinOptionalBinaryTransformation(function() use ($query, $parent, $foreignKey, $localKey) {
169
            return new HasMany($query, $parent, $foreignKey, $localKey);
170
        }, $query->getModel(), $parent);
171
    }
172
173
    /**
174
     * Instantiate a new HasManyThrough relationship.
175
     *
176
     * @param Builder $query
177
     * @param Model   $farParent
178
     * @param Model   $throughParent
179
     * @param  string $firstKey
180
     * @param  string $secondKey
181
     * @param  string $localKey
182
     * @param  string $secondLocalKey
183
     * @return HasManyThrough
184
     */
185
    protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
186
    {
187
        return $this->executeWithinOptionalBinaryTransformation(function() use ($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey) {
188
            return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
189
        }, $query->getModel(), $farParent);
190
    }
191
192
    /**
193
     * Instantiate a new MorphMany relationship.
194
     *
195
     * @param Builder $query
196
     * @param Model   $parent
197
     * @param  string $type
198
     * @param  string $id
199
     * @param  string $localKey
200
     * @return MorphMany
201
     */
202
    protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey)
203
    {
204
        return $this->executeWithinOptionalBinaryTransformation(function() use ($query, $parent, $type, $id, $localKey) {
205
            return new MorphMany($query, $parent, $type, $id, $localKey);
206
        }, $query->getModel(), $parent);
207
    }
208
209
    /**
210
     * Instantiate a new BelongsToMany relationship.
211
     *
212
     * @param Builder $query
213
     * @param Model   $parent
214
     * @param  string $table
215
     * @param  string $foreignPivotKey
216
     * @param  string $relatedPivotKey
217
     * @param  string $parentKey
218
     * @param  string $relatedKey
219
     * @param  string $relationName
220
     * @return BelongsToMany
221
     */
222
    protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey,
223
        $parentKey, $relatedKey, $relationName = null)
224
    {
225
        return $this->executeWithinOptionalBinaryTransformation(function() use ($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName) {
226
            return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName);
227
        }, $query->getModel(), $parent);
228
    }
229
230
    /**
231
     * Instantiate a new MorphToMany relationship.
232
     *
233
     * @param Builder $query
234
     * @param Model   $parent
235
     * @param  string $name
236
     * @param  string $table
237
     * @param  string $foreignPivotKey
238
     * @param  string $relatedPivotKey
239
     * @param  string $parentKey
240
     * @param  string $relatedKey
241
     * @param  string $relationName
242
     * @param  bool                                  $inverse
243
     * @return MorphToMany
244
     */
245
    protected function newMorphToMany(Builder $query, Model $parent, $name, $table, $foreignPivotKey,
246
        $relatedPivotKey, $parentKey, $relatedKey,
247
        $relationName = null, $inverse = false)
248
    {
249
        return $this->executeWithinOptionalBinaryTransformation(function() use ($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey,
250
            $relationName, $inverse) {
251
            return new MorphToMany($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey,
252
                $relationName, $inverse);
253
        }, $query->getModel(), $parent);
254
    }
255
}
256