Completed
Push — development ( 84b942...7ca4dd )
by Ashutosh
07:25
created

ExtendedOrderController::advanceSearch()   F

Complexity

Conditions 10
Paths 932

Size

Total Lines 64
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 64
rs 3.5944
c 0
b 0
f 0
cc 10
nc 932
nop 7

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
2
3
namespace App\Http\Controllers\Order;
4
5
use Crypt;
6
use Bugsnag;
7
use App\Model\Order\Order;
8
use Illuminate\Http\Request;
9
use App\Model\Product\Subscription;
10
use App\Http\Controllers\Controller;
11
use App\Http\Controllers\Common\BaseSettingsController;
12
13
class ExtendedOrderController extends Controller
14
{
15
    public function advanceSearch($order_no = '', $product_id = '', $expiry = '', 
16
        $expiryTill='', $from = '', $till = '', $domain = '')
17
    {
18
        try{
19
        $join = Order::leftJoin('subscriptions', 'orders.id', '=', 'subscriptions.order_id');
20
        if ($order_no) {
21
            $join = $join->where('number', $order_no);
22
        }
23
        if ($product_id) {
24
            $join = $join->where('product', $product_id);
25
        }
26
        if ($expiry) {
27
            // $join = $join->where('ends_at', 'LIKE', '%'.$expiry.'%');
28
             $expiryFrom = (new BaseSettingsController)->getDateFormat($expiry);
29
            // $fromExpiryDate = date_create($expiry);
30
             
31
            // $from = date_format($fromExpiryDate, 'Y-m-d H:m:i');
32
            $tills = (new BaseSettingsController)->getDateFormat();
33
34
            $tillDate = $this->getTillDate($expiryFrom, $expiryTill, $tills);
35
            $join = $join->whereBetween('subscriptions.ends_at', [$expiryFrom, $tillDate]);
36
        }
37
 
38
39
40
         if ($expiryTill) {
41
          
42
            // $tillExpiryDate = date_create($expiryTill);
43
            // $till = date_format($tillExpiryDate, 'Y-m-d H:m:i');
44
              $exptill = (new BaseSettingsController)->getDateFormat($expiryTill);
45
            $froms = Subscription::first()->ends_at;
46
            $fromDate = $this->getFromDate($expiry, $froms);
47
            $join = $join->whereBetween('subscriptions.ends_at', [$fromDate, $exptill]);
48
         }
49
        if ($from) {
50
            $fromdate = date_create($from);
51
52
            $from = date_format($fromdate, 'Y-m-d H:m:i');
53
            $tills = date('Y-m-d H:m:i');
54
55
            $tillDate = $this->getTillDate($from, $till, $tills);
56
            $join = $join->whereBetween('orders.created_at', [$from, $tillDate]);
57
        }
58
        if ($till) {
59
            $tilldate = date_create($till);
60
            $till = date_format($tilldate, 'Y-m-d H:m:i');
61
            $froms = Order::first()->created_at;
62
            $fromDate = $this->getFromDate($from, $froms);
63
            $join = $join->whereBetween('orders.created_at', [$fromDate, $till]);
64
        }
65
        if ($domain) {
66
            if (str_finish($domain, '/')) {
67
                $domain = substr_replace($domain, '', -1, 0);
68
            }
69
            $join = $join->where('domain', 'LIKE', '%'.$domain.'%');
70
        }
71
// dd($join->get());
72
        $join = $join->orderBy('created_at', 'desc')
73
        ->select('orders.id', 'orders.created_at', 'client',
74
            'price_override', 'order_status','product', 'number', 'serial_key');
75
76
        return $join;
77
     } catch(\Exception $ex) {
78
        dd($ex);
79
     }
80
   }
81
82
    public function getTillDate($from, $till, $tills)
83
    {
84
        if ($till) {
85
            $todate = date_create($till);
86
            $tills = date_format($todate, 'Y-m-d H:m:i');
87
        }
88
89
        return $tills;
90
    }
91
92
    public function getFromDate($from, $froms)
93
    {
94
        if ($from) {
95
            $fromdate = date_create($from);
96
            $froms = date_format($fromdate, 'Y-m-d H:m:i');
97
        }
98
99
        return $froms;
100
    }
101
102
    /**
103
     * Create orders.
104
     *
105
     * @param Request $request
106
     *
107
     * @return type
108
     */
109
    public function orderExecute(Request $request)
110
    {
111
        try {
112
            $invoiceid = $request->input('invoiceid');
113
            $execute = $this->executeOrder($invoiceid);
114
            if ($execute == 'success') {
115
                return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
116
            } else {
117
                return redirect()->back()->with('fails', \Lang::get('message.not-saved-successfully'));
118
            }
119
        } catch (\Exception $ex) {
120
            Bugsnag::notifyException($ex);
121
122
            return redirect()->back()->with('fails', $ex->getMessage());
123
        }
124
    }
125
126
    /**
127
     * generating serial key if product type is downloadable.
128
     *
129
     * @param type $product_type
130
     *
131
     * @throws \Exception
132
     *
133
     * @return type
134
     */
135
    public function generateSerialKey($product_type)
136
    {
137
        try {
138
            // if ($product_type == 2) {
139
            $str = str_random(16);
140
            $str = strtoupper($str);
141
            $str = Crypt::encrypt($str);
142
143
            return $str;
144
            // }
145
        } catch (\Exception $ex) {
146
            Bugsnag::notifyException($ex);
147
148
            throw new \Exception($ex->getMessage());
149
        }
150
    }
151
152
    public function generateNumber()
153
    {
154
        try {
155
            return rand('10000000', '99999999');
156
        } catch (\Exception $ex) {
157
            throw new \Exception($ex->getMessage());
158
        }
159
    }
160
161
    public function domainChange(Request $request)
162
    {
163
        $domain = $request->input('domain');
164
        $id = $request->input('id');
165
        $order = Order::find($id);
166
        $order->domain = $domain;
167
        $order->save();
168
    }
169
}
170