Completed
Push — main ( e2e758...26f2ec )
by Emmanuel
01:18
created

Queries   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 353
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 43
lcom 2
cbo 5
dl 0
loc 353
rs 8.96
c 0
b 0
f 0

43 Methods

Rating   Name   Duplication   Size   Complexity  
A orders() 0 5 1
A orders_orderby() 0 3 1
A orders_today() 0 4 1
A orders_todayByStatus() 0 4 1
A orders_thisMonth() 0 4 1
A orders_thisMonthByStatus() 0 4 1
A orders_thisYear() 0 5 1
A orders_thisYearByStatus() 0 5 1
A get_orders_with_status() 0 4 1
A pending_orders() 0 4 1
A cancelled_orders() 0 4 1
A completed_orders() 0 4 1
A getOrderById() 0 4 1
A getOrdersOnDate() 0 4 1
A getOrdersOnDateRange() 0 3 1
A getInvoiceById() 0 4 1
A getInvoiceByOrderId() 0 4 1
A getUserOrders() 0 4 1
A getUserOrdersByStatus() 0 4 1
A getOrdersByStatus() 0 4 1
A getUserInvoices() 0 4 1
A totalUserInvoices() 0 4 1
A getUserInvoiceByStatus() 0 4 1
A getInvoiceByStatus() 0 4 1
A sumUserInvoiceByStatus() 0 4 1
A totalInvoiceByStatus() 0 4 1
A sumInvoiceByStatus() 0 4 1
A totalUserInvoiceByStatus() 0 4 1
A getAllBillingHistory() 0 5 1
A getUserBillingHistory() 0 5 1
A getUserBillingHistoryByStartDate() 0 5 1
A getUserBillingHistoryBetweenDates() 0 5 1
A updateBillingHistory() 0 4 1
A updateInvoiceByInstance() 0 4 1
A updateDueDate() 0 7 1
A getBillingHistoryByStatus() 0 5 1
A getUserBillingHistoryByStatus() 0 5 1
A getOrderItems() 0 5 1
A getOrderItemsByInvoiceId() 0 5 1
A getPaymentMethods() 0 4 1
A getPaymentMethodById() 0 4 1
A editPaymentMethod() 0 7 1
A addPaymentMethod() 0 11 1

How to fix   Complexity   

Complex Class

Complex classes like Queries often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Queries, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Author: Emmanuel Paul Mnzava
5
 * Twitter: @epmnzava
6
 * Github: https://github.com/dbrax/bill-me
7
 * Email: [email protected]
8
 *
9
 */
10
11
namespace Epmnzava\BillMe;
12
13
use Carbon\Carbon;
14
use Epmnzava\BillMe\Models\BillingPayment;
15
use Epmnzava\BillMe\Models\Order;
16
use Epmnzava\BillMe\Models\Invoice;
17
use Epmnzava\BillMe\Models\OrderItem;
18
use Epmnzava\BillMe\Mail\Client\Invoices\InvoiceCreated;
19
use Epmnzava\BillMe\Mail\Client\OrderReceived;
20
use Epmnzava\BillMe\Mail\Merchant\NewOrder;
21
use Epmnzava\BillMe\Models\PaymentMethod;
22
23
24
use Mail;
25
26
class Queries extends Stats
27
{
28
29
    public function orders()
30
    {
31
32
        return Order::all();
33
    }
34
35
36
    public function orders_orderby($orderby)
0 ignored issues
show
Unused Code introduced by
The parameter $orderby is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
    {
38
    }
39
40
41
42
    public function orders_today()
43
    {
44
        return Order::whereDate('date', date('Y-m-d'))->get();
45
    }
46
47
48
49
    public function orders_todayByStatus($status)
50
    {
51
        return Order::whereDate('date', date('Y-m-d'))->where('status',$status)->get();
52
    }
53
54
55
56
    public function orders_thisMonth()
57
    {
58
        return Order::whereYear('date', date('Y'))->whereMonth('date', date('m'))->get();
59
    }
60
61
     public function orders_thisMonthByStatus($status)
62
    {
63
        return Order::whereYear('date', date('Y'))->whereMonth('date', date('m'))->where('status',$status)->get();
64
    }
65
66
67
68
    public function orders_thisYear()
69
    {
70
        return Order::whereYear('date', date('Y'))->get();
71
72
    }
73
74
75
     public function orders_thisYearByStatus($status)
76
    {
77
        return Order::whereYear('date', date('Y'))->where('status',$status)->get();
78
79
    }
80
81
82
    public function get_orders_with_status($status)
83
    {
84
        return Order::where('status', $status)->get();
85
    }
86
87
    public function pending_orders()
88
    {
89
        return Order::where('status', "pending")->get();
90
    }
91
92
    public function cancelled_orders()
93
    {
94
        return Order::where('status', "cancelled")->get();
95
    }
96
97
    public function completed_orders()
98
    {
99
        return Order::where('status', "completed")->get();
100
    }
101
102
    public function getOrderById($orderid)
103
    {
104
        return Order::find($orderid);
105
    }
106
107
    public function getOrdersOnDate($date)
108
    {
109
        return Order::where('date', $date)->get();
110
    }
111
112
113
    public function getOrdersOnDateRange($startdate, $enddate)
0 ignored issues
show
Unused Code introduced by
The parameter $startdate is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $enddate is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
    {
115
    }
116
117
118
    /** Function to get an invoice */
119
    public function getInvoiceById($invoiceid)
120
    {
121
        return Invoice::find($invoiceid);
122
    }
123
124
125
    /** Function to get an invoice */
126
    public function getInvoiceByOrderId($orderid)
127
    {
128
        return Invoice::find(Order::where('id', $orderid)->first()->invoiceid);
129
    }
130
131
132
133
    /**
134
     *
135
     * User Queries
136
     */
137
138
139
    /** Function to get given user orders */
140
    public function getUserOrders($userid)
141
    {
142
        return Order::where('userid', $userid)->get();
143
    }
144
145
146
    /** Function to get given user orders by status */
147
    public function getUserOrdersByStatus($userid, $status)
148
    {
149
        return Order::where('userid', $userid)->where('status', $status)->get();
150
    }
151
152
153
154
155
    /** Function to get  orders by status */
156
    public function getOrdersByStatus( $status)
157
    {
158
        return Order::where('status', $status)->get();
159
    }
160
161
162
163
    /** Function to get given user invoices */
164
    public function getUserInvoices($userid)
165
    {
166
        return Invoice::where('userid', $userid)->get();
167
    }
168
169
170
    /** Function that returns total number of user invoices */
171
    public function totalUserInvoices($userid): int
172
    {
173
        return Invoice::where('userid', $userid)->count();
174
    }
175
176
177
178
179
    /** Function to get given user invoices by status */
180
    public function getUserInvoiceByStatus($userid, $status)
181
    {
182
        return Invoice::where('userid', $userid)->where('status', $status)->get();
183
    }
184
185
186
187
188
    /** Function to get given all  invoices by status */
189
    public function getInvoiceByStatus( $status)
190
    {
191
        return Invoice::where('status', $status)->get();
192
    }
193
194
195
    /**
196
     * @param $userid
197
     * @param $status
198
     * @return mixed
199
     * Function to get given user invoices by status
200
     */
201
    public function sumUserInvoiceByStatus($userid, $status) : int
202
    {
203
        return Invoice::where('userid', $userid)->where('status', $status)->sum('amount');
204
    }
205
206
207
208
209
210
    /**
211
     * @param $status
212
     * @return mixed
213
     *  Function to get given user invoices by status
214
     */
215
    public function totalInvoiceByStatus($status)
216
    {
217
        return Invoice::where('status', $status)->count();
218
    }
219
220
221
222
223
    /**
224
     * @param $status
225
     * @return mixed
226
     *  Function to get given user invoices by status
227
     */
228
    public function sumInvoiceByStatus($status)
229
    {
230
        return Invoice::where('status', $status)->sum('amount');
231
    }
232
233
234
235
236
    /**
237
     * @param $userid
238
     * @param $status
239
     * @return mixed
240
     *  Function to get given user invoices by status
241
     */
242
    public function totalUserInvoiceByStatus($userid, $status)
243
    {
244
        return Invoice::where('userid', $userid)->where('status', $status)->count();
245
    }
246
247
    /** Function that gets full  billing history */
248
249
    public function getAllBillingHistory()
250
    {
251
252
        return BillingPayment::all();
253
    }
254
255
256
    /** Function that gets full user billing history*/
257
258
    public function getUserBillingHistory($userid)
259
    {
260
261
        return BillingPayment::where('userid', $userid)->get();
262
    }
263
264
265
266
267
    public function getUserBillingHistoryByStartDate($userid, $start_date)
268
    {
269
270
        return BillingPayment::where('userid', $userid)->where('date', $start_date)->get();
271
    }
272
273
274
    /** Function to get payment history of a given user for a given period */
275
276
    public function getUserBillingHistoryBetweenDates($userid, $start_date, $enddate)
277
    {
278
279
        return BillingPayment::where('userid', $userid)->whereBetween('date', [$start_date, $enddate])->get();
280
    }
281
282
283
    /**
284
     * Returns the model instance to be updated
285
     */
286
    public function updateBillingHistory($invoiceid): BillingPayment
287
    {
288
        return BillingPayment::find(BillingPayment::where('invoiceid', $invoiceid)->first()->id);
289
    }
290
291
292
    /**
293
     * Returns the model instance to be updated
294
     */
295
    public function updateInvoiceByInstance($invoiceid): Invoice
296
    {
297
        return Invoice::find($invoiceid);
298
    }
299
300
    /**
301
     *
302
     * Returns updated invoice with new due_date
303
     */
304
    public function updateDueDate($date, $invoiceid): Invoice
305
    {
306
        $invoice = $this->updateInvoiceByInstance($invoiceid);
307
        $invoice->due_date = $date;
308
        $invoice->save();
309
        return $invoice;
310
    }
311
312
313
314
 public function getBillingHistoryByStatus($status)
315
    {
316
317
        return BillingPayment::where('status', $status)->get();
318
    }
319
320
321
    public function getUserBillingHistoryByStatus($userid, $status)
322
    {
323
324
        return BillingPayment::where('userid', $userid)->where('status', $status)->get();
325
    }
326
327
    /** Return OrderItems for particular order
328
     *@param $orderid
329
     **/
330
    public function getOrderItems($orderid)
331
    {
332
333
        return OrderItem::where('order_id', $orderid)->get();
334
    }
335
336
337
    /** Return OrderItems for particular order gets invoiceid
338
     *@param $invoiceid
339
     **/
340
    public function getOrderItemsByInvoiceId($invoiceid)
341
    {
342
343
        return OrderItem::where('order_id', Invoice::where('id', $invoiceid)->first()->orderid)->get();
344
    }
345
346
    public function getPaymentMethods(){
347
348
        return PaymentMethod::all();
349
    }
350
351
      public function getPaymentMethodById($pid){
0 ignored issues
show
Unused Code introduced by
The parameter $pid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
352
353
        return PaymentMethod::find($id);
0 ignored issues
show
Bug introduced by
The variable $id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
354
    }
355
356
        public function editPaymentMethod($pmethod_id, $pmethod)
357
    {
358
        $pmethodObj = PaymentMethod::find($pmethod_id);
359
        $pmethodObj->pmethod = $pmethod;
360
        $pmethodObj->save();
361
        return $pmethodObj;
362
    }
363
364
    
365
    public function addPaymentMethod(string $pmethod)
366
    {
367
368
369
        $pmethodObj = new PaymentMethod;
370
        $pmethodObj->pmethod=$pmethod;
371
        $pmethodObj->save();
372
373
      
374
        return $pmethodObj;
375
    }
376
377
    
378
}
379