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
Pull Request — master (#688)
by
unknown
18:52
created

DisplayDatatablesAsync::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace SleepingOwl\Admin\Display;
4
5
use Request;
6
use Illuminate\Routing\Router;
7
use Illuminate\Support\Collection;
8
use Illuminate\Database\Eloquent\Builder;
9
use SleepingOwl\Admin\Display\Column\Link;
10
use SleepingOwl\Admin\Display\Column\Text;
11
use SleepingOwl\Admin\Display\Column\Email;
12
use SleepingOwl\Admin\Display\Column\Control;
13
use SleepingOwl\Admin\Contracts\WithRoutesInterface;
14
use SleepingOwl\Admin\Contracts\Display\ColumnInterface;
15
use SleepingOwl\Admin\Contracts\Display\ColumnMetaInterface;
16
17
class DisplayDatatablesAsync extends DisplayDatatables implements WithRoutesInterface
18
{
19
    /**
20
     * Register display routes.
21
     *
22
     * @param Router $router
23
     *
24
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
25
     */
26 285 View Code Duplication
    public static function registerRoutes(Router $router)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28 285
        $routeName = 'admin.display.async';
29 285
        if (! $router->has($routeName)) {
30 285
            $router->get('{adminModel}/async/{adminDisplayName?}', [
31 285
                'as'   => $routeName,
32 285
                'uses' => 'SleepingOwl\Admin\Http\Controllers\DisplayController@async',
33 285
            ]);
34 285
        }
35
36 285
        $routeName = 'admin.display.async.inlineEdit';
37 285
        if (! $router->has($routeName)) {
38 285
            $router->post('{adminModel}/async/{adminDisplayName?}', [
39 285
                'as'   => $routeName,
40 285
                'uses' => 'SleepingOwl\Admin\Http\Controllers\AdminController@inlineEdit',
41 285
            ]);
42 285
        }
43 285
    }
44
45
    protected $payload;
46
    /**
47
     * @var string
48
     */
49
    protected $name;
50
51
    /**
52
     * @param string|null $name
53
     */
54
    protected $distinct;
55
56
    /**
57
     * @var
58
     */
59
    protected $displaySearch = false;
60
61
    /**
62
     * @var
63
     */
64
    protected $displayLength = false;
65
66
    /**
67
     * @var array
68
     */
69
    protected $searchableColumns = [
70
        Text::class,
71
        Link::class,
72
        Email::class,
73
    ];
74
75
    /**
76
     * DisplayDatatablesAsync constructor.
77
     *
78
     * @param string|null $name
79
     * @param string|null $distinct
80
     */
81
    public function __construct($name = null, $distinct = null)
82
    {
83
        parent::__construct();
84
85
        $this->setName($name);
86
        $this->setDistinct($distinct);
87
88
        $this->getColumns()->setView('display.extensions.columns_async');
89
    }
90
91
    /**
92
     * Initialize display.
93
     */
94
    public function initialize()
95
    {
96
        parent::initialize();
97
98
        $attributes = Request::all();
99
        array_unshift($attributes, $this->getName());
100
        array_unshift($attributes, $this->getModelConfiguration()->getAlias());
101
102
        $this->setHtmlAttribute('style', 'width:100%');
103
        $this->setHtmlAttribute('data-url', route('admin.display.async', $attributes));
104
        $this->setHtmlAttribute('data-payload', json_encode($this->payload));
105
106
        if ($this->getDisplaySearch()) {
107
            $this->setHtmlAttribute('data-display-search', 1);
108
        }
109
110
        if ($this->getDisplayLength()) {
111
            $this->setHtmlAttribute('data-display-dtlength', 1);
112
        }
113
    }
114
115
    /**
116
     * @param bool $length
117
     * @return $this
118
     */
119
    public function setDisplayLength($length)
120
    {
121
        $this->displayLength = $length;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    public function getDisplayLength()
130
    {
131
        return $this->displayLength;
132
    }
133
134
    /**
135
     * @param $search
136
     * @return $this
137
     */
138
    public function setDisplaySearch($search)
139
    {
140
        $this->displaySearch = $search;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function getDisplaySearch()
149
    {
150
        return $this->displaySearch;
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function getName()
157
    {
158
        return $this->name;
159
    }
160
161
    /**
162
     * @param string $name
163
     *
164
     * @return $this
165
     */
166
    public function setName($name)
167
    {
168
        $this->name = $name;
169
170
        return $this;
171
    }
172
173
    /**
174
     * @return mixed
175
     */
176
    public function getDistinct()
177
    {
178
        return $this->distinct;
179
    }
180
181
    /**
182
     * @param mixed $distinct
183
     *
184
     * @return $this
185
     */
186
    public function setDistinct($distinct)
187
    {
188
        $this->distinct = $distinct;
189
190
        return $this;
191
    }
192
193
    /**
194
     * Render async request.
195
     *
196
     * @param \Illuminate\Http\Request $request
197
     *
198
     * @return array
199
     */
200
    public function renderAsync(\Illuminate\Http\Request $request)
201
    {
202
        $query = $this->getRepository()->getQuery();
203
        $totalCount = $query->count();
204
        $filteredCount = 0;
205
206
        if (! is_null($this->distinct)) {
207
            $filteredCount = $query->distinct()->count($this->getDistinct());
208
        }
209
210
        $this->modifyQuery($query);
211
        $this->applySearch($query, $request);
212
213
        if (is_null($this->distinct)) {
214
            $countQuery = clone $query;
215
            $countQuery->getQuery()->orders = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $orders.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
216
            $filteredCount = $countQuery->count();
217
        }
218
219
        $this->applyOffset($query, $request);
220
        $collection = $query->get();
221
222
        return $this->prepareDatatablesStructure($request, $collection, $totalCount, $filteredCount);
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $query->get() on line 220 can also be of type array<integer,object<Ill...base\Eloquent\Builder>>; however, SleepingOwl\Admin\Displa...reDatatablesStructure() does only seem to accept object<Illuminate\Support\Collection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
223
    }
224
225
    /**
226
     * Apply offset and limit to the query.
227
     *
228
     * @param $query
229
     * @param \Illuminate\Http\Request $request
230
     */
231
    protected function applyOffset($query, \Illuminate\Http\Request $request)
232
    {
233
        $offset = $request->input('start', 0);
234
        $limit = $request->input('length', 10);
235
236
        if ($limit == -1) {
237
            return;
238
        }
239
240
        $query->offset((int) $offset)->limit((int) $limit);
241
    }
242
243
    /**
244
     * Apply search to the query.
245
     *
246
     * @param Builder $query
247
     * @param \Illuminate\Http\Request $request
248
     */
249
    protected function applySearch(Builder $query, \Illuminate\Http\Request $request)
250
    {
251
        $search = $request->input('search.value');
252
        if (empty($search)) {
253
            return;
254
        }
255
256
        $query->where(function (Builder $query) use ($search) {
257
            $columns = $this->getColumns()->all();
258
259
            foreach ($columns as $column) {
260
                if (in_array(get_class($column), $this->searchableColumns)) {
261 View Code Duplication
                    if ($column instanceof ColumnInterface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
262
                        if (($metaInstance = $column->getMetaData()) instanceof ColumnMetaInterface) {
263
                            if (method_exists($metaInstance, 'onSearch')) {
264
                                $metaInstance->onSearch($column, $query, $search);
0 ignored issues
show
Bug introduced by
The method onSearch() does not seem to exist on object<SleepingOwl\Admin...ay\ColumnMetaInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
265
                                continue;
266
                            }
267
                        }
268
269
                        if (is_callable($callback = $column->getSearchCallback())) {
270
                            $callback($column, $query, $search);
271
                            continue;
272
                        }
273
                    }
274
275
                    $query->orWhere($column->getName(), 'like', '%'.$search.'%');
276
                }
277
            }
278
        });
279
    }
280
281
    /**
282
     * Convert collection to the datatables structure.
283
     *
284
     * @param \Illuminate\Http\Request $request
285
     * @param array|Collection $collection
286
     * @param int $totalCount
287
     * @param int $filteredCount
288
     *
289
     * @return array
290
     */
291
    protected function prepareDatatablesStructure(
292
        \Illuminate\Http\Request $request,
293
        Collection $collection,
294
        $totalCount,
295
        $filteredCount
296
    ) {
297
        $columns = $this->getColumns();
298
299
        $result = [];
300
        $result['draw'] = $request->input('draw', 0);
301
        $result['recordsTotal'] = $totalCount;
302
        $result['recordsFiltered'] = $filteredCount;
303
        $result['data'] = [];
304
305
        foreach ($collection as $instance) {
306
            $_row = [];
307
308
            foreach ($columns->all() as $column) {
309
                $column->setModel($instance);
310
311
                if ($column instanceof Control) {
312
                    $column->initialize();
313
                }
314
315
                $_row[] = (string) $column;
316
            }
317
318
            $result['data'][] = $_row;
319
        }
320
321
        return $result;
322
    }
323
324
    /**
325
     * @return Collection
326
     * @throws \Exception
327
     */
328
    public function getCollection()
329
    {
330
    }
331
332
    /**
333
     * @param $payload
334
     */
335
    public function setPayload($payload)
336
    {
337
        $this->payload = $payload;
338
    }
339
340
    /**
341
     * @return mixed
342
     */
343
    public function getPayload()
344
    {
345
        return $this->payload;
346
    }
347
348
    /**
349
     * @return array
350
     */
351
    public function toArray()
352
    {
353
        $params = parent::toArray();
354
        $params['payload'] = $this->payload;
355
356
        return $params;
357
    }
358
}
359