Completed
Push — main ( 1bd1cf...5770f2 )
by Emmanuel
01:29
created

BillMe::createOrder()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 8.9599
c 0
b 0
f 0
cc 4
nc 4
nop 11

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 Epmnzava\BillMe\Models\Order;
14
use Epmnzava\BillMe\Models\Receipt;
15
use Epmnzava\BillMe\Models\Invoice;
16
use Epmnzava\BillMe\Models\OrderItem;
17
use Epmnzava\BillMe\Mail\Client\Invoices\InvoiceCreated;
18
use Epmnzava\BillMe\Mail\Client\OrderReceived;
19
use Epmnzava\BillMe\Mail\Merchant\NewOrder;
20
use Carbon\Carbon;
21
use Epmnzava\BillMe\Mail\Client\Invoices\InvoicePaid;
22
use Epmnzava\BillMe\Models\BillingPayment;
23
use Epmnzava\BillMe\Models\PaymentMethod;
24
use Mail;
25
26
class BillMe extends Queries
27
{
28
29
30
31
    /**
32
     * A function that triggers order creation
33
     *
34
     */
35
36
37
    public function createOrder(
38
        string $firstname,
39
        string $lastname,
40
        string $email,
41
        string $mobile_number,
42
        int $amount,
43
        string $payment_method,
44
        string $notes,
45
        string $ordertitle = "",
46
        string $address,
47
        array $orderitems,
48
        $userid = null
49
    ): Order {
50
51
        $order = new Order;
52
        $order->userid = $userid;
53
        $order->firstname = $firstname;
54
        $order->lastname = $lastname;
55
        $order->email = $email;
56
        $order->mobile_number = $mobile_number;
57
        $order->amount = $amount;
58
        $order->payment_method = $payment_method;
59
        $order->status = "pending";
60
        $order->notes = $notes;
61
        $order->ordertitle = $ordertitle;
62
        $order->address = $address;
63
        $order->date = date("Y-m-d");
64
65
        $order->save();
66
67
        // Loop through order items here
68
        if (!empty($orderitems)) {
69
70
            for ($i = 0; $i < count($orderitems); $i++) {
71
                $orderItem = new OrderItem();
72
                $orderItem->order_id = $order->id;
73
                $orderItem->amount = $orderitems[$i]['amount'];
74
                $orderItem->quantity = $orderitems[$i]['quantity'];
75
                $orderItem->item = $orderitems[$i]['item'];
76
                $orderItem->extra_details = serialize($orderitems[$i]['extra_details']);
77
                $orderItem->date = date("Y-m-d");
78
79
                $orderItem->save();
80
            }
81
        }
82
83
84
        $invoice = $this->createInvoice($order);
85
86
        if (config('bill-me.send_mail') == 1)
87
            $this->sendMailNotifications($order, $invoice);
88
89
        $billing_record = $this->add_billing_record($order, $invoice);
90
91
        return $order;
92
    }
93
94
95
96
    public function add_billing_record(Order $order, Invoice $invoice): BillingPayment
97
    {
98
99
        $bill_payment = new BillingPayment;
100
        $bill_payment->userid = $order->userid;
101
        $bill_payment->invoiceid = $invoice->id;
102
        $bill_payment->orderid = $order->id;
103
        $bill_payment->amount = $invoice->amount;
104
        $bill_payment->date = $order->date;
105
        $bill_payment->save();
106
107
108
        return $bill_payment;
109
    }
110
111
112
    /**
113
     * Function that triggers sending of email notification for orders
114
     */
115
116
    public function sendMailNotifications(Order $order, Invoice $invoice): void
117
    {
118
        Mail::to(["email" => $order->email])->send(new OrderReceived($order));
119
        Mail::to(["email" => config('bill-me.your_mail_address')])->send(new NewOrder($order));
120
        Mail::to(["address" => $invoice->email])->send(new InvoiceCreated($invoice));
121
    }
122
123
124
125
    /**
126
     * Function that creates an invoie from an order
127
     */
128
129
    public function createInvoice(Order $order): Invoice
130
    {
131
132
        $invoice = new Invoice;
133
        $invoice->orderid = $order->id;
134
        $invoice->userid = $order->userid;
135
        $invoice->firstname = $order->firstname;
136
        $invoice->lastname = $order->lastname;
137
        $invoice->mobile_number = $order->mobile_number;
138
        $invoice->email = $order->email;
139
        $invoice->invoice_title = $order->ordertitle;
140
        $invoice->amount = $order->amount;
141
        $invoice->status = $order->status;
142
        $invoice->address = $order->address;
143
        $invoice->date = date('Y-m-d');
144
        $invoice->due_date = Carbon::now()->addDays(config('bill-me.due_date_duration'))->format('Y-m-d');
145
        $invoice->save();
146
147
        $order_update = Order::find($order->id);
148
        $order_update->invoiceid = $invoice->id;
149
        $order_update->save();
150
151
        return $invoice;
152
    }
153
154
155
    public function order_paid($orderid): void
156
    {
157
158
        $invoiceid = Invoice::where('orderid', $orderid)->first()->id;
159
160
        $this->invoice_paid($invoiceid);
161
    }
162
163
164
    /**
165
     * Function gets @param invoiceid and updates order , invoice and billing record that the user has paid
166
     */
167
    public function invoice_paid($invoiceid): Invoice
168
    {
169
170
        $invoice = Invoice::find($invoiceid);
171
        $invoice->status = "paid";
172
        $invoice->save();
173
174
        $order = Order::find($invoice->orderid);
175
        $order->status = "completed";
176
        $order->save();
177
178
        $billing = $this->paid_billing_record($invoiceid);
179
180
        //$receiptid = $this->create_receipt($invoiceid, $billingid);
181
182
        // create email notification invoice paid order paid..
183
184
        if (config('bill-me.send_mail') == 1)
185
            Mail::to(["email" => $order->email])->send(new InvoicePaid($invoice));
186
187
188
        return  $invoice;
189
    }
190
191
    /**
192
     * Function gets @param invoiceid and @param billingid and creates receipt
193
     */
194
    public function create_receipt($invoiceid, $billingid): int
195
    {
196
197
        $receipt = new Receipt;
198
        $receipt->invoiceid = $invoiceid;
199
        $receipt->paymentid = $billingid;
200
        $receipt->save();
201
202
        return $receipt->id;
203
    }
204
205
206
207
    public function paid_billing_record($invoiceid): BillingPayment
208
    {
209
210
        $billing_record = BillingPayment::find(BillingPayment::where('invoiceid', $invoiceid)->first()->id);
211
        $billing_record->status = "paid";
212
        $billing_record->save();
213
214
        return  $billing_record;
215
    }
216
    /**
217
     * Function that gets you invoice details by using orderid
218
     */
219
220
    public function getInvoiceByOrderId($orderid)
221
    {
222
        return Invoice::where('orderid', $orderid)->first();
223
    }
224
225
    /**
226
     * Function that gets you invoice details by using invoiceid
227
     */
228
229
    public function getInvoiceByInvoiceId($invoiceid)
230
    {
231
232
        return Invoice::find($invoiceid);
233
    }
234
235
236
237
238
239
    /**
240
     * Function that gets you order details by using orderid
241
     */
242
243
    public function getOrderByOrderId($orderid)
244
    {
245
        return Order::find($orderid);
246
    }
247
248
    /**
249
     * Function that gets you order details by using invoiceid
250
     */
251
252
    public function getOrderByInvoiceId($invoiceid)
253
    {
254
        return Order::find(Invoice::where('id', $invoiceid)->first()->orderid);
255
    }
256
257
258
    /**
259
     * Function that updates invoice status and returns void
260
     */
261
    public function update_invoice_status(string $invoiceid, string $status): void
262
    {
263
        $invoice = Invoice::find($invoiceid);
264
        $invoice->status = $status;
265
        $invoice->save();
266
    }
267
268
    /**
269
     * Function to update invoice
270
     */
271
272
    public function update_invoice(string $invoiceid, Invoice $invoice): Invoice
273
    {
274
    }
275
276
277
278
279
    /**
280
     * Function that updates order status and returns void
281
     */
282
    public function update_order_status(string $orderid, string $status): void
283
    {
284
        $order = Order::find($orderid);
285
        $order->status = $status;
286
        $order->save();
287
    }
288
289
290
    /**
291
     * Function that updates an  order  returns void
292
     */
293
    public function update_order(string $order_id, Order $order): void
294
    {
295
    }
296
297
298
    /**
299
     * Function that updates and  status to cancelled  returns void
300
     */
301
    public function cancel_order(string $orderid): void
302
    {
303
        $order = Order::find($orderid);
304
        $order->status = "cancelled";
305
        $order->save();
306
    }
307
308
309
    /**
310
     * Function that deletes an order returns void
311
     */
312
    public function delete_order(string $orderid)
313
    {
314
315
        $order = Order::find($orderid);
316
        $order->delete();
317
        $this->delete_invoice($orderid);
318
    }
319
320
    /**
321
     * Function that deletes an invoice returns void
322
     */
323
    public function delete_invoice(string $orderid): void
324
    {
325
326
        $invoice = Invoice::where('orderid', $orderid)->delete();
327
    }
328
329
330
    public function editPaymentMethod($pmethod_id, $pmethod)
331
    {
332
        $pmethod = PaymentMethod::find($pmethod_id);
333
        $pmethod->pmethod = $pmethod;
334
        $pmethod->save();
335
        return $pmethod;
336
    }
337
338
    
339
    public function addPaymentMethod(string $pmethod)
340
    {
341
342
343
        $pmethod =new PaymentMethod;
344
        $pmethod->pmethod=$pmethod
345
        $pmethod->save();
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_VARIABLE
Loading history...
346
347
      
348
        return $pmethod;
349
    }
350
}
351