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 ( c14cfb...7a5a14 )
by Aden
03:33
created

ModelQuerying::getQueryFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace LaravelFlare\Flare\Traits\ModelAdmin;
4
5
trait ModelQuerying
6
{
7
    /**
8
     * Query
9
     * 
10
     * @var string
11
     */
12
    public $query;
13
14
    /**
15
     * Allows filtering of the default query, for instance:.
16
     *
17
     *      $queryFilter = [
18
     *                          'whereNotNull' => ['parent_id'],
19
     *                          'where' => ['name', 'John'],
20
     *                      ]
21
     *
22
     * Would result in an Eloquent query with the following scope:
23
     *     Model::whereNotNull('parent_id')->where('name', 'John')->get();
24
     *
25
     * Note: This queryFilter is not used for custom filters and
26
     * can also be overridden by setQueryFilter();
27
     * 
28
     * @var array
29
     */
30
    protected $queryFilter = [];
31
32
    /**
33
     * The number of models to return for pagination.
34
     *
35
     * @var int
36
     */
37
    protected $perPage = 15;
38
39
    /**
40
     * Order By - Column/Attribute to OrderBy.
41
     *
42
     * Primary Key of Model by default
43
     * 
44
     * @var string
45
     */
46
    protected $orderBy;
47
48
    /**
49
     * Sort By - Either Desc or Asc.
50
     * 
51
     * @var string
52
     */
53
    protected $sortBy;
54
55
    /**
56
     * Finds an existing Model entry and sets it to the current model.
57
     * 
58
     * @param int $modelitemId
59
     * 
60
     * @return
61
     */
62
    public function find($modelitemId)
63
    {
64
        $this->model = $this->model->findOrFail($modelitemId);
65
66
        return $this->model;
67
    }
68
69
    /**
70
     * Returns Model Items, either all() or paginated().
71
     *
72
     * Filtered by any defined query filters ($queryFilter)
73
     * Ordered by Managed Model orderBy and sortBy methods
74
     * 
75
     * @return
76
     */
77
    public function items()
78
    {
79
        $this->query = $this->model->newQuery();
80
81
        return $this->query();
82
    }
83
84
    /**
85
     * Returns All Model Items, either all() or paginated().
86
     *
87
     * Filtered by any defined query filters ($queryFilter)
88
     * Ordered by Managed Model orderBy and sortBy methods
89
     * 
90
     * @return
91
     */
92
    public function allItems()
93
    {
94
        if (!$this->hasSoftDeleting()) {
0 ignored issues
show
Bug introduced by
It seems like hasSoftDeleting() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
95
            throw new \Exception('Model does not have Soft Deleting');
96
        }
97
98
        $this->query = $this->model->newQuery()->withTrashed();
99
100
        return $this->query();
101
    }
102
103
    /**
104
     * Returns Model Items, either all() or paginated().
105
     *
106
     * Filtered by any defined query filters ($queryFilter)
107
     * Ordered by Managed Model orderBy and sortBy methods
108
     * 
109
     * @return
110
     */
111
    public function onlyTrashedItems()
112
    {
113
        if (!$this->hasSoftDeleting()) {
0 ignored issues
show
Bug introduced by
It seems like hasSoftDeleting() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
114
            throw new \Exception('Model does not have Soft Deleting');
115
        }
116
117
        $this->query = $this->model->newQuery()->onlyTrashed();
118
119
        return $this->query();
120
    }
121
122
    /**
123
     * Performs the Model Query
124
     * 
125
     * @return \Illuminate\Database\Eloquent\Collection
126
     */
127
    private function query()
128
    {
129
        $this->applyQueryFilters();
130
131
        if ($this->orderBy()) {
132
            $this->query = $this->query->orderBy(
133
                                $this->orderBy(),
134
                                $this->sortBy()
135
                            );
136
        }
137
138
        if ($this->perPage > 0) {
139
            return $this->query->paginate($this->perPage);
140
        }
141
142
        return $this->query->get();
143
    }
144
145
    /**
146
     * Return Totals of All, With Trashed and Only Trashed
147
     * 
148
     * @return array
149
     */
150
    public function totals()
151
    {
152
        if ($this->hasSoftDeleting()) {
0 ignored issues
show
Bug introduced by
It seems like hasSoftDeleting() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
153
            return [
154
                        'all' => $this->model->count(),
155
                        'with_trashed' => $this->model->withTrashed()->count(),
156
                        'only_trashed' => $this->model->onlyTrashed()->count(),
157
                    ];
158
        }
159
160
        return ['all' => $this->model->count()];
161
    }
162
163
    /**
164
     * Return Managed Model OrderBy.
165
     *
166
     * Primary key is default.
167
     *
168
     * @return string
169
     */
170
    public function orderBy()
171
    {
172
        if (\Request::input('order')) {
173
            return \Request::input('order');
174
        }
175
176
        if ($this->orderBy) {
177
            return $this->orderBy;
178
        }
179
    }
180
181
    /**
182
     * Return Managed Model SortBy (Asc or Desc).
183
     *
184
     * Descending is default.
185
     * 
186
     * @return string
187
     */
188
    public function sortBy()
189
    {
190
        if (\Request::input('sort')) {
191
            return \Request::input('sort');
192
        }
193
194
        if ($this->sortBy == 'asc') {
195
            return 'asc';
196
        }
197
198
        return 'desc';
199
    }
200
201
    /**
202
     * Get the number of models to return per page.
203
     *
204
     * @return int
205
     */
206
    public function getPerPage()
207
    {
208
        return $this->perPage;
209
    }
210
211
    /**
212
     * Set the number of models to return per page.
213
     *
214
     * @param int $perPage
215
     */
216
    public function setPerPage($perPage)
217
    {
218
        $this->perPage = $perPage;
219
    }
220
221
    /**
222
     * Apply the Query Filters
223
     * 
224
     * @return 
225
     */
226
    private function applyQueryFilters()
227
    {
228
        if (is_array($this->getQueryFilter())) {
229
            return $this->createQueryFilter();
230
        }
231
232
        return $this->getQueryFilter();
233
    }
234
235
    /**
236
     * Create the Query Filter from Array
237
     * 
238
     * @return
239
     */
240
    private function createQueryFilter()
241
    {
242
        if (count($this->getQueryFilter()) > 0) {
243
            foreach ($this->getQueryFilter() as $filter => $parameters) {
244
                if (!is_array($parameters)) {
245
                    $parameters = [$parameters];
246
                }
247
                $this->query = call_user_func_array([$this->query, $filter], $parameters);
248
            }
249
        }
250
    }
251
252
    /**
253
     * Access the Query Filter
254
     * 
255
     * @return 
256
     */
257
    public function getQueryFilter()
258
    {
259
        return $this->queryFilter;
260
    }
261
262
    /**
263
     * Set the Query Filter
264
     * 
265
     * @param array $filter
266
     *
267
     * @return void
268
     */
269
    public function setQueryFilter($filter = [])
270
    {
271
        $this->queryFilter = $filter;
272
    }
273
}
274