Completed
Branch feature-dynamic-fields (3b03cc)
by Ashutosh
09:05
created

ExtendedOrderController::domain()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace App\Http\Controllers\Order;
4
5
use App\Http\Controllers\Common\BaseSettingsController;
6
use App\Http\Controllers\Controller;
7
use App\Model\Common\StatusSetting;
8
use App\Model\Order\Order;
9
use App\Model\Product\Subscription;
10
use Bugsnag;
11
use Illuminate\Http\Request;
12
13
class ExtendedOrderController extends Controller
14
{
15
    /**
16
     * Perform Advance Search for Orders Page.
17
     *
18
     * @author Ashutosh Pathak <[email protected]>
19
     *
20
     * @date   2019-01-19T01:35:08+0530
21
     *
22
     * @param string $order_no
23
     * @param string $product_id
24
     * @param string $expiry
25
     * @param string $expiryTill
26
     * @param string $from
27
     * @param string $till
28
     * @param string $domain
29
     *
30
     * @return array
31
     */
32
    public function advanceSearch(
33
        $order_no = '',
34
        $product_id = '',
35
        $expiry = '',
36
        $expiryTill = '',
37
        $from = '',
38
        $till = '',
39
        $domain = ''
40
    ) {
41
        try {
42
            $join = Order::leftJoin('subscriptions', 'orders.id', '=', 'subscriptions.order_id');
43
            ($this->orderNum($order_no, $join));
44
            $this->product($product_id, $join);
45
            $this->expiry($expiryTill, $expiry, $join);
46
            $this->expiryTill($expiry, $expiryTill, $join);
47
            $this->orderFrom($till, $from, $join);
48
            $this->orderTill($from, $till, $join);
49
            $this->domain($domain, $join);
50
51
            $join = $join->orderBy('created_at', 'desc')
52
           ->select(
53
               'orders.id',
54
               'orders.created_at',
55
               'client',
56
            'price_override',
57
               'order_status',
58
               'product',
59
               'number',
60
               'serial_key'
61
           );
62
63
            return $join;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $join also could return the type Illuminate\Database\Eloq...\Database\Query\Builder which is incompatible with the documented return type array.
Loading history...
64
        } catch (\Exception $ex) {
65
            dd($ex);
66
        }
67
    }
68
69
    /**
70
     * Searches for Order No.
71
     *
72
     * @param int             $order_no The Order NO to be searched
73
     * @param App\Model\Order $join     The Order instance
74
     *
75
     * @return $join
0 ignored issues
show
Documentation Bug introduced by
The doc comment $join at position 0 could not be parsed: Unknown type name '$join' at position 0 in $join.
Loading history...
76
     */
77
    private function orderNum($order_no, $join)
78
    {
79
        if ($order_no) {
80
            $join = $join->where('number', $order_no);
81
82
            return $join;
83
        }
84
    }
85
86
    /**
87
     * Searches for Product.
88
     *
89
     * @param int             $order_no The Order NO to be searched
90
     * @param App\Model\Order $join     The Order instance
91
     *
92
     * @return $join
0 ignored issues
show
Documentation Bug introduced by
The doc comment $join at position 0 could not be parsed: Unknown type name '$join' at position 0 in $join.
Loading history...
93
     */
94
    private function product($product_id, $join)
95
    {
96
        if ($product_id) {
97
            $join = $join->where('product', $product_id);
98
99
            return $join;
100
        }
101
    }
102
103
    /**
104
     * Searches for Expiry From.
105
     *
106
     * @param string $expiry The Expiry From Date
107
     * @param object $join
108
     *
109
     * @return Query
110
     */
111
    private function expiry($expiryTill, $expiry, $join)
112
    {
113
        if ($expiry) {
114
            $expiryFrom = (new BaseSettingsController())->getDateFormat($expiry);
115
            $tills = (new BaseSettingsController())->getDateFormat();
116
117
            $tillDate = $this->getTillDate($expiryFrom, $expiryTill, $tills);
118
            $join = $join->whereBetween('subscriptions.ends_at', [$expiryFrom, $tillDate]);
119
120
            return $join;
121
        }
122
    }
123
124
    /**
125
     * Searches for Expiry Till.
126
     *
127
     * @param string $expiry The Expiry Till Date
128
     * @param object $join
129
     *
130
     * @return Query
131
     */
132
    private function expiryTill($expiry, $expiryTill, $join)
133
    {
134
        if ($expiryTill) {
135
            $exptill = (new BaseSettingsController())->getDateFormat($expiryTill);
136
            $froms = Subscription::first()->ends_at;
137
            $fromDate = $this->getFromDate($expiry, $froms);
138
            $join = $join->whereBetween('subscriptions.ends_at', [$fromDate, $exptill]);
139
140
            return $join;
141
        }
142
    }
143
144
    /**
145
     * Searches for Order From Date.
146
     *
147
     * @param string $expiry The Order From Date
148
     * @param object $join
149
     *
150
     * @return Query
151
     */
152
    public function orderFrom($till, $from, $join)
153
    {
154
        if ($from) {
155
            $fromdate = date_create($from);
156
157
            $from = date_format($fromdate, 'Y-m-d H:m:i');
158
            $tills = date('Y-m-d H:m:i');
159
160
            $tillDate = $this->getTillDate($from, $till, $tills);
161
            $join = $join->whereBetween('orders.created_at', [$from, $tillDate]);
162
163
            return $join;
164
        }
165
    }
166
167
    /**
168
     * Searches for Order Till Date.
169
     *
170
     * @param string $expiry The Order Till Date
171
     * @param object $join
172
     *
173
     * @return Query
174
     */
175
    public function orderTill($from, $till, $join)
176
    {
177
        if ($till) {
178
            $tilldate = date_create($till);
179
            $till = date_format($tilldate, 'Y-m-d H:m:i');
180
            $froms = Order::first()->created_at;
181
            $fromDate = $this->getFromDate($from, $froms);
182
            $join = $join->whereBetween('orders.created_at', [$fromDate, $till]);
183
184
            return $join;
185
        }
186
    }
187
188
    /**
189
     * Searches for Domain.
190
     *
191
     * @param string $domain domaiin
192
     * @param object $join
193
     *
194
     * @return Query
195
     */
196
    public function domain($domain, $join)
197
    {
198
        if ($domain) {
199
            if (str_finish($domain, '/')) {
200
                $domain = substr_replace($domain, '', -1, 0);
201
            }
202
            $join = $join->where('domain', 'LIKE', '%'.$domain.'%');
203
204
            return $join;
205
        }
206
    }
207
208
    public function getTillDate($from, $till, $tills)
209
    {
210
        if ($till) {
211
            $todate = date_create($till);
212
            $tills = date_format($todate, 'Y-m-d H:m:i');
213
        }
214
215
        return $tills;
216
    }
217
218
    public function getFromDate($from, $froms)
219
    {
220
        if ($from) {
221
            $fromdate = date_create($from);
222
            $froms = date_format($fromdate, 'Y-m-d H:m:i');
223
        }
224
225
        return $froms;
226
    }
227
228
    /**
229
     * Create orders.
230
     *
231
     * @param Request $request
232
     *
233
     * @return type
234
     */
235
    public function orderExecute(Request $request)
236
    {
237
        try {
238
            $invoiceid = $request->input('invoiceid');
239
            $execute = $this->executeOrder($invoiceid);
240
            if ($execute == 'success') {
241
                return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
242
            } else {
243
                return redirect()->back()->with('fails', \Lang::get('message.not-saved-successfully'));
244
            }
245
        } catch (\Exception $ex) {
246
            Bugsnag::notifyException($ex);
247
248
            return redirect()->back()->with('fails', $ex->getMessage());
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...ls', $ex->getMessage()) also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type App\Http\Controllers\Order\type.
Loading history...
249
        }
250
    }
251
252
    /**
253
     * generate serial key and add no of agents in the last 4 digits og the 16 string/digit serial key .
254
     *
255
     * @param int $productid
256
     * @param int $agents    No Of Agents
257
     *
258
     * @throws \Exception
259
     *
260
     * @return string The Final Serial Key after adding no of agents in the last 4 digits
261
     */
262
    public function generateSerialKey(int $productid, $agents)
263
    {
264
        try {
265
            $len = strlen($agents);
266
            switch ($len) {//Get Last Four digits based on No.Of Agents
267
268
                case '1':
269
                   $lastFour = '000'.$agents;
270
                    break;
271
                   case '2':
272
273
                    $lastFour = '00'.$agents;
274
                     break;
275
                      case '3':
276
                    $lastFour = '0'.$agents;
277
                     break;
278
                      case '4':
279
                    $lastFour = $agents;
280
281
                     break;
282
                default:
283
                    $lastFour = '0000';
284
                    break;
285
            }
286
            $str = strtoupper(str_random(12));
287
            $licCode = $str.$lastFour;
288
289
            return $licCode;
290
        } catch (\Exception $ex) {
291
            app('log')->error($ex->getMessage());
292
            Bugsnag::notifyException($ex);
293
294
            throw new \Exception($ex->getMessage());
295
        }
296
    }
297
298
    public function generateNumber()
299
    {
300
        try {
301
            return rand('10000000', '99999999');
302
        } catch (\Exception $ex) {
303
            throw new \Exception($ex->getMessage());
304
        }
305
    }
306
307
    public function changeDomain(Request $request)
308
    {
309
        $domain = '';
310
        $arrayOfDomains = [];
311
        $allDomains = $request->input('domain');
312
        $seperateDomains = explode(',', $allDomains); //Bifurcate the domains here
313
        $allowedDomains = $this->getAllowedDomains($seperateDomains);
314
        $id = $request->input('id');
315
        $order = Order::findorFail($id);
316
        $licenseCode = $order->serial_key;
317
        $order->domain = implode(',', $allowedDomains);
318
        $order->save();
319
        $licenseStatus = StatusSetting::pluck('license_status')->first();
320
        if ($licenseStatus == 1) {
321
            $expiryDate = $order->subscription->ends_at;
322
            $cont = new \App\Http\Controllers\License\LicenseController();
323
            $updateLicensedDomain = $cont->updateLicensedDomain($licenseCode, $order->domain, $order->product, $expiryDate, $order->number);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $updateLicensedDomain is correct as $cont->updateLicensedDom...ryDate, $order->number) targeting App\Http\Controllers\Lic...:updateLicensedDomain() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
324
            //Now make Installation status as inactive
325
            $updateInstallStatus = $cont->updateInstalledDomain($licenseCode, $order->product);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $updateInstallStatus is correct as $cont->updateInstalledDo...eCode, $order->product) targeting App\Http\Controllers\Lic...updateInstalledDomain() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
326
        }
327
328
        return ['message' => 'success', 'update'=>'Licensed Domain Updated'];
329
    }
330
331
    public function getAllowedDomains($seperateDomains)
332
    {
333
        $needle = 'www';
334
        foreach ($seperateDomains as $domain) {
335
            $isIP = (bool) ip2long($domain);
336
            if ($isIP == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
337
                $allowedDomains[] = $domain;
338
            } else {
339
                $customDomain = (strpos($domain, $needle) !== false) ? str_replace('www.', '', $domain) : 'www.'.$domain;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
340
                $allowedDomains[] = ($domain.','.$customDomain);
341
            }
342
        }
343
344
        return  $allowedDomains;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $allowedDomains seems to be defined by a foreach iteration on line 334. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
345
    }
346
}
347