Passed
Push — master ( a738f1...b5a19b )
by Jonas
06:56
created

HasJsonRelationships::getAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 3
rs 9.9332
c 2
b 0
f 0
1
<?php
2
3
namespace Staudenmeir\EloquentJsonRelations;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8
use Illuminate\Database\Eloquent\Relations\HasMany;
9
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
10
use Illuminate\Database\Eloquent\Relations\HasOne;
11
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
12
use Illuminate\Database\Eloquent\Relations\MorphMany;
13
use Illuminate\Database\Eloquent\Relations\MorphOne;
14
use Illuminate\Support\Str;
15
use Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson;
16
use Staudenmeir\EloquentJsonRelations\Relations\HasManyJson;
17
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\BelongsTo as BelongsToPostgres;
18
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\HasMany as HasManyPostgres;
19
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\HasManyThrough as HasManyThroughPostgres;
20
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\HasOne as HasOnePostgres;
21
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\HasOneThrough as HasOneThroughPostgres;
22
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\MorphMany as MorphManyPostgres;
23
use Staudenmeir\EloquentJsonRelations\Relations\Postgres\MorphOne as MorphOnePostgres;
24
25
trait HasJsonRelationships
26
{
27
    /**
28
     * Get an attribute from the model.
29
     *
30
     * @param string $key
31
     * @return mixed
32
     */
33 284
    public function getAttribute($key)
34
    {
35 284
        if (!Str::contains($key, '->')) {
36 281
            return parent::getAttribute($key);
37
        }
38
39 162
        [$attribute, $path] = explode('->', $key, 2);
40
41 162
        if (substr($attribute, -2) === '[]') {
42 3
            $attribute = substr($attribute, 0, -2);
43
44 3
            $path = '*.'.$path;
45
        }
46
47 162
        $path = str_replace(['->', '[]'], ['.', '.*'], $path);
48
49 162
        return data_get(
50 162
            $this->getAttributeValue($attribute),
0 ignored issues
show
Bug introduced by
The method getAttributeValue() does not exist on Staudenmeir\EloquentJson...ns\HasJsonRelationships. Did you maybe mean getAttribute()? ( Ignorable by Annotation )

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

50
            $this->/** @scrutinizer ignore-call */ 
51
                   getAttributeValue($attribute),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
            $path
52
        );
53
    }
54
55
    /**
56
     * Instantiate a new HasOne relationship.
57
     *
58
     * @param \Illuminate\Database\Eloquent\Builder $query
59
     * @param \Illuminate\Database\Eloquent\Model $parent
60
     * @param string $foreignKey
61
     * @param string $localKey
62
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
63
     */
64 24
    protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey)
65
    {
66 24
        if ($query->getConnection()->getDriverName() === 'pgsql') {
67 6
            return new HasOnePostgres($query, $parent, $foreignKey, $localKey);
68
        }
69
70 18
        return new HasOne($query, $parent, $foreignKey, $localKey);
71
    }
72
73
    /**
74
     * Instantiate a new HasOneThrough relationship.
75
     *
76
     * @param \Illuminate\Database\Eloquent\Builder $query
77
     * @param \Illuminate\Database\Eloquent\Model $farParent
78
     * @param \Illuminate\Database\Eloquent\Model $throughParent
79
     * @param string $firstKey
80
     * @param string $secondKey
81
     * @param string $localKey
82
     * @param string $secondLocalKey
83
     * @return \Illuminate\Database\Eloquent\Relations\HasOneThrough
84
     */
85 24
    protected function newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
86
    {
87 24
        if ($query->getConnection()->getDriverName() === 'pgsql') {
88 6
            return new HasOneThroughPostgres($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
89
        }
90
91 18
        return new HasOneThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
92
    }
93
94
    /**
95
     * Instantiate a new MorphOne relationship.
96
     *
97
     * @param \Illuminate\Database\Eloquent\Builder $query
98
     * @param \Illuminate\Database\Eloquent\Model $parent
99
     * @param string $type
100
     * @param string $id
101
     * @param string $localKey
102
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
103
     */
104 20
    protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey)
105
    {
106 20
        if ($query->getConnection()->getDriverName() === 'pgsql') {
107 5
            return new MorphOnePostgres($query, $parent, $type, $id, $localKey);
108
        }
109
110 15
        return new MorphOne($query, $parent, $type, $id, $localKey);
111
    }
112
113
    /**
114
     * Instantiate a new BelongsTo relationship.
115
     *
116
     * @param \Illuminate\Database\Eloquent\Builder $query
117
     * @param \Illuminate\Database\Eloquent\Model $child
118
     * @param string $foreignKey
119
     * @param string $ownerKey
120
     * @param string $relation
121
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
122
     */
123 36
    protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
124
    {
125 36
        if ($query->getConnection()->getDriverName() === 'pgsql') {
126 9
            return new BelongsToPostgres($query, $child, $foreignKey, $ownerKey, $relation);
127
        }
128
129 27
        return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation);
130
    }
131
132
    /**
133
     * Instantiate a new HasMany relationship.
134
     *
135
     * @param \Illuminate\Database\Eloquent\Builder $query
136
     * @param \Illuminate\Database\Eloquent\Model $parent
137
     * @param string $foreignKey
138
     * @param string $localKey
139
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
140
     */
