AbstractListSorter::extractKeys()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ListSorter;
6
7
use Illuminate\Http\Request;
8
9
abstract class AbstractListSorter
10
{
11
    protected $request;
12
    protected $sortableItems;
13
14 15
    public function __construct(Request $request, array $sortableItems)
15
    {
16 15
        $this->setRequest($request);
17 15
        $this->setSortableItems($sortableItems);
18 12
    }
19
20
    /**
21
     * @return string
22
     */
23
    abstract public function getSortBy();
24
25
    /**
26
     * @return string
27
     */
28
    abstract public function getSortDir();
29
30
    /**
31
     * @return Request
32
     */
33 6
    public function getRequest()
34
    {
35 6
        return $this->request;
36
    }
37
38
    /**
39
     * @param Request $request
40
     */
41 15
    public function setRequest(Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
42
    {
43 15
        $this->request = $request;
44 15
    }
45
46
    /**
47
     * @return array
48
     */
49 8
    public function getSortableItems()
50
    {
51 8
        return $this->sortableItems;
52
    }
53
54
    /**
55
     * @param array $sortableItems
56
     *
57
     * @throws \Exception
58
     */
59 15
    public function setSortableItems(array $sortableItems)
60
    {
61
        // validate aliases
62 15
        $this->validateSortableItems($sortableItems);
63
64 12
        foreach ($sortableItems as $key => $sortableItem) {
65 12
            if (!$sortableItem instanceof SortableItem) {
66 12
                $sortableItems[$key] = new SortableItem($sortableItem);
67
            }
68
        }
69
70 12
        $this->sortableItems = $sortableItems;
71 12
    }
72
73
    /**
74
     * @param array $sortableItems
75
     *
76
     * @throws \Exception
77
     *
78
     * @return bool
79
     */
80 15
    private function validateSortableItems(array $sortableItems)
81
    {
82 15
        if (empty($sortableItems)) {
83 1
            throw new \Exception('Sortable items can not be empty');
84
        }
85
86 14
        if ($this->areKeysUnique($this->extractKeys($sortableItems)) !== true) {
87 2
            throw new \Exception('Sortable item alias must be unique');
88
        }
89
90 12
        return true;
91
    }
92
93
    /**
94
     * @param array $sortableItems
95
     *
96
     * @return array
97
     */
98 14
    private function extractKeys(array $sortableItems)
99
    {
100 14
        $keys = [];
101 14
        foreach ($sortableItems as $sortableItem) {
102 14
            if (!$sortableItem instanceof SortableItem) {
103 2
                $keys[] = $sortableItem;
104 2
                continue;
105
            }
106
107 12
            $keys[] = $sortableItem->getKey();
108
        }
109
110 14
        return $keys;
111
    }
112
113
    /**
114
     * @param array $aliases
115
     *
116
     * @return bool
117
     */
118 14
    private function areKeysUnique(array $aliases)
119
    {
120 14
        if (count($aliases) === count(array_unique($aliases))) {
121 12
            return true;
122
        }
123
124 2
        return false;
125
    }
126
}
127