Completed
Push — development ( 059e30...d1b094 )
by Ashutosh
09:16
created

ExtendedOrderController::generateNumber()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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\Order\Order;
8
use App\Model\Product\Subscription;
9
use Bugsnag;
10
use Crypt;
11
use Illuminate\Http\Request;
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
                $expiryFrom = (new BaseSettingsController())->getDateFormat($expiry);
28
                $tills = (new BaseSettingsController())->getDateFormat();
29
30
                $tillDate = $this->getTillDate($expiryFrom, $expiryTill, $tills);
31
                $join = $join->whereBetween('subscriptions.ends_at', [$expiryFrom, $tillDate]);
32
            }
33
34
            if ($expiryTill) {
35
                $exptill = (new BaseSettingsController())->getDateFormat($expiryTill);
36
                $froms = Subscription::first()->ends_at;
37
                $fromDate = $this->getFromDate($expiry, $froms);
38
                $join = $join->whereBetween('subscriptions.ends_at', [$fromDate, $exptill]);
39
            }
40
            if ($from) {
41
                $fromdate = date_create($from);
42
43
                $from = date_format($fromdate, 'Y-m-d H:m:i');
44
                $tills = date('Y-m-d H:m:i');
45
46
                $tillDate = $this->getTillDate($from, $till, $tills);
47
                $join = $join->whereBetween('orders.created_at', [$from, $tillDate]);
48
            }
49
            if ($till) {
50
                $tilldate = date_create($till);
51
                $till = date_format($tilldate, 'Y-m-d H:m:i');
52
                $froms = Order::first()->created_at;
53
                $fromDate = $this->getFromDate($from, $froms);
54
                $join = $join->whereBetween('orders.created_at', [$fromDate, $till]);
55
            }
56
            if ($domain) {
57
                if (str_finish($domain, '/')) {
58
                    $domain = substr_replace($domain, '', -1, 0);
59
                }
60
                $join = $join->where('domain', 'LIKE', '%'.$domain.'%');
61
            }
62
            // dd($join->get());
63
            $join = $join->orderBy('created_at', 'desc')
64
        ->select('orders.id', 'orders.created_at', 'client',
65
            'price_override', 'order_status', 'product', 'number', 'serial_key');
66
67
            return $join;
68
        } catch (\Exception $ex) {
69
            dd($ex);
70
        }
71
    }
72
73
    public function getTillDate($from, $till, $tills)
74
    {
75
        if ($till) {
76
            $todate = date_create($till);
77
            $tills = date_format($todate, 'Y-m-d H:m:i');
78
        }
79
80
        return $tills;
81
    }
82
83
    public function getFromDate($from, $froms)
84
    {
85
        if ($from) {
86
            $fromdate = date_create($from);
87
            $froms = date_format($fromdate, 'Y-m-d H:m:i');
88
        }
89
90
        return $froms;
91
    }
92
93
    /**
94
     * Create orders.
95
     *
96
     * @param Request $request
97
     *
98
     * @return type
99
     */
100
    public function orderExecute(Request $request)
101
    {
102
        try {
103
            $invoiceid = $request->input('invoiceid');
104
            $execute = $this->executeOrder($invoiceid);
105
            if ($execute == 'success') {
106
                return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
107
            } else {
108
                return redirect()->back()->with('fails', \Lang::get('message.not-saved-successfully'));
109
            }
110
        } catch (\Exception $ex) {
111
            Bugsnag::notifyException($ex);
112
113
            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...
114
        }
115
    }
116
117
    /**
118
     * generating serial key if product type is downloadable.
119
     *
120
     * @param type $product_type
121
     *
122
     * @throws \Exception
123
     *
124
     * @return type
125
     */
126
    public function generateSerialKey($product_type)
127
    {
128
        try {
129
            // if ($product_type == 2) {
130
            $str = str_random(16);
131
            $str = strtoupper($str);
132
            $str = Crypt::encrypt($str);
133
134
            return $str;
135
            // }
136
        } catch (\Exception $ex) {
137
            Bugsnag::notifyException($ex);
138
139
            throw new \Exception($ex->getMessage());
140
        }
141
    }
142
143
    public function generateNumber()
144
    {
145
        try {
146
            return rand('10000000', '99999999');
147
        } catch (\Exception $ex) {
148
            throw new \Exception($ex->getMessage());
149
        }
150
    }
151
152
    public function changeDomain(Request $request)
153
    {
154
        $domain = '';
155
        $arrayOfDomains = array();
156
        $allDomains = $request->input('domain');
157
        $seperateDomains = explode(',', $allDomains);//Bifurcate the domains here
158
          
159
        $allowedDomains =$this->getAllowedDomains( $seperateDomains);
160
        $id = $request->input('id');
161
        $order = Order::findorFail($id);
162
        $clientEmail = $order->user->email;
163
        $order->domain = implode(",", $allowedDomains);
164
        $order->save();
165
        $cont = new \App\Http\Controllers\License\LicenseController();
166
        $updateLicensedDomain = $cont->updateLicensedDomain($clientEmail,$order->domain);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $updateLicensedDomain is correct as $cont->updateLicensedDom...tEmail, $order->domain) 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...
167
        //Now make Installation status as inactive
168
        $updateInstallStatus = $cont->updateInstalledDomain($clientEmail);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $updateInstallStatus is correct as $cont->updateInstalledDomain($clientEmail) 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...
169
170
        return ['message' => 'success', 'update'=>'Licensed Domain Updated'];
171
    }
172
173
174
    public function getAllowedDomains($seperateDomains)
175
    {
176
        $needle = 'www';
177
        foreach ($seperateDomains as $domain) {
178
         $isIP = (bool)ip2long($domain);
179
         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...
180
           $allowedDomains[] = $domain;
181
         } else {
182
          $customDomain = (strpos($domain, $needle) !== false) ? str_replace('www.', '', $domain) : 'www.'.$domain;
183
          $allowedDomains[] = ($domain.','.$customDomain);
184
         
185
         }
186
       }
187
        return  $allowedDomains;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $allowedDomains seems to be defined by a foreach iteration on line 177. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
188
    }
189
}
190