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.
Passed
Branch development (184ec4)
by butschster
06:08
created

DisplayDatatablesAsync::getCollection()   A

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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
15
class DisplayDatatablesAsync extends DisplayDatatables implements WithRoutesInterface
16
{
17
    /**
18
     * Register display routes.
19
     *
20
     * @param Router $router
21
     *
22
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
23
     */
24 283 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...
25
    {
26 283
        $routeName = 'admin.display.async';
27 283
        if (! $router->has($routeName)) {
28 283
            $router->get('{adminModel}/async/{adminDisplayName?}', [
29 283
                'as' => $routeName,
30 283
                'uses' => 'SleepingOwl\Admin\Http\Controllers\DisplayController@async',
31 283
            ]);
32 283
        }
33
34 283
        $routeName = 'admin.display.async.inlineEdit';
35 283
        if (! $router->has($routeName)) {
36 283
            $router->post('{adminModel}/async/{adminDisplayName?}', [
37 283
                'as' => $routeName,
38 283
                'uses' => 'SleepingOwl\Admin\Http\Controllers\AdminController@inlineEdit',
39 283
            ]);
40 283
        }
41 283
    }
42
43
    /**
44
     * @var string
45
     */
46
    protected $name;
47
48
    /**
49
     * @param string|null $name
50
     */
51
    protected $distinct;
52
53
    /**
54
     * @var array
55
     */
56
    protected $searchableColumns = [
57
        Text::class,
58
        Link::class,
59
        Email::class,
60
    ];
61
62
    /**
63
     * DisplayDatatablesAsync constructor.
64
     *
65
     * @param string|null $name
66
     * @param string|null $distinct
67
     */
68
    public function __construct($name = null, $distinct = null)
69
    {
70
        parent::__construct();
71
72
        $this->setName($name);
73
        $this->setDistinct($distinct);
74
75
        $this->getColumns()->setView('display.extensions.columns_async');
76
    }
77
78
    /**
79
     * Initialize display.
80
     */
81
    public function initialize()
82
    {
83
        parent::initialize();
84
85
        $attributes = Request::all();
86
        array_unshift($attributes, $this->getName());
87
        array_unshift($attributes, $this->getModelConfiguration()->getAlias());
88
89
        $this->setHtmlAttribute('data-url', route('admin.display.async', $attributes));
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getName()
96
    {
97
        return $this->name;
98
    }
99
100
    /**
101
     * @param string $name
102
     *
103
     * @return $this
104
     */
105
    public function setName($name)
106
    {
107
        $this->name = $name;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return mixed
114
     */
115
    public function getDistinct()
116
    {
117
        return $this->distinct;
118
    }
119
120
    /**
121
     * @param mixed $distinct
122
     *
123
     * @return $this
124
     */
125
    public function setDistinct($distinct)
126
    {
127
        $this->distinct = $distinct;
128
129
        return $this;
130
    }
131
132
    /**
133
     * Render async request.
134
     *
135
     * @param \Illuminate\Http\Request $request
136
     *
137
     * @return array
138
     */
139
    public function renderAsync(\Illuminate\Http\Request $request)
140
    {
141
        $query = $this->getRepository()->getQuery();
142
        $totalCount = $query->count();
143
        $filteredCount = 0;
144
145
        if (! is_null($this->distinct)) {
146
            $filteredCount = $query->distinct()->count($this->getDistinct());
147
        }
148
149
        $this->modifyQuery($query);
150
        $this->applySearch($query, $request);
151
152
        if (is_null($this->distinct)) {
153
            $filteredCount = $query->count();
154
        }
155
156
        $this->applyOffset($query, $request);
157
        $collection = $query->get();
158
159
        return $this->prepareDatatablesStructure($request, $collection, $totalCount, $filteredCount);
0 ignored issues
show
Bug introduced by
It seems like $collection defined by $query->get() on line 157 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...
160
    }
161
162
    /**
163
     * Apply offset and limit to the query.
164
     *
165
     * @param $query
166
     * @param \Illuminate\Http\Request $request
167
     */
168
    protected function applyOffset($query, \Illuminate\Http\Request $request)
169
    {
170
        $offset = $request->input('start', 0);
171
        $limit = $request->input('length', 10);
172
173
        if ($limit == -1) {
174
            return;
175
        }
176
177
        $query->offset($offset)->limit($limit);
178
    }
179
180
    /**
181
     * Apply search to the query.
182
     *
183
     * @param Builder $query
184
     * @param \Illuminate\Http\Request $request
185
     */
186
    protected function applySearch(Builder $query, \Illuminate\Http\Request $request)
187
    {
188
        $search = $request->input('search.value');
189
        if (empty($search)) {
190
            return;
191
        }
192
193
        $query->where(function ($query) use ($search) {
194
            $columns = $this->getColumns()->all();
195
            foreach ($columns as $column) {
196
                if (in_array(get_class($column), $this->searchableColumns)) {
197
                    $query->orWhere($column->getName(), 'like', '%'.$search.'%');
198
                }
199
            }
200
        });
201
    }
202
203
    /**
204
     * Convert collection to the datatables structure.
205
     *
206
     * @param \Illuminate\Http\Request $request
207
     * @param array|Collection $collection
208
     * @param int $totalCount
209
     * @param int $filteredCount
210
     *
211
     * @return array
212
     */
213
    protected function prepareDatatablesStructure(\Illuminate\Http\Request $request, Collection $collection, $totalCount, $filteredCount)
214
    {
215
        $columns = $this->getColumns();
216
217
        $result = [];
218
        $result['draw'] = $request->input('draw', 0);
219
        $result['recordsTotal'] = $totalCount;
220
        $result['recordsFiltered'] = $filteredCount;
221
        $result['data'] = [];
222
223
        foreach ($collection as $instance) {
224
            $_row = [];
225
226
            foreach ($columns->all() as $column) {
227
                $column->setModel($instance);
228
229
                if ($column instanceof Control) {
230
                    $column->initialize();
231
                }
232
233
                $_row[] = (string) $column;
234
            }
235
236
            $result['data'][] = $_row;
237
        }
238
239
        return $result;
240
    }
241
242
    /**
243
     * @return Collection
244
     * @throws \Exception
245
     */
246
    public function getCollection()
247
    {
248
    }
249
}
250