Completed
Push — master ( bc3f80...57eaea )
by Lars
02:13
created

OrderRepository::switchModels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
namespace LarsJanssen\IncrementDecrement\Repository;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class OrderRepository implements OrderRepositoryInterface
8
{
9
    /**
10
     * @var mixed
11
     */
12
    public $column;
13
14
    /**
15
     * OrderRepository constructor.
16
     */
17
    public function __construct()
18
    {
19
        $this->column = config('increment-decrement.order_column_name');
20
    }
21
22
    /**
23
     * @param Model $model
24
     *
25
     * @return bool
26
     */
27 View Code Duplication
    public function increment(Model $model)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        if ($model->{$this->column} != 1) {
30
            $model::where($this->column, $model->{$this->column} - 1)->increment($this->column);
31
            $model->decrement($this->column);
0 ignored issues
show
Bug introduced by
The method decrement() cannot be called from this context as it is declared protected in class Illuminate\Database\Eloquent\Model.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
32
33
            return true;
34
        }
35
36
        return $this->toLast($model);
37
    }
38
39
    /**
40
     * @param Model $model
41
     *
42
     * @return bool
43
     */
44 View Code Duplication
    public function decrement(Model $model)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        if ($model->{$this->column} != $this->count($model)) {
47
            $model::where($this->column, $model->{$this->column} + 1)->decrement($this->column);
48
            $model->increment($this->column);
0 ignored issues
show
Bug introduced by
The method increment() cannot be called from this context as it is declared protected in class Illuminate\Database\Eloquent\Model.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
49
50
            return true;
51
        }
52
53
        return $this->toFirst($model);
54
    }
55
56
    /**
57
     * @param Model $model
58
     * @return bool
59
     */
60
    public function delete(Model $model)
61
    {
62
        $toDecrement = $model::where($this->column, '>',  $model->{$this->column})->get();
63
        if(count($toDecrement)) {
64
            $toDecrement->each->decrement($this->column);
65
            true;
66
        }
67
68
        return false;
69
    }
70
71
    /**
72
     * @param Model $model
73
     *
74
     * @return bool
75
     */
76
    public function toFirst(Model $model)
77
    {
78 View Code Duplication
        if ($model->{$this->column} != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
            $models = $model::where($this->column, '<', $model->{$this->column})->get();
80
            $models->each->increment($this->column);
81
            $model->{$this->column} = 1;
82
            $model->save();
83
84
            return true;
85
        }
86
87
        return false;
88
    }
89
90
    /**
91
     * @param Model $model
92
     *
93
     * @return bool
94
     */
95
    public function toLast(Model $model)
96
    {
97
        $last = $this->count($model);
98
99 View Code Duplication
        if ($last != $model->{$this->column}) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
            $models = $model::where($this->column, '>', $model->{$this->column})->get();
101
            $models->each->decrement($this->column);
102
            $model->{$this->column} = $last;
103
            $model->save();
104
105
            return true;
106
        }
107
108
        return false;
109
    }
110
111
    /**
112
     * @param Model $model
113
     *
114
     * @return bool
115
     */
116
    public function toMiddle(Model $model)
117
    {
118
        $middle = number_format($this->count($model) / 2);
119
        $between = [$model->{$this->column}, $middle];
120
121
        if ($model->{$this->column} != $middle) {
122 View Code Duplication
            if ($model->{$this->column} > $middle) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
                $result = $model::whereBetween($this->column, array_reverse($between))->get();
124
                $result->each->increment($this->column);
125
            }
126
127 View Code Duplication
            if ($model->{$this->column} < $middle) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
                $result = $model::whereBetween($this->column, $between)->get();
129
                $result->each->decrement($this->column);
130
            }
131
132
            $model->{$this->column} = $middle;
133
            $model->save();
134
135
            return true;
136
        }
137
138
        return false;
139
    }
140
141
    /**
142
     * @param Model $model1
143
     * @param Model $model2
144
     *
145
     * @return bool
146
     */
147
    public function switchModels(Model $model1, Model $model2)
148
    {
149
        $temp = $model1->{$this->column};
150
        $model1->{$this->column} = $model2->{$this->column};
151
        $model2->{$this->column} = $temp;
152
153
        $model1->save();
154
        $model2->save();
155
156
        return true;
157
    }
158
159
    /**
160
     * @param Model $model
161
     * @param $index1
162
     * @param $index2
163
     *
164
     * @return bool
165
     */
166
    public function switchIndexes(Model $model, $index1, $index2)
167
    {
168
        $model1 = $model::where($this->column, $index1)->first();
169
        $model2 = $model::where($this->column, $index2)->first();
170
171
        return $this->switchModels($model1, $model2);
172
    }
173
174
    /**
175
     * @param Model $model
176
     *
177
     * @return mixed
178
     */
179
    public function count(Model $model)
180
    {
181
        return $model::count();
182
    }
183
}
184