GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — merge-dev-to-master ( fdc6fe )
by
unknown
11:18
created

ElementSaveRelation::afterSave()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 24
rs 9.2222
1
<?php
2
3
namespace SleepingOwl\Admin\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Collection;
7
use SleepingOwl\Admin\Contracts\Form\Element\Taggable;
8
use SleepingOwl\Admin\Contracts\Form\Element\HasSyncCallback;
9
use SleepingOwl\Admin\Contracts\Form\Element\MustDeleteRelatedItem;
10
11
trait ElementSaveRelation
12
{
13
    /**
14
     * @return array|string
15
     */
16
    public function getValueFromModel()
17
    {
18
        $value = parent::getValueFromModel();
19
20
        if (is_array($value)) {
21
            foreach ($value as $key => $val) {
22
                $value[$key] = $val;
23
            }
24
        }
25
26
        if ($value instanceof Collection && $value->count() > 0) {
27
            $value = $value->pluck($value->first()->getKeyName())->all();
28
        }
29
30
        if ($value instanceof Collection) {
31
            $value = $value->toArray();
32
        }
33
34
        return $value;
35
    }
36
37
    /**
38
     * @param \Illuminate\Http\Request $request
39
     *
40
     * @return void
41
     */
42
    public function save(\Illuminate\Http\Request $request)
43
    {
44
        if (is_null($this->getModelForOptions())) {
0 ignored issues
show
Bug introduced by
It seems like getModelForOptions() 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

44
        if (is_null($this->/** @scrutinizer ignore-call */ getModelForOptions())) {
Loading history...
45
            parent::save($request);
46
        }
47
    }
48
49
    /**
50
     * @param \Illuminate\Http\Request $request
51
     *
52
     * @return void
53
     */
54
    public function afterSave(\Illuminate\Http\Request $request)
55
    {
56
        if (is_null($this->getModelForOptions())) {
57
            return;
58
        }
59
60
        if ($this->isValueSkipped()) {
0 ignored issues
show
Bug introduced by
It seems like isValueSkipped() 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

60
        if ($this->/** @scrutinizer ignore-call */ isValueSkipped()) {
Loading history...
61
            return;
62
        }
63
64
        $attribute = $this->getModelAttributeKey();
0 ignored issues
show
Bug introduced by
It seems like getModelAttributeKey() 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

64
        /** @scrutinizer ignore-call */ 
65
        $attribute = $this->getModelAttributeKey();
Loading history...
65
66
        if (is_null($request->input($this->getPath()))) {
0 ignored issues
show
Bug introduced by
It seems like getPath() 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

66
        if (is_null($request->input($this->/** @scrutinizer ignore-call */ getPath()))) {
Loading history...
67
            $values = [];
68
        } else {
69
            $values = $this->getValueFromModel();
70
        }
71
72
        $relation = $this->getModel()->{$attribute}();
0 ignored issues
show
Bug introduced by
It seems like getModel() 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

72
        $relation = $this->/** @scrutinizer ignore-call */ getModel()->{$attribute}();
Loading history...
73
        if ($relation instanceof \Illuminate\Database\Eloquent\Relations\BelongsToMany) {
74
            $this->syncBelongsToManyRelation($relation, $values);
0 ignored issues
show
Bug introduced by
It seems like $values can also be of type string; however, parameter $values of SleepingOwl\Admin\Traits...BelongsToManyRelation() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

74
            $this->syncBelongsToManyRelation($relation, /** @scrutinizer ignore-type */ $values);
Loading history...
75
        } elseif ($relation instanceof \Illuminate\Database\Eloquent\Relations\HasMany) {
76
            $this->deleteOldItemsFromHasManyRelation($relation, $values);
0 ignored issues
show
Bug introduced by
It seems like $values can also be of type string; however, parameter $values of SleepingOwl\Admin\Traits...msFromHasManyRelation() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

76
            $this->deleteOldItemsFromHasManyRelation($relation, /** @scrutinizer ignore-type */ $values);
Loading history...
77
            $this->attachItemsToHasManyRelation($relation, $values);
0 ignored issues
show
Bug introduced by
It seems like $values can also be of type string; however, parameter $values of SleepingOwl\Admin\Traits...temsToHasManyRelation() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

77
            $this->attachItemsToHasManyRelation($relation, /** @scrutinizer ignore-type */ $values);
Loading history...
78
        }
79
    }
80
81
    /**
82
     * @param \Illuminate\Database\Eloquent\Relations\BelongsToMany $relation
83
     * @param array $values
84
     *
85
     * @return void
86
     */
87
    protected function syncBelongsToManyRelation(
88
        \Illuminate\Database\Eloquent\Relations\BelongsToMany $relation,
89
        array $values
90
    ) {
91
        if ($this instanceof Taggable) {
92
            foreach ($values as $i => $value) {
93
                if (! array_key_exists($value, $this->getOptions()) && $this->isTaggable()) {
0 ignored issues
show
Bug introduced by
The method getOptions() does not exist on SleepingOwl\Admin\Contracts\Form\Element\Taggable. Since it exists in all sub-types, consider adding an abstract or default implementation to SleepingOwl\Admin\Contracts\Form\Element\Taggable. ( Ignorable by Annotation )

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

93
                if (! array_key_exists($value, $this->/** @scrutinizer ignore-call */ getOptions()) && $this->isTaggable()) {
Loading history...
Bug introduced by
It seems like getOptions() 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

93
                if (! array_key_exists($value, $this->/** @scrutinizer ignore-call */ getOptions()) && $this->isTaggable()) {
Loading history...
94
                    $model = clone $this->getModelForOptions();
0 ignored issues
show
Bug introduced by
The method getModelForOptions() does not exist on SleepingOwl\Admin\Contracts\Form\Element\Taggable. Since it exists in all sub-types, consider adding an abstract or default implementation to SleepingOwl\Admin\Contracts\Form\Element\Taggable. ( Ignorable by Annotation )

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

94
                    $model = clone $this->/** @scrutinizer ignore-call */ getModelForOptions();
Loading history...
95
                    $model->{$this->getDisplay()} = $value;
0 ignored issues
show
Bug introduced by
It seems like getDisplay() 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

95
                    $model->{$this->/** @scrutinizer ignore-call */ getDisplay()} = $value;
Loading history...
Bug introduced by
The method getDisplay() does not exist on SleepingOwl\Admin\Contracts\Form\Element\Taggable. Since it exists in all sub-types, consider adding an abstract or default implementation to SleepingOwl\Admin\Contracts\Form\Element\Taggable. ( Ignorable by Annotation )

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

95
                    $model->{$this->/** @scrutinizer ignore-call */ getDisplay()} = $value;
Loading history...
96
                    $model->save();
97
98
                    $values[$i] = $model->getKey();
99
                }
100
            }
101
        }
102
103
        if (($this instanceof HasSyncCallback) && is_callable($callback = $this->getSyncCallback())) {
104
            $callbackModel = $this->getModel();
0 ignored issues
show
Bug introduced by
The method getModel() does not exist on SleepingOwl\Admin\Contra...Element\HasSyncCallback. Since it exists in all sub-types, consider adding an abstract or default implementation to SleepingOwl\Admin\Contra...Element\HasSyncCallback. ( Ignorable by Annotation )

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

104
            /** @scrutinizer ignore-call */ 
105
            $callbackModel = $this->getModel();
Loading history...
105
            $callback($values, $callbackModel);
106
107
            return;
108
        }
109
110
        $relation->sync($values);
111
    }
112
113
    /**
114
     * @param \Illuminate\Database\Eloquent\Relations\HasMany $relation
115
     * @param array $values
116
     */
117
    protected function deleteOldItemsFromHasManyRelation(
118
        \Illuminate\Database\Eloquent\Relations\HasMany $relation,
119
        array $values
120
    ) {
121
        $items = $relation->get();
122
123
        foreach ($items as $item) {
124
            if (! in_array($item->getKey(), $values)) {
125
                if (($this instanceof MustDeleteRelatedItem) && $this->isDeleteRelatedItem()) {
126
                    $item->delete();
127
                } else {
128
                    $item->{$this->getForeignKeyNameFromRelation($relation)} = null;
0 ignored issues
show
Bug introduced by
It seems like getForeignKeyNameFromRelation() 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

128
                    $item->{$this->/** @scrutinizer ignore-call */ getForeignKeyNameFromRelation($relation)} = null;
Loading history...
129
                    $item->save();
130
                }
131
            }
132
        }
133
    }
134
135
    /**
136
     * @param \Illuminate\Database\Eloquent\Relations\HasMany $relation
137
     * @param array $values
138
     */
139
    protected function attachItemsToHasManyRelation(
140
        \Illuminate\Database\Eloquent\Relations\HasMany $relation,
141
        array $values
142
    ) {
143
        foreach ($values as $i => $value) {
144
            /** @var Model $model */
145
            $model = clone $this->getModelForOptions();
146
            $item = $model->find($value);
147
148
            if (is_null($item)) {
149
                if (! ($this instanceof Taggable) && $this->isTaggable()) {
0 ignored issues
show
Bug introduced by
It seems like isTaggable() 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

149
                if (! ($this instanceof Taggable) && $this->/** @scrutinizer ignore-call */ isTaggable()) {
Loading history...
150
                    continue;
151
                }
152
153
                $model->{$this->getDisplay()} = $value;
154
                $item = $model;
155
            }
156
157
            $relation->save($item);
158
        }
159
    }
160
}
161