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.
Completed
Push — master ( 2998b0...c1d9d9 )
by Dave
28:06 queued 22:43
created

OrderableModel::getParentFieldName()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Builder;
7
8
/**
9
 * Trait OrderableModel.
10
 * @method static $this orderModel()
11
 * @method Builder findByPosition($position)
12
 */
13
trait OrderableModel
14
{
15
    /**
16
     * Boot trait.
17
     */
18
    protected static function bootOrderableModel()
19
    {
20
        static::creating(function (Model $row) {
21
            $row->updateOrderFieldOnCreate();
22
        });
23
24
        static::deleted(function (Model $row) {
25
            $row->updateOrderFieldOnDelete();
26
        });
27
28
        if (in_array("Illuminate\Database\Eloquent\SoftDeletes", trait_uses_recursive(new static()))) {
0 ignored issues
show
Bug introduced by
new static() of type SleepingOwl\Admin\Traits\OrderableModel is incompatible with the type string expected by parameter $trait of trait_uses_recursive(). ( Ignorable by Annotation )

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

28
        if (in_array("Illuminate\Database\Eloquent\SoftDeletes", trait_uses_recursive(/** @scrutinizer ignore-type */ new static()))) {
Loading history...
29
            static::restoring(function (Model $row) {
30
                $row->updateOrderFieldOnRestore();
31
            });
32
        }
33
    }
34
35
    /**
36
     * Get order value.
37
     * @return int
38
     */
39
    public function getOrderValue()
40
    {
41
        return $this->{$this->getOrderField()};
42
    }
43
44
    /**
45
     * Move model up.
46
     */
47
    public function moveUp()
48
    {
49
        $this->move(1);
50
    }
51
52
    /**
53
     * Move model down.
54
     */
55
    public function moveDown()
56
    {
57
        $this->move(-1);
58
    }
59
60
    /**
61
     * Move model in the $destination.
62
     *
63
     * @param $destination -1 (move down) or 1 (move up)
0 ignored issues
show
Documentation Bug introduced by
The doc comment -1 at position 0 could not be parsed: Unknown type name '-1' at position 0 in -1.
Loading history...
64
     */
65
    protected function move($destination)
66
    {
67
        if ($previousRow = static::orderModel()->findByPosition($this->getOrderValue() - $destination)->first()) {
68
            $previousRow->{$this->getOrderField()} += $destination;
69
            $previousRow->save();
70
        }
71
72
        $this->{$this->getOrderField()} -= $destination;
73
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() 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

73
        $this->/** @scrutinizer ignore-call */ 
74
               save();
Loading history...
74
    }
75
76
    /**
77
     * Update order field on create.
78
     */
79
    protected function updateOrderFieldOnCreate()
80
    {
81
        $this->{$this->getOrderField()} = static::orderModel()->count();
0 ignored issues
show
Bug introduced by
It seems like count() 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

81
        $this->{$this->getOrderField()} = static::orderModel()->/** @scrutinizer ignore-call */ count();
Loading history...
82
    }
83
84
    /**
85
     * Update order field on delete.
86
     */
87
    protected function updateOrderFieldOnDelete()
88
    {
89
        static::orderModel()
90
            ->where($this->getOrderField(), '>', $this->getOrderValue())
0 ignored issues
show
Bug introduced by
It seems like where() 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

90
            ->/** @scrutinizer ignore-call */ where($this->getOrderField(), '>', $this->getOrderValue())
Loading history...
91
            ->decrement($this->getOrderField());
92
    }
93
94
    /**
95
     * Update order field on restore.
96
     */
97
    protected function updateOrderFieldOnRestore()
98
    {
99
        static::orderModel()
100
            ->where($this->getOrderField(), '>', $this->getOrderValue())
101
            ->increment($this->getOrderField());
102
    }
103
104
    /**
105
     * Order scope.
106
     *
107
     * @param $query
108
     *
109
     * @return mixed
110
     */
111
    public function scopeOrderModel($query)
112
    {
113
        $parentFields = $this->getParentFieldName();
114
        if ($parentFields) {
115
            $parentFields = (array) $parentFields;
116
            foreach ($parentFields as $parentFieldName) {
117
                $query->where($parentFieldName, $this->{$parentFieldName});
118
            }
119
        }
120
121
        return $query->orderBy($this->getOrderField(), 'ASC');
122
    }
123
124
    /**
125
     * @param Builder $query
126
     * @param int $position
127
     *
128
     * @return mixed
129
     */
130
    public function scopeFindByPosition(Builder $query, $position)
131
    {
132
        $query->where($this->getOrderField(), $position);
133
    }
134
135
    /**
136
     * Get order field name.
137
     *
138
     * @return string
139
     */
140
    public function getOrderField()
141
    {
142
        return $this->orderField ?: 'order';
143
    }
144
145
    /**
146
     * Get parent order field name.
147
     *
148
     * @return string
149
     */
150
    public function getParentFieldName()
151
    {
152
        return $this->parentFieldName ?: null;
153
    }
154
}
155