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 ( 508770...7d3333 )
by Aden
03:03
created

ModelQuerying   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 15
c 3
b 0
f 1
lcom 1
cbo 1
dl 0
loc 147
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 6 1
B items() 0 26 6
A orderBy() 0 10 3
A sortBy() 0 12 3
A getPerPage() 0 4 1
A setPerPage() 0 4 1
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
        if (count($this->query_filter) > 0) {
72
            foreach ($this->query_filter as $filter => $parameters) {
73
                if (!is_array($parameters)) {
74
                    $parameters = [$parameters];
75
                }
76
                $model = call_user_func_array([$this->model, $filter], $parameters);
77
            }
78
        }
79
80
        if ($this->orderBy()) {
81
            $model = $model->orderBy(
82
                                $this->orderBy(),
83
                                $this->sortBy()
84
                            );
85
        }
86
87
        if ($this->perPage > 0) {
88
            return $model->paginate($this->perPage);
89
        }
90
91
        return $model->all();
92
    }
93
94
    /**
95
     * Return Managed Model OrderBy.
96
     *
97
     * Primary key is default.
98
     *
99
     * @return string
100
     */
101
    public function orderBy()
102
    {
103
        if (\Request::input('order')) {
104
            return \Request::input('order');
105
        }
106
107
        if ($this->orderBy) {
108
            return $this->orderBy;
109
        }
110
    }
111
112
    /**
113
     * Return Managed Model SortBy (Asc or Desc).
114
     *
115
     * Descending is default.
116
     * 
117
     * @return string
118
     */
119
    public function sortBy()
120
    {
121
        if (\Request::input('sort')) {
122
            return \Request::input('sort');
123
        }
124
125
        if ($this->sortBy == 'asc') {
126
            return 'asc';
127
        }
128
129
        return 'desc';
130
    }
131
132
    /**
133
     * Get the number of models to return per page.
134
     *
135
     * @return int
136
     */
137
    public function getPerPage()
138
    {
139
        return $this->perPage;
140
    }
141
142
    /**
143
     * Set the number of models to return per page.
144
     *
145
     * @param int $perPage
146
     */
147
    public function setPerPage($perPage)
148
    {
149
        $this->perPage = $perPage;
150
    }
151
}
152