Completed
Push — master ( f2025c...16072a )
by Lars
06:18
created

OrderRepository   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 160
Duplicated Lines 28.75 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 46
loc 160
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A increment() 11 11 2
A decrement() 11 11 2
A toFirst() 8 13 2
A toLast() 8 15 2
B toMiddle() 8 24 4
A switchModels() 0 11 1
A switchIndexes() 0 7 1
A count() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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