Completed
Push — main ( 205bcf...cdec47 )
by Emmanuel
01:46
created

BillMe::createOrder()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 9.1781
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
        $this->createInvoice($order);
73
    }
74
75
76
77
78
    public function createInvoice(Order $order)
79
    {
80
81
        $invoice = new Invoice;
82
        $invoice->orderid = $order->id;
83
        $invoice->firstname = $order->firstname;
84
        $invoice->lastname = $order->lastname;
85
        $invoice->mobile_number = $order->mobile_number;
86
        $invoice->email = $order->email;
87
        $invoice->amount = $order->amount;
88
        $invoice->status = $order->status;
89
        $invoice->address = $order->address;
90
        $invoice->date = date('Y-m-d');
91
        $invoice->save();
92
93
        if (config('billme.send_mail'))
94
            Mail::to(["address" => $invoice->email, "name" => $invoice->email])->send(new InvoiceCreated($invoice));
95
96
97
98
99
        $order_update = Order::find($order->id);
100
        $order_update->invoiceid = $invoice->id;
101
        $order_update->save();
102
    }
103
104
105
    /**
106
     * Function that gets you invoice details by using orderid
107
     */
108
109
    public function getInvoiceByOrderId($orderid)
110
    {
111
        return Invoice::where('orderid', $orderid)->first();
112
    }
113
114
    /**
115
     * Function that gets you invoice details by using invoiceid
116
     */
117
118
    public function getInvoiceByInvoiceId($invoiceid)
119
    {
120
        return Invoice::find($invoiceid);
121
    }
122
123
124
125
    /**
126
     * Function that gets you order details by using orderid
127
     */
128
129
    public function getOrderByOrderId($orderid)
130
    {
131
        return Order::find($orderid);
132
    }
133
134
    /**
135
     * Function that gets you order details by using invoiceid
136
     */
137
138
    public function getOrderByInvoiceId($invoiceid)
139
    {
140
        return Order::find(Invoice::where('id', $invoiceid)->first()->orderid);
141
    }
142
143
144
    /**
145
     * Function that updates invoice status and returns void
146
     */
147
    public function update_invoice_status(string $invoiceid, string $status): void
148
    {
149
        $invoice = Invoice::find($invoiceid);
150
        $invoice->status = $status;
151
        $invoice->save();
152
    }
153
154
155
    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...
156
    {
157
    }
158
159
160
161
162
    /**
163
     * Function that updates order status and returns void
164
     */
165
    public function update_order_status(string $orderid, string $status): void
166
    {
167
        $order = Order::find($orderid);
168
        $order->status = $status;
169
        $order->save();
170
    }
171
172
173
174
    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...
175
    {
176
    }
177
178
    public function cancel_order(string $orderid): void
179
    {
180
        $order = Order::find($orderid);
181
        $order->status = "Cancelled";
182
        $order->save();
183
    }
184
185
    public function delete_order(string $orderid)
186
    {
187
188
        $order = Order::find($orderid);
189
        $order->delete();
190
        $this->delete_invoice($orderid);
191
    }
192
193
    public function delete_invoice(string $orderid): void
194
    {
195
196
        $invoice = Invoice::where('orderid', $orderid)->delete();
0 ignored issues
show
Unused Code introduced by
$invoice is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
197
    }
198
}
199