141 24
    protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey)
142
    {
143 24
        if ($query->getConnection()->getDriverName() === 'pgsql') {
144 6
            return new HasManyPostgres($query, $parent, $foreignKey, $localKey);
145
        }
146
147 18
        return new HasMany($query, $parent, $foreignKey, $localKey);
148
    }
149
150
    /**
151
     * Instantiate a new HasManyThrough relationship.
152
     *
153
     * @param \Illuminate\Database\Eloquent\Builder $query
154
     * @param \Illuminate\Database\Eloquent\Model $farParent
155
     * @param \Illuminate\Database\Eloquent\Model $throughParent
156
     * @param string $firstKey
157
     * @param string $secondKey
158
     * @param string $localKey
159
     * @param string $secondLocalKey
160
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
161
     */
162 24
    protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
163
    {
164 24
        if ($query->getConnection()->getDriverName() === 'pgsql') {
165 6
            return new HasManyThroughPostgres($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
166
        }
167
168 18
        return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
169
    }
170
171
    /**
172
     * Instantiate a new MorphMany relationship.
173
     *
174
     * @param \Illuminate\Database\Eloquent\Builder $query
175
     * @param \Illuminate\Database\Eloquent\Model $parent
176
     * @param string $type
177
     * @param string $id
178
     * @param string $localKey
179
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
180
     */
181 20
    protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey)
182
    {
183 20
        if ($query->getConnection()->getDriverName() === 'pgsql') {
184 5
            return new MorphManyPostgres($query, $parent, $type, $id, $localKey);
185
        }
186
187 15
        return new MorphMany($query, $parent, $type, $id, $localKey);
188
    }
189
190
    /**
191
     * Define an inverse one-to-one or many JSON relationship.
192
     *
193
     * @param string $related
194
     * @param string $foreignKey
195
     * @param string $ownerKey
196
     * @param string $relation
197
     * @return \Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson
198
     */
199 70
    public function belongsToJson($related, $foreignKey, $ownerKey = null, $relation = null)
200
    {
201 70
        if (is_null($relation)) {
202 70
            $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

202
            /** @scrutinizer ignore-call */ 
203
            $relation = $this->guessBelongsToRelation();
Loading history...
203
        }
204
205
        /** @var \Illuminate\Database\Eloquent\Model $instance */
206 70
        $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

206
        /** @scrutinizer ignore-call */ 
207
        $instance = $this->newRelatedInstance($related);
Loading history...
207
208 70
        $ownerKey = $ownerKey ?: $instance->getKeyName();
209
210 70
        return $this->newBelongsToJson(
211 70
            $instance->newQuery(),
212
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\EloquentJson...ns\HasJsonRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $child of Staudenmeir\EloquentJson...ips::newBelongsToJson(). ( Ignorable by Annotation )

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

212
            /** @scrutinizer ignore-type */ $this,
Loading history...
213
            $foreignKey,
214
            $ownerKey,
215
            $relation
216
        );
217
    }
218
219
    /**
220
     * Instantiate a new BelongsToJson relationship.
221
     *
222
     * @param \Illuminate\Database\Eloquent\Builder $query
223
     * @param \Illuminate\Database\Eloquent\Model $child
224
     * @param string $foreignKey
225
     * @param string $ownerKey
226
     * @param string $relation
227
     * @return \Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson
228
     */
229 70
    protected function newBelongsToJson(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
230
    {
231 70
        return new BelongsToJson($query, $child, $foreignKey, $ownerKey, $relation);
232
    }
233
234
    /**
235
     * Define a one-to-many JSON relationship.
236
     *
237
     * @param string $related
238
     * @param string $foreignKey
239
     * @param string $localKey
240
     * @return \Staudenmeir\EloquentJsonRelations\Relations\HasManyJson
241
     */
242 36
    public function hasManyJson($related, $foreignKey, $localKey = null)
243
    {
244
        /** @var \Illuminate\Database\Eloquent\Model $instance */
245 36
        $instance = $this->newRelatedInstance($related);
246
247 36
        $localKey = $localKey ?: $this->getKeyName();
0 ignored issues
show
Bug introduced by
It seems like getKeyName() 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

247
        $localKey = $localKey ?: $this->/** @scrutinizer ignore-call */ getKeyName();
Loading history...
248
249 36
        return $this->newHasManyJson(
250 36
            $instance->newQuery(),
251
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\EloquentJson...ns\HasJsonRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\EloquentJson...ships::newHasManyJson(). ( Ignorable by Annotation )

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

251
            /** @scrutinizer ignore-type */ $this,
Loading history...
252 36
            $instance->getTable().'.'.$foreignKey,
253
            $localKey
254
        );
255
    }
256
257
    /**
258
     * Instantiate a new HasManyJson relationship.
259
     *
260
     * @param \Illuminate\Database\Eloquent\Builder $query
261
     * @param \Illuminate\Database\Eloquent\Model $parent
262
     * @param string $foreignKey
263
     * @param string $localKey
264
     * @return \Staudenmeir\EloquentJsonRelations\Relations\HasManyJson
265
     */
266 36
    protected function newHasManyJson(Builder $query, Model $parent, $foreignKey, $localKey)
267
    {
268 36
        return new HasManyJson($query, $parent, $foreignKey, $localKey);
269
    }
270
}
271