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) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
if ($model->{$this->column} != 1) { |
30
|
|
|
$model::where($this->column, $model->{$this->column} - 1)->increment($this->column); |
31
|
|
|
$model->decrement($this->column); |
|
|
|
|
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) |
|
|
|
|
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); |
|
|
|
|
49
|
|
|
|
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->toFirst($model); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Model $model |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function delete(Model $model) |
62
|
|
|
{ |
63
|
|
|
$toDecrement = $model::where($this->column, '>', $model->{$this->column})->get(); |
64
|
|
|
if (count($toDecrement)) { |
65
|
|
|
$toDecrement->each->decrement($this->column); |
66
|
|
|
true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Model $model |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function toFirst(Model $model) |
78
|
|
|
{ |
79
|
|
View Code Duplication |
if ($model->{$this->column} != 1) { |
|
|
|
|
80
|
|
|
$models = $model::where($this->column, '<', $model->{$this->column})->get(); |
81
|
|
|
$models->each->increment($this->column); |
82
|
|
|
$model->{$this->column} = 1; |
83
|
|
|
$model->save(); |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param Model $model |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function toLast(Model $model) |
97
|
|
|
{ |
98
|
|
|
$last = $this->count($model); |
99
|
|
|
|
100
|
|
View Code Duplication |
if ($last != $model->{$this->column}) { |
|
|
|
|
101
|
|
|
$models = $model::where($this->column, '>', $model->{$this->column})->get(); |
102
|
|
|
$models->each->decrement($this->column); |
103
|
|
|
$model->{$this->column} = $last; |
104
|
|
|
$model->save(); |
105
|
|
|
|
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param Model $model |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function toMiddle(Model $model) |
118
|
|
|
{ |
119
|
|
|
$middle = number_format($this->count($model) / 2); |
120
|
|
|
$between = [$model->{$this->column}, $middle]; |
121
|
|
|
|
122
|
|
|
if ($model->{$this->column} != $middle) { |
123
|
|
View Code Duplication |
if ($model->{$this->column} > $middle) { |
|
|
|
|
124
|
|
|
$result = $model::whereBetween($this->column, array_reverse($between))->get(); |
125
|
|
|
$result->each->increment($this->column); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
View Code Duplication |
if ($model->{$this->column} < $middle) { |
|
|
|
|
129
|
|
|
$result = $model::whereBetween($this->column, $between)->get(); |
130
|
|
|
$result->each->decrement($this->column); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$model->{$this->column} = $middle; |
134
|
|
|
$model->save(); |
135
|
|
|
|
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param Model $model1 |
144
|
|
|
* @param Model $model2 |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public function switchModels(Model $model1, Model $model2) |
149
|
|
|
{ |
150
|
|
|
$temp = $model1->{$this->column}; |
151
|
|
|
$model1->{$this->column} = $model2->{$this->column}; |
152
|
|
|
$model2->{$this->column} = $temp; |
153
|
|
|
|
154
|
|
|
$model1->save(); |
155
|
|
|
$model2->save(); |
156
|
|
|
|
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param Model $model |
162
|
|
|
* @param $index1 |
163
|
|
|
* @param $index2 |
164
|
|
|
* |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function switchIndexes(Model $model, $index1, $index2) |
168
|
|
|
{ |
169
|
|
|
$model1 = $model::where($this->column, $index1)->first(); |
170
|
|
|
$model2 = $model::where($this->column, $index2)->first(); |
171
|
|
|
|
172
|
|
|
return $this->switchModels($model1, $model2); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param Model $model |
177
|
|
|
* |
178
|
|
|
* @return mixed |
179
|
|
|
*/ |
180
|
|
|
public function count(Model $model) |
181
|
|
|
{ |
182
|
|
|
return $model::count(); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
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.