Passed
Push — feature/optimize ( 0e0305...fdd242 )
by Fu
03:04
created

ParserOrderByTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A multiOrderBy() 0 19 2
A parserOrderBy() 0 14 5
A orderBy() 0 3 1
1
<?php
2
3
4
namespace Modules\Core\Traits\Criteria;
5
6
7
use Illuminate\Support\Str;
8
9
/**
10
 * Trait ParserOrderByTrait
11
 *
12
 * @property \Illuminate\Http\Request $request
13
 * @property \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder $model
14
 * @property $search
15
 * @property $searchData
16
 * @property $searchFields
17
 * @property $isFirstField = true
18
 * @property $modelForceAndWhere
19
 * @property $fieldsSearchable
20
 * @property $fields
21
 * @property $filter
22
 * @property $orderBy
23
 * @property $sortedBy
24
 * @property $with
25
 * @property $searchJoin
26
 * @property $acceptedConditions
27
 * @property $originalFields
28
 * @package Modules\Core\Traits\Criteria
29
 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $isFirstField at position 0 could not be parsed: Unknown type name '$isFirstField' at position 0 in $isFirstField.
Loading history...
30
trait ParserOrderByTrait
31
{
32
    protected function parserOrderBy()
33
    {
34
        $this->orderBy = $this->request->get(config('repository.criteria.params.orderBy', 'orderBy'), null);
0 ignored issues
show
Bug Best Practice introduced by
The property orderBy does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
35
        $this->sortedBy = $this->request->get(config('repository.criteria.params.sortedBy', 'sortedBy'), 'asc');
0 ignored issues
show
Bug Best Practice introduced by
The property sortedBy does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
        $this->sortedBy = !empty($this->sortedBy) ? $this->sortedBy : 'asc';
37
38
        if (isset($this->orderBy) && !empty($this->orderBy)) {
39
40
            $split = explode('|', $this->orderBy);
41
42
            if (count($split) > 1) {
43
                $this->multiOrderBy($split);
44
            } else {
45
                $this->orderBy();
46
            }
47
        }
48
    }
49
50
    protected function orderBy()
51
    {
52
        $this->model = $this->model->orderBy($this->orderBy, $this->sortedBy);
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
    }
54
55
    protected function multiOrderBy($split)
56
    {
57
        $table = $this->model->getModel()->getTable();
58
        $sortTable = $split[0];
59
        $sortColumn = $split[1];
60
61
        $split = explode(':', $sortTable);
62
63
        if (count($split) > 1) {
64
            $sortTable = $split[0];
65
            $keyName = $table.'.'.$split[1];
66
        } else {
67
            $prefix = Str::singular($sortTable);
68
            $keyName = $table.'.'.$prefix.'_id';
69
        }
70
71
        $this->model = $this->model->leftJoin($sortTable, $keyName, '=', $sortTable.'.id')
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
72
                                   ->orderBy($sortColumn, $this->sortedBy)
73
                                   ->addSelect($table.'.*');
74
    }
75
}