Completed
Push — main ( ad262c...205bcf )
by Emmanuel
01:13
created

BillMe::createOrder()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 9.1563
c 0
b 0
f 0
cc 3
nc 4
nop 9

How to fix   Many Parameters   

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\Invoice;
15
use Epmnzava\BillMe\Models\OrderItem;
16
use Epmnzava\BillMe\Mail\Client\Invoices\InvoiceCreated;
17
use Epmnzava\BillMe\Mail\Client\OrderReceived;
18
use Epmnzava\BillMe\Mail\Merchant\NewOrder;
19
20
use Mail;
21
22
class BillMe
23
{
24
25
26
27
    /** A function that triggers order creation */
28
    public function createOrder(
29
        string $firstname,
30
        string $lastname,
31
        string $email,
32
        string $mobile_number,
33
        int $amount,
34
        string $payment_method,
35
        string $notes,
36
        string $address,
37
        array $orderitems
38
    ) {
39
40
        $order = new Order;
41
        $order->firstname = $firstname;
42
        $order->lastname = $lastname;
43
        $order->email = $email;
44
        $order->mobile_number = $mobile_number;
45
        $order->amount = $amount;
46
        $order->payment_method = $payment_method;
47
        $order->status = "Pending";
48
        $order->notes = $notes;
49
        $order->address = $address;
50
        $order->date = date("Y-m-d");
51
52
        $order->save();
53
54
        // order items ... here
55
56
        if (!empty($orderitems)) {
57
            /* $orderItem=new OrderItem();
58
          $orderItem->order_id=$order->id;
59
          $orderItem->amount=$orderitems->amount;
60
          $orderItem->quantity=$orderitems->quantity;
61
62
63
          */
64
        }
65
66
        //perform checks if the user needs email service use a separate function here add bulk sms functionality ...
67
        if (config('billme.send_mail')) {
68
            Mail::to(["address" => $order->email, "name" => $order->firstname])->send(new OrderReceived($order));
69
            Mail::to(["address" => $order->email, "name" => $order->firstname])->send(new NewOrder($order));
70
        }
71
72
73
        $this->createInvoice($order);
74
    }
75
76
77
78
79
    public function createInvoice(Order $order)
80
    {
81
82
        $invoice = new Invoice;
83
        $invoice->orderid = $order->id;
84
        $invoice->firstname = $order->firstname;
85
        $invoice->lastname = $order->lastname;
86
        $invoice->mobile_number = $order->mobile_number;
87
        $invoice->email = $order->email;
88
        $invoice->amount = $order->amount;
89
        $invoice->status = $order->status;
90
        $invoice->address = $order->address;
91
        $invoice->date = date('Y-m-d');
92
        $invoice->save();
93
94
        if (config('billme.send_mail'))
95
            Mail::to(["address" => $invoice->email, "name" => $invoice->email])->send(new InvoiceCreated($invoice));
96
97
98
99
100
        $order_update = Order::find($order->id);
101
        $order_update->invoiceid = $invoice->id;
102
        $order_update->save();
103
    }
104
105
106
    /**
107
     * Function that gets you invoice details by using orderid
108
     */
109
110
    public function getInvoiceByOrderId($orderid)
111
    {
112
        return Invoice::where('orderid', $orderid)->first();
113
    }
114
115
    /**
116
     * Function that gets you invoice details by using invoiceid
117
     */
118
119
    public function getInvoiceByInvoiceId($invoiceid)
120
    {
121
        return Invoice::find($invoiceid);
122
    }
123
124
125
126
    /**
127
     * Function that gets you order details by using orderid
128
     */
129
130
    public function getOrderByOrderId($orderid)
131
    {
132
        return Order::find($orderid);
133
    }
134
135
    /**
136
     * Function that gets you order details by using invoiceid
137
     */
138
139
    public function getOrderByInvoiceId($invoiceid)
140
    {
141
        return Order::find(Invoice::where('id', $invoiceid)->first()->orderid);
142
    }
143
144
145
    /**
146
     * Function that updates invoice status and returns void
147
     */
148
    public function update_invoice_status(string $invoiceid, string $status): void
149
    {
150
        $invoice = Invoice::find($invoiceid);
151
        $invoice->status = $status;
152
        $invoice->save();
153
    }
154
155
156
    public function update_invoice(string $invoiceid, Invoice $invoice): void
0 ignored issues
show
Unused Code introduced by
The parameter $invoiceid 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 $invoice 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...
157
    {
158
    }
159
160
161
162
163
    /**
164
     * Function that updates order status and returns void
165
     */
166
    public function update_order_status(string $orderid, string $status): void
167
    {
168
        $order = Order::find($orderid);
169
        $order->status = $status;
170
        $order->save();
171
    }
172
173
174
175
    public function update_order(string $order_id, Order $order): void
0 ignored issues
show
Unused Code introduced by
The parameter $order_id 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 $order 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...
176
    {
177
    }
178
179
    public function cancel_order(string $orderid) : void
180
    {
181
        $order = Order::find($orderid);
182
        $order->status ="CANCELLED";
183
        $order->save();
184
    }
185
186
    public function delete_order(string $order_id)
0 ignored issues
show
Unused Code introduced by
The parameter $order_id 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...
187
    {
188
    }
189
}
190