GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b864c9...ed9166 )
by butschster
12:41
created

ColumnFilters::modifyQuery()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 7
nop 1
dl 0
loc 30
ccs 0
cts 24
cp 0
crap 56
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Display\Extension;
4
5
use Request;
6
use KodiComponents\Support\HtmlAttributes;
7
use SleepingOwl\Admin\Contracts\Initializable;
8
use SleepingOwl\Admin\Contracts\Display\Placable;
9
use SleepingOwl\Admin\Contracts\Display\NamedColumnInterface;
10
use SleepingOwl\Admin\Contracts\Display\Extension\ColumnFilterInterface;
11
12
class ColumnFilters extends Extension implements Initializable, Placable
13
{
14
    use HtmlAttributes;
15
16
    /**
17
     * @var ColumnFilterInterface[]
18
     */
19
    protected $columnFilters = [];
20
21
    /**
22
     * @var string|\Illuminate\View\View
23
     */
24
    protected $view = 'display.extensions.columns_filters_table';
25
26
    /**
27
     * @var string
28
     */
29
    protected $placement = 'table.footer';
30
31
    /**
32
     * @param array|ColumnFilterInterface $columnFilters
33
     *
34
     * @return $this
35
     */
36
    public function set($columnFilters)
37
    {
38
        if (! is_array($columnFilters)) {
39
            $columnFilters = func_get_args();
40
        }
41
42
        $this->columnFilters = [];
43
44
        foreach ($columnFilters as $filter) {
45
            $this->push($filter);
46
        }
47
48
        return $this;
49
    }
50
51
    /**
52
     * @return ColumnFilterInterface[]
53
     */
54
    public function all()
55
    {
56
        return $this->columnFilters;
57
    }
58
59
    /**
60
     * @param ColumnFilterInterface $filter
61
     *
62
     * @return $this
63
     */
64
    public function push(ColumnFilterInterface $filter = null)
65
    {
66
        $this->columnFilters[] = $filter;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @return string|\Illuminate\View\View
73
     */
74
    public function getView()
75
    {
76
        return $this->view;
77
    }
78
79
    /**
80
     * @param string|\Illuminate\View\View $view
81
     *
82
     * @return $this
83
     */
84
    public function setView($view)
85
    {
86
        $this->view = $view;
87
88
        return $this;
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getPlacement()
95
    {
96
        return $this->placement;
97
    }
98
99
    /**
100
     * @param string $placement
101
     *
102
     * @return $this
103
     */
104
    public function setPlacement($placement)
105
    {
106
        $this->placement = $placement;
107
108
        return $this;
109
    }
110
111
    /**
112
     * @deprecated use getPlacement()
113
     * @return string
114
     */
115
    public function getPosition()
116
    {
117
        return $this->getPlacement();
118
    }
119
120
    /**
121
     * @deprecated use setPlacement(string $placement)
122
     * @param string $position
123
     *
124
     * @return $this
125
     */
126
    public function setPosition($position)
127
    {
128
        return $this->setPlacement($position);
129
    }
130
131
    /**
132
     * Get the instance as an array.
133
     *
134
     * @return array
135
     */
136
    public function toArray()
137
    {
138
        return [
139
            'filters' => $this->columnFilters,
140
            'attributes' => $this->htmlAttributesToString(),
141
            'tag' => $this->getPlacement() == 'table.header' ? 'thead' : 'tfoot',
142
            'displayClass' => get_class($this->getDisplay()),
143
        ];
144
    }
145
146
    /**
147
     * Initialize class.
148
     */
149
    public function initialize()
150
    {
151
        if (empty($this->columnFilters)) {
152
            return;
153
        }
154
155
        $this->validNumberOfFilters();
156
157
        foreach ($this->all() as $filter) {
158
            if ($filter instanceof Initializable) {
159
                $filter->initialize();
160
            }
161
        }
162
163
        $this->validNumberOfFilters();
164
        $this->prepareView();
165
    }
166
167
    /**
168
     * @param \Illuminate\Database\Eloquent\Builder $query
169
     */
170
    public function modifyQuery(\Illuminate\Database\Eloquent\Builder $query)
171
    {
172
        $search = Request::input('columns', []);
173
174
        $display = $this->getDisplay();
175
176
        if (! $display->getExtensions()->has('columns')) {
177
            return;
178
        }
179
180
        $columns = $display->getColumns()->all();
181
182
        if (! is_int(key($search))) {
183
            $search = [$search];
184
        }
185
186
        foreach ($search as $index => $columnData) {
187
            $column = $columns->get($index);
188
            $columnFilter = array_get($this->all(), $index);
189
190
            if ($column && $column instanceof NamedColumnInterface && $columnFilter) {
191
                $columnFilter->apply(
192
                    $column,
193
                    $query,
194
                    array_get($columnData, 'search.value'),
195
                    array_get($columnData, 'search')
196
                );
197
            }
198
        }
199
    }
200
201
    protected function validNumberOfFilters()
202
    {
203
        $display = $this->getDisplay();
204
205
        if ($display->getExtensions()->has('columns')) {
206
            $totalColumns = count($display->getColumns()->all());
207
            $totalFilters = count($this->all());
208
            $missedFilters = $totalColumns - $totalFilters;
209
210
            while ($missedFilters > 0) {
211
                $this->push(null);
212
                $missedFilters--;
213
            }
214
        }
215
    }
216
217
    protected function prepareView()
218
    {
219
        if (! in_array($this->getPlacement(), ['table.footer', 'table.header']) && $this->view == 'display.extensions.columns_filters_table') {
220
            $this->view = 'display.extensions.columns_filters';
221
            $this->setHtmlAttribute('class', 'table table-default');
222
        }
223
224
        $this->setHtmlAttribute('class', 'display-filters');
225
    }
226
}
227