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 ( db959c...2802ed )
by Aden
04:36
created

ModelQuerying::onlyTrashedItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 10
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace LaravelFlare\Flare\Traits\ModelAdmin;
4
5
trait ModelQuerying
6
{
7
    /**
8
     * Allows filtering of the query, for instance:.
9
     *
10
     *      $query_filter = [
11
     *                          'whereNotNull' => ['parent_id'],
12
     *                          'where' => ['name', 'John'],
13
     *                      ]
14
     *
15
     * Would result in an Eloquent query with the following scope:
16
     *     Model::whereNotNull('parent_id')->where('name', 'John')->get();
17
     * 
18
     * @var array
19
     */
20
    protected $query_filter = [];
21
22
    /**
23
     * The number of models to return for pagination.
24
     *
25
     * @var int
26
     */
27
    protected $perPage = 15;
28
29
    /**
30
     * Order By - Column/Attribute to OrderBy.
31
     *
32
     * Primary Key of Model by default
33
     * 
34
     * @var string
35
     */
36
    protected $orderBy;
37
38
    /**
39
     * Sort By - Either Desc or Asc.
40
     * 
41
     * @var string
42
     */
43
    protected $sortBy;
44
45
    /**
46
     * Finds an existing Model entry and sets it to the current model.
47
     * 
48
     * @param int $modelitem_id
49
     * 
50
     * @return
51
     */
52
    public function find($modelitem_id)
53
    {
54
        $this->model = $this->model->findOrFail($modelitem_id);
55
56
        return $this->model;
57
    }
58
59
    /**
60
     * Returns Model Items, either all() or paginated().
61
     *
62
     * Filtered by any defined query filters ($query_filter)
63
     * Ordered by Managed Model orderBy and sortBy methods
64
     * 
65
     * @return
66
     */
67
    public function items()
68
    {
69
        $model = $this->model;
70
71
        return $this->query($model);
72
    }
73
74
    /**
75
     * Returns All Model Items, either all() or paginated().
76
     *
77
     * Filtered by any defined query filters ($query_filter)
78
     * Ordered by Managed Model orderBy and sortBy methods
79
     * 
80
     * @return
81
     */
82 View Code Duplication
    public function allItems()
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...
83
    {
84
        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...
85
            throw new \Exception('Model does not have Soft Deleting');
86
        }
87
        
88
        $model = $this->model->withTrashed();
89
90
        return $this->query($model);
91
    }
92
93
    /**
94
     * Returns Model Items, either all() or paginated().
95
     *
96
     * Filtered by any defined query filters ($query_filter)
97
     * Ordered by Managed Model orderBy and sortBy methods
98
     * 
99
     * @return
100
     */
101 View Code Duplication
    public function onlyTrashedItems()
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...
102
    {
103
        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...
104
            throw new \Exception('Model does not have Soft Deleting');
105
        }
106
        
107
        $model = $this->model->onlyTrashed();
108
109
        return $this->query($model);
110
    }
111
112
    /**
113
     * Performs the Model Query
114
     * 
115
     * @param  \Illuminate\Database\Eloquent\Model $model
116
     * 
117
     * @return \Illuminate\Database\Eloquent\Collection
118
     */
119
    private function query($model)
120
    {
121
        if (count($this->query_filter) > 0) {
122
            foreach ($this->query_filter as $filter => $parameters) {
123
                if (!is_array($parameters)) {
124
                    $parameters = [$parameters];
125
                }
126
                $model = call_user_func_array([$this->model, $filter], $parameters);
127
            }
128
        }
129
130
        if ($this->orderBy()) {
131
            $model = $model->orderBy(
132
                                $this->orderBy(),
133
                                $this->sortBy()
134
                            );
135
        }
136
137
        if ($this->perPage > 0) {
138
            return $model->paginate($this->perPage);
139
        }
140
141
        return $model->get();
142
    }
143
144
    /**
145
     * Return Totals of All, With Trashed and Only Trashed
146
     * 
147
     * @return array
148
     */
149
    public function totals()
150
    {
151
        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...
152
            return [
153
                        'all' => $this->model->count(),
154
                        'with_trashed' => $this->model->withTrashed()->count(),
155
                        'only_trashed' => $this->model->onlyTrashed()->count(),
156
                    ];
157
        }
158
159
        return ['all' => $this->model->count()];
160
    }
161
162
    /**
163
     * Return Managed Model OrderBy.
164
     *
165
     * Primary key is default.
166
     *
167
     * @return string
168
     */
169
    public function orderBy()
170
    {
171
        if (\Request::input('order')) {
172
            return \Request::input('order');
173
        }
174
175
        if ($this->orderBy) {
176
            return $this->orderBy;
177
        }
178
    }
179
180
    /**
181
     * Return Managed Model SortBy (Asc or Desc).
182
     *
183
     * Descending is default.
184
     * 
185
     * @return string
186
     */
187
    public function sortBy()
188
    {
189
        if (\Request::input('sort')) {
190
            return \Request::input('sort');
191
        }
192
193
        if ($this->sortBy == 'asc') {
194
            return 'asc';
195
        }
196
197
        return 'desc';
198
    }
199
200
    /**
201
     * Get the number of models to return per page.
202
     *
203
     * @return int
204
     */
205
    public function getPerPage()
206
    {
207
        return $this->perPage;
208
    }
209
210
    /**
211
     * Set the number of models to return per page.
212
     *
213
     * @param int $perPage
214
     */
215
    public function setPerPage($perPage)
216
    {
217
        $this->perPage = $perPage;
218
    }
219
}
220