Sorter::direction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Terranet\Administrator\Services;
4
5
class Sorter
6
{
7
    /** @var array */
8
    protected $sortable;
9
10
    /** @var null|string */
11
    protected $element;
12
13
    /** @var null|string */
14
    protected $direction;
15
16
    /**
17
     * Sorter constructor.
18
     *
19
     * @param array $sortable
20
     * @param string $sortDir
21
     */
22
    public function __construct(array $sortable = [], string $sortDir = 'desc')
23
    {
24
        $this->sortable = $sortable;
25
26
        $this->element = $this->input('sort_by', $this->first());
27
28
        $this->direction = $this->input('sort_dir', $sortDir);
29
    }
30
31
    /**
32
     * Build sortable url.
33
     *
34
     * @param string $element
35
     *
36
     * @return string
37
     */
38
    public function makeUrl(string $element)
39
    {
40
        return \admin\helpers\qsRoute(null, [
41
            'sort_by' => $element,
42
            'sort_dir' => $this->proposeDirection($element),
43
        ]);
44
    }
45
46
    /**
47
     * Get current sorting direction.
48
     *
49
     * @return null|string
50
     */
51
    public function direction(): ?string
52
    {
53
        return $this->direction;
54
    }
55
56
    /**
57
     * Get current sorting element.
58
     *
59
     * @return null|string
60
     */
61
    public function element(): ?string
62
    {
63
        return $this->element ?: $this->first();
64
    }
65
66
    /**
67
     * Check if a column is sortable.
68
     *
69
     * @param string $column
70
     *
71
     * @return bool
72
     */
73
    public function canSortBy(string $column): bool
74
    {
75
        return array_key_exists($column, $this->sortable) || \in_array($column, $this->sortable, true);
76
    }
77
78
    /**
79
     * Retrieve first sortable element.
80
     *
81
     * @return mixed
82
     */
83
    protected function first()
84
    {
85
        foreach ($this->sortable as $key => $value) {
86
            if (is_numeric($key)) {
87
                return $value;
88
            }
89
90
            return $key;
91
        }
92
93
        return null;
94
    }
95
96
    /**
97
     * Propose new sort direction for element.
98
     *
99
     * @param $forElement
100
     *
101
     * @return string
102
     */
103
    protected function proposeDirection($forElement)
104
    {
105
        $sortDir = $this->direction();
106
107
        return $forElement === $this->element() ? $this->reverseDirection($sortDir) : $sortDir;
0 ignored issues
show
Bug introduced by
It seems like $sortDir can also be of type null; however, parameter $direction of Terranet\Administrator\S...ter::reverseDirection() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

107
        return $forElement === $this->element() ? $this->reverseDirection(/** @scrutinizer ignore-type */ $sortDir) : $sortDir;
Loading history...
108
    }
109
110
    /**
111
     * Reverse sorting direction.
112
     *
113
     * @param string $direction
114
     *
115
     * @return string
116
     */
117
    protected function reverseDirection(string $direction)
118
    {
119
        return 'asc' === strtolower($direction) ? 'desc' : 'asc';
120
    }
121
122
    /**
123
     * @param $key
124
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
125
     *
126
     * @return mixed
127
     */
128
    protected function input($key, $default = null)
129
    {
130
        return app('request')->input($key, $default);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

130
        return /** @scrutinizer ignore-call */ app('request')->input($key, $default);
Loading history...
131
    }
132
}
133