Completed
Push — master ( 9e3b82...1525fb )
by
unknown
10:24 queued 06:58
created

OrderByFieldCriteria   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 2
A apply() 0 4 1
1
<?php
2
3
namespace App\Ship\Criterias\Eloquent;
4
5
use App\Ship\Parents\Criterias\Criteria;
6
use Illuminate\Support\Str;
7
use Prettus\Repository\Contracts\RepositoryInterface as PrettusRepositoryInterface;
8
9
/**
10
 * Class OrderByFieldCriteria
11
 *
12
 * @package App\Ship\Criterias\Eloquent
13
 * @author  Johannes Schobel <[email protected]>
14
 */
15
class OrderByFieldCriteria extends Criteria
16
{
17
18
    private $field;
19
    private $sortOrder;
20
21
    /**
22
     * OrderByFieldCriteria constructor.
23
     *
24
     * @param string $field The field to be sorted
25
     * @param string $sortOrder the sort direction (asc or desc)
26
     */
27
    public function __construct($field, $sortOrder)
28
    {
29
        $this->field = $field;
30
31
        $sortOrder = Str::lower($sortOrder);
32
        $availableDirections = [
33
            'asc',
34
            'desc',
35
        ];
36
37
        // check if the value is available, otherwise set "default" sort order to ascending!
38
        if (! array_search($sortOrder, $availableDirections)) {
39
            $sortOrder = 'asc';
40
        }
41
42
        $this->sortOrder = $sortOrder;
43
    }
44
45
    /**
46
     * @param                                                   $model
47
     * @param \Prettus\Repository\Contracts\RepositoryInterface $repository
48
     *
49
     * @return mixed
50
     */
51
    public function apply($model, PrettusRepositoryInterface $repository)
52
    {
53
        return $model->orderBy($this->field, $this->sortOrder);
54
    }
55
56
}
57