Completed
Push — master ( 5c4b87...207d41 )
by Yaro
06:22
created

EloquentModelRepository::applySortableOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Yaro\Jarboe\Table\Repositories;
4
5
use Illuminate\Http\Request;
6
use Yaro\Jarboe\Table\CRUD;
7
use Yaro\Jarboe\Table\Fields\AbstractField;
8
9
class EloquentModelRepository implements ModelRepositoryInterface
10
{
11
    /**
12
     * @var CRUD
13
     */
14
    private $crud;
15
    private $filter;
16
    private $perPage = 20;
17
    private $defaultOrder = [
18
        'column'    => false,
19
        'direction' => false,
20
    ];
21
22 46
    public function setCrud(CRUD $crud): ModelRepositoryInterface
23
    {
24 46
        $this->crud = $crud;
25
26 46
        return $this;
27
    }
28
29 1
    public function get()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
30
    {
31 1
        $model = $this->crud->getModel();
32 1
        $model = $model::query();
33
34 1
        $this->applyFilter($model);
35 1
        $this->applySearchFilters($model);
36
37 1
        if ($this->crud->isSortableByWeightActive()) {
38
            $this->applySortableOrder($model);
39
        } else {
40 1
            $this->applyOrder($model);
41
        }
42
43 1
        $this->applyPaginate($model);
44
45 1
        return $model;
46
    }
47
48 20
    public function find($id)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
49
    {
50 20
        $model = $this->crud->getModel();
51 20
        $model = $model::query();
52 20
        $this->applyFilter($model);
53
54 20
        $model = $model->find($id);
55 20
        if (!$model) {
56
            throw new \RuntimeException(sprintf('Not allowed or no record to edit [%s]', $id));
57
        }
58
59 20
        return $model;
60
    }
61
62 1
    public function delete($id): bool
63
    {
64 1
        $model = $this->find($id);
65
66 1
        return $model->delete();
67
    }
68
69 1
    public function store(array $data)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
70
    {
71 1
        $model = $this->crud->getModel();
72
73 1
        $model = $model::create($data);
74
75 1
        return $model;
76
    }
77
78 2
    public function update($id, array $data)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
79
    {
80 2
        $model = $this->find($id);
81
82 2
        $model->update($data);
83
84 2
        return $model;
85
    }
86
87 46
    public function filter(\Closure $callback)
88
    {
89 46
        $this->filter = $callback;
90 46
    }
91
92
    public function order(string $column, string $direction)
93
    {
94
        $this->defaultOrder = [
95
            'column'    => $column,
96
            'direction' => $direction,
97
        ];
98
    }
99
100 1
    public function perPage(int $perPage = null)
101
    {
102 1
        if (is_null($perPage)) {
103 1
            return $this->perPage;
104
        }
105
106
        $this->perPage = $perPage;
107
    }
108
109 21
    private function applyFilter($model)
110
    {
111 21
        $callback = $this->filter;
112 21
        if ($callback) {
113 21
            $callback($model);
114
        }
115 21
    }
116
117 1
    private function applySearchFilters($model)
118
    {
119 1
        foreach ($this->crud->getAllFieldObjects() as $field) {
120 1
            if ($field->filter()) {
121
                $field->filter()->apply($model, $field->name());
122
            }
123
        }
124 1
    }
125
126 1
    private function applyOrder($model)
127
    {
128 1
        $shouldApplyDefaultOrder = true;
129 1
        foreach ($this->crud->getAllFieldObjects() as $field) {
130 1
            if ($field->isOrderable()) {
131
                $direction = $this->crud->getOrderFilterParam($field->name());
132
                if (!is_null($direction)) {
133
                    $callback = $field->getOverridedOrderCallback();
134
                    if ($callback) {
135
                        $callback($model, $field, $direction, $shouldApplyDefaultOrder);
136
                    } else {
137
                        $model->orderBy($field->name(), $direction);
138
                        $shouldApplyDefaultOrder = false;
139
                    }
140
                }
141
            }
142
        }
143
144 1
        if ($shouldApplyDefaultOrder && array_filter($this->defaultOrder)) {
145
            $model->orderBy($this->defaultOrder['column'], $this->defaultOrder['direction']);
146
        }
147 1
    }
148
149 1
    private function applyPaginate(&$model)
150
    {
151 1
        $model = $model->paginate($this->perPage());
152 1
    }
153
154
    private function applySortableOrder($model)
155
    {
156
        $model->orderBy($this->crud->getSortableWeightFieldName(), 'asc');
157
    }
158
159
    public function reorder($id, $idPrev, $idNext)
160
    {
161
        $sort = $this->crud->getSortableWeightFieldName();
162
        $model = $this->crud->getModel();
163
        $query = $model::query();
164
        $key = (new $model)->getKeyName();
165
166
        if ($idPrev) {
167
            $weight = $this->find($idPrev)->$sort + 1;
168
            $this->find($id)->update([
169
                $sort => $weight,
170
            ]);
171
172
            $query->where($key, '!=', $id)->where($sort, '>=', $weight)->increment($sort);
173
        } elseif ($idNext) {
174
            $weight = $this->find($idNext)->$sort - 1;
175
            $this->find($id)->update([
176
                $sort => $weight,
177
            ]);
178
179
            $query->where($key, '!=', $id)->where($sort, '<=', $weight)->decrement($sort);
180
        }
181
    }
182
183 2
    public function restore($id)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
184
    {
185 2
        $model = $this->find($id);
186
187 2
        return $model->restore();
188
    }
189
190 1
    public function forceDelete($id)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
191
    {
192 1
        $model = $this->find($id);
193
194 1
        return $model->forceDelete();
195
    }
196
}
197