Passed
Push — master ( 7b9381...037114 )
by Matthijs
04:14
created

HandleOrderStatusEmail::handle()   D

Complexity

Conditions 32
Paths 118

Size

Total Lines 149
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 32
eloc 84
nc 118
nop 1
dl 0
loc 149
rs 4.0166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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:

1
<?php namespace Hideyo\Ecommerce\Framework\Services\Order\Events\Handlers;
2
3
use Hideyo\Ecommerce\Framework\Services\Order\Events\OrderChangeStatus;
4
5
use Illuminate\Queue\InteractsWithQueue;
6
use Illuminate\Contracts\Queue\ShouldBeQueued;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Queue\ShouldBeQueued was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Mail;
8
use File;
9
use Hideyo\Ecommerce\Framework\Services\Invoice\InvoiceFacade as InvoiceService;
10
use Hideyo\Ecommerce\Framework\Services\GeneralSetting\GeneralSettingFacade as GeneralSettingService;
11
use Notification;
12
13
class HandleOrderStatusEmail
14
{
15
    /**
16
     * Handle the event.
17
     *
18
     * @param  OrderChangeStatus  $event
19
     * @return void
20
     */
21
    public function handle(OrderChangeStatus $event)
22
    {
23
        if($event->order->shop->wholesale) {
24
25
            if ($event->status->send_email_to_customer) {
26
    
27
                if ($event->status->orderStatusEmailTemplate) {
28
29
                    $destinationPath = storage_path() . "/app";
30
                    $orderStatusEmailFromResult = GeneralSettingService::selectOneByShopIdAndName($event->order->shop_id, 'order-status-email-from');
0 ignored issues
show
Bug introduced by
The method selectOneByShopIdAndName() does not exist on Hideyo\Ecommerce\Framewo...ng\GeneralSettingFacade. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
                    /** @scrutinizer ignore-call */ 
31
                    $orderStatusEmailFromResult = GeneralSettingService::selectOneByShopIdAndName($event->order->shop_id, 'order-status-email-from');
Loading history...
31
                    
32
                    $orderStatusEmailFrom = '[email protected]';
33
                    if ($orderStatusEmailFromResult) {
34
                        $orderStatusEmailFrom = $orderStatusEmailFromResult->value;
35
                    }
36
37
                    $orderStatusEmailNameResult = GeneralSettingService::selectOneByShopIdAndName($event->order->shop_id, 'order-status-email-name');
38
                    
39
                    $orderStatusEmailName = 'Phil & Phae B2B';
40
                    if ($orderStatusEmailNameResult) {
41
                        $orderStatusEmailName = $orderStatusEmailNameResult->value;
42
                    }
43
44
45
                    $orderStatusEmailBcc = '[email protected]';
46
                    $language = 'en';
47
                    if(strtoupper($event->order->orderBillAddress->country) == 'NL') {
48
                        $language = 'nl';
49
                    }    
50
            
51
52
                    Mail::send('frontend.email.order-status', ['content' => Cart::replaceTags($event->status->orderStatusEmailTemplate->translate($language)->content, $event->order)], function ($message) use ($event, $destinationPath, $orderStatusEmailFrom, $orderStatusEmailName, $orderStatusEmailBcc, $language) {
0 ignored issues
show
Bug introduced by
The type Hideyo\Ecommerce\Framewo...er\Events\Handlers\Cart was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The property content does not seem to exist on Illuminate\Database\Eloquent\Builder.
Loading history...
53
                        $message->from($orderStatusEmailFrom, $orderStatusEmailName);
54
                        $message->to($event->order->client->email, $event->order->orderBillAddress->firstname)->subject(Cart::replaceTags($event->status->orderStatusEmailTemplate->translate($language)->subject, $event->order));
0 ignored issues
show
Bug introduced by
The property subject does not seem to exist on Illuminate\Database\Eloquent\Builder.
Loading history...
55
56
                        if ($orderStatusEmailBcc) {
57
                            $message->bcc($orderStatusEmailBcc, $orderStatusEmailName);
58
                        }
59
60
                        if ($event->order and $event->status->attach_order_to_email) {
61
                            $pdfText = "";
62
                            $sign = '&euro;';
63
                            if($event->order->client->usd) {
0 ignored issues
show
Bug introduced by
The property usd does not seem to exist on Hideyo\Ecommerce\Framewo...es\Client\Entity\Client. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
64
                                $sign = '&#36;';
65
                            }
66
67
68
                            $pdf = \PDF::loadView('admin.order.pdf-wholesale', array('order' => $event->order, 'sign' => $sign, 'pdfText' => $pdfText))->setPaper('a4', 'landscape'); 
0 ignored issues
show
Bug introduced by
The type PDF was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
69
                            if (!File::exists($destinationPath.'/order/')) {
70
                                File::makeDirectory($destinationPath.'/order/', 0777, true);
71
                            }
72
73
                            $upload_success = $pdf->save($destinationPath.'/order/order-'.$event->order->generated_custom_order_id.'.pdf');
0 ignored issues
show
Unused Code introduced by
The assignment to $upload_success is dead and can be removed.
Loading history...
74
                            $message->attach($destinationPath.'/order/order-'.$event->order->generated_custom_order_id.'.pdf');
75
                            Notification::success('Email has order attachment');
76
                        }
77
78
                        Notification::success('Email with order status has been sent to '.$event->order->client->email.' from info@'.$orderStatusEmailFrom);
79
                    });
80
81
                    if ($event->status->attach_order_to_email) {
82
                        File::delete($destinationPath.'/order/order-'.$event->order->generated_custom_order_id.'.pdf');
83
                    }
84
                }
85
86
                if($event->order->type == 'pre_order') {
0 ignored issues
show
Bug introduced by
The property type does not seem to exist on Hideyo\Ecommerce\Framewo...ices\Order\Entity\Order. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
87
88
                }elseif($event->order->type == 'from_stock') {
89
90
                }
91
92
            }
93
94
        } else {
95
            if ($event->status->send_email_to_customer) {
96
                if ($event->status->orderStatusEmailTemplate) {
97
                    $destinationPath = storage_path() . "/app";
98
                    $orderStatusEmailFromResult = GeneralSettingService::selectOneByShopIdAndName($event->order->shop_id, 'order-status-email-from');
99
                    
100
                    $orderStatusEmailFrom = '[email protected]';
101
                    if ($orderStatusEmailFromResult) {
102
                        $orderStatusEmailFrom = $orderStatusEmailFromResult->value;
103
                    }
104
105
                    $orderStatusEmailNameResult = GeneralSettingService::selectOneByShopIdAndName($event->order->shop_id, 'order-status-email-name');
106
                    
107
                    $orderStatusEmailName = 'Phil & Phae';
108
                    if ($orderStatusEmailNameResult) {
109
                        $orderStatusEmailName = $orderStatusEmailNameResult->value;
110
                    }
111
112
                    $orderStatusEmailBccResult = GeneralSettingService::selectOneByShopIdAndName($event->order->shop_id, 'order-status-email-bcc');
113
                    $orderStatusEmailBcc = false;
114
                    
115
                    if ($orderStatusEmailBccResult) {
116
                        $orderStatusEmailBcc = $orderStatusEmailBccResult->value;
117
                    }
118
119
                    $language = 'en';
120
                    if(strtoupper($event->order->orderBillAddress->country) == 'NL') {
121
                        $language = 'nl';
122
                    }    
123
124
                    Mail::send('frontend.email.order-status', ['content' => Cart::replaceTags($event->status->orderStatusEmailTemplate->translate($language)->content, $event->order)], function ($message) use ($event, $destinationPath, $orderStatusEmailFrom, $orderStatusEmailName, $orderStatusEmailBcc, $language) {
125
                        $message->from($orderStatusEmailFrom, $orderStatusEmailName);
126
                        $message->to($event->order->client->email, $event->order->orderBillAddress->firstname)->subject(Cart::replaceTags($event->status->orderStatusEmailTemplate->translate($language)->subject, $event->order));
0 ignored issues
show
Bug introduced by
The property subject does not seem to exist on Illuminate\Database\Eloquent\Builder.
Loading history...
127
128
                        if ($orderStatusEmailBcc) {
129
                            $message->bcc($orderStatusEmailBcc, $orderStatusEmailName);
130
                        }
131
132
                        if ($event->order->invoice and $event->status->attach_invoice_to_email) {
133
                            $pdf = \PDF::loadView('admin.invoice.pdf-consumer', array('invoice' => InvoiceService::find($event->order->invoice->id)));
134
                            if (!File::exists($destinationPath.'/invoice/')) {
135
                                File::makeDirectory($destinationPath.'/invoice/', 0777, true);
136
                            }
137
138
                            $upload_success = $pdf->save($destinationPath.'/invoice/invoice-'.$event->order->invoice->generated_custom_invoice_id.'.pdf');
0 ignored issues
show
Unused Code introduced by
The assignment to $upload_success is dead and can be removed.
Loading history...
139
                            $message->attach($destinationPath.'/invoice/invoice-'.$event->order->invoice->generated_custom_invoice_id.'.pdf');
140
                            Notification::success('Email has invoice attachment');
141
                        }
142
143
                        if ($event->order and $event->status->attach_order_to_email) {
144
                            $text = $this->sendingPaymentMethodRelated->selectOneByShopIdAndPaymentMethodIdAndSendingMethodId($event->order->shop->id, $event->order->orderPaymentMethod->payment_method_id, $event->order->orderSendingMethod->sending_method_id);
0 ignored issues
show
Bug Best Practice introduced by
The property sendingPaymentMethodRelated does not exist on Hideyo\Ecommerce\Framewo...\HandleOrderStatusEmail. Did you maybe forget to declare it?
Loading history...
145
                        
146
                            $pdfText = "";
147
                            if ($text) {
148
                                $pdfText = Cart::replaceTags($text->pdf_text, $event->order);
149
                            }
150
151
                            $pdf = \PDF::loadView('admin.order.pdf', array('order' => $event->order, 'pdfText' => $pdfText));
152
                            if (!File::exists($destinationPath.'/order/')) {
153
                                File::makeDirectory($destinationPath.'/order/', 0777, true);
154
                            }
155
156
                            $upload_success = $pdf->save($destinationPath.'/order/order-'.$event->order->generated_custom_order_id.'.pdf');
157
                            $message->attach($destinationPath.'/order/order-'.$event->order->generated_custom_order_id.'.pdf');
158
                            Notification::success('Email has order attachment');
159
                        }
160
161
                        Notification::success('Email with order status has been sent to '.$event->order->client->email.' from info@'.$orderStatusEmailFrom);
162
                    });
163
164
                    if ($event->status->attach_invoice_to_email AND $event->order->invoice) {
165
                        File::delete($destinationPath.'/invoice/invoice-'.$event->order->invoice->generated_custom_invoice_id.'.pdf');
166
                    }
167
168
                    if ($event->status->attach_order_to_email) {
169
                        File::delete($destinationPath.'/order/order-'.$event->order->generated_custom_order_id.'.pdf');
170
                    }
171
                }
172
            }
173
        }
174
    }
175
}