Passed
Pull Request — 1.0.0-release (#16)
by Alex
02:46
created

UpdatesModelRelations   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 110
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isFillableRelation() 0 4 1
A getRelationType() 0 10 3
A updateToOneResourceRelationship() 0 5 1
B updateToManyResourceRelationship() 0 23 6
A updateResourceRelationships() 0 13 4
1
<?php
2
3
namespace Huntie\JsonApi\Http\Concerns;
4
5
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7
8
trait UpdatesModelRelations
9
{
10
    /**
11
     * The model relationships that can be updated.
12
     *
13
     * @var array
14
     */
15
    protected $fillableRelations = [];
16
17
    /**
18
     * Determine whether the given named relation can be updated on the model.
19
     *
20
     * @param string $relation
21
     */
22
    protected function isFillableRelation(string $relation): bool
23
    {
24
        return array_key_exists($relation, $this->fillableRelations);
25
    }
26
27
    /**
28
     * Determine the JSON API relation type for a named relation on the primary
29
     * resource.
30
     *
31
     * @param string $relation
32
     *
33
     * @return string|void
34
     */
35
    protected function getRelationType(string $relation)
0 ignored issues
show
Unused Code introduced by
The parameter $relation is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        $relation = $this->model->{$name}();
0 ignored issues
show
Bug introduced by
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
38
39
        if ($relation instanceof BelongsTo) {
40
            return 'To-One';
41
        } else if ($relation instanceof BelongsToMany) {
42
            return 'To-Many';
43
        }
44
    }
45
46
    /**
47
     * Update a named many-to-one relationship association on a model instance.
48
     *
49
     * http://jsonapi.org/format/#crud-updating-to-one-relationships
50
     *
51
     * @param Model  $record
52
     * @param string $relation
53
     * @param array  $data
54
     */
55
    protected function updateToOneResourceRelationship($record, string $relation, array $data)
56
    {
57
        $record->{$relation}()->associate($data['id']);
58
        $record->save();
59
    }
60
61
    /**
62
     * Update named many-to-many relationship entries on a model instance.
63
     *
64
     * http://jsonapi.org/format/#crud-updating-to-many-relationships
65
     *
66
     * @param Model  $record
67
     * @param string $relation
68
     * @param array  $data
69
     * @param string $method
70
     */
71
    protected function updateToManyResourceRelationship($record, string $relation, array $data, string $method)
72
    {
73
        $items = [];
74
75
        foreach ($data as $item) {
76
            if (isset($item['attributes'])) {
77
                $items[$item['id']] = $item['attributes'];
78
            } else {
79
                $items[] = $item['id'];
80
            }
81
        }
82
83
        switch ($method) {
84
            case 'PATCH':
85
                $record->{$relation}()->sync($items);
86
                break;
87
            case 'POST':
88
                $record->{$relation}()->sync($items, false);
89
                break;
90
            case 'DELETE':
91
                $record->{$relation}()->detach(array_keys($items));
92
        }
93
    }
94
95
    /**
96
     * Update one or more relationships on a model instance from an array of
97
     * named relationships and associated resource identifiers.
98
     *
99
     * http://jsonapi.org/format/#crud-updating-resource-relationships
100
     *
101
     * @param Model $record
102
     * @param array $relationships
103
     */
104
    protected function updateResourceRelationships($record, array $relationships)
105
    {
106
        $relationships = array_intersect_key($relationships, array_flip($this->fillableRelations));
107
108
        foreach ($relationships as $name => $relationship) {
109
            if ($this->getRelationType($relation) === 'To-One') {
0 ignored issues
show
Bug introduced by
The variable $relation does not exist. Did you mean $relationships?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
110
                $record->{$name}()->associate(array_get($relationship, 'data.id'));
111
                $record->save();
112
            } else if ($this->getRelationType($relation) === 'To-Many') {
0 ignored issues
show
Bug introduced by
The variable $relation does not exist. Did you mean $relationships?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
113
                $record->{$name}()->sync(array_pluck($relationship['data'], 'id'));
114
            }
115
        }
116
    }
117
}
118