WithSorts   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 64
ccs 27
cts 27
cp 1
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSortValue() 0 7 2
A formatSorts() 0 13 3
A enableSorts() 0 19 3
A isRequestedSort() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\QueryBuilder\Concerns;
6
7
use Illuminate\Support\Collection;
8
use Zing\QueryBuilder\Sort;
9
10
trait WithSorts
11
{
12
    /**
13
     * @param array<(string|\Zing\QueryBuilder\Sort)> $sorts
14
     *
15
     * @return $this
16
     */
17 6
    public function enableSorts(array $sorts)
18
    {
19 6
        $this->formatSorts($sorts)
20 6
            ->each(
21 6
                function (Sort $sort): void {
22 6
                    $thisIsRequestedSort = $this->isRequestedSort($sort);
23 6
                    if ($thisIsRequestedSort) {
24 6
                        $sort->sort($this->getBuilder(), $this->getSortValue($sort));
0 ignored issues
show
Bug introduced by
It seems like getBuilder() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
                        $sort->sort($this->/** @scrutinizer ignore-call */ getBuilder(), $this->getSortValue($sort));
Loading history...
25
26 6
                        return;
27
                    }
28
29 1
                    if ($sort->hasDefaultDirection()) {
30 1
                        $sort->sort($this->getBuilder(), $sort->getDefaultDirection());
31
                    }
32
                }
33
            );
34
35 6
        return $this;
36
    }
37
38 6
    protected function isRequestedSort(Sort $sort): bool
39
    {
40 6
        if ($this->request->input('asc') === $sort->getProperty()) {
41 3
            return true;
42
        }
43
44 4
        return $this->request->input('desc') === $sort->getProperty();
45
    }
46
47 6
    protected function getSortValue(Sort $sort): string
48
    {
49 6
        if ($this->request->input('desc') === $sort->getProperty()) {
50 4
            return 'desc';
51
        }
52
53 3
        return 'asc';
54
    }
55
56
    /**
57
     * @phpstan-param array<(string|\Zing\QueryBuilder\Sort)> $sorts
58
     *
59
     * @param mixed $sorts
60
     */
61 6
    protected function formatSorts($sorts): Collection
62
    {
63 6
        return collect($sorts)->map(
64 6
            function ($sort, $key): Sort {
65 6
                if ($sort instanceof Sort) {
66 3
                    return $sort;
67
                }
68
69 4
                if (\is_string($key)) {
70 1
                    return Sort::field($key, $sort);
71
                }
72
73 3
                return Sort::field($sort);
74
            }
75
        );
76
    }
77
}
78