Completed
Push — development ( 1edf8d...636245 )
by Ashutosh
10:23
created

LicenseController::deleteProductFromAPL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\License;
4
5
use App\ApiKey;
6
use App\Http\Controllers\Controller;
7
use App\Model\Order\Order;
8
use App\Model\Product\Product;
9
use App\User;
10
11
class LicenseController extends Controller
12
{
13
    private $api_key_secret;
14
    private $url;
15
    private $license;
16
17
    public function __construct()
18
    {
19
        $model = new ApiKey();
20
        $this->license = $model->first();
21
22
        $this->api_key_secret = $this->license->license_api_secret;
23
        $this->url = $this->license->license_api_url;
24
    }
25
26
    private function postCurl($post_url, $post_info)
27
    {
28
        $ch = curl_init();
29
        curl_setopt($ch, CURLOPT_URL, $post_url);
30
        curl_setopt($ch, CURLOPT_POST, 1);
31
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_info);
32
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
33
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
34
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
35
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
36
        $result = curl_exec($ch);
37
        curl_close($ch);
38
39
        return $result;
40
    }
41
42
    /*
43
    *  Add New Product
44
    */
45
    public function addNewProduct($product_name, $product_sku)
46
    {
47
        $url = $this->url;
48
        $api_key_secret = $this->api_key_secret;
49
        $addProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=products_add&product_title=$product_name&product_sku=$product_sku&product_status=1");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 174 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...
50
    }
51
52
    /*
53
   *  Add New User
54
   */
55
    public function addNewUser($first_name, $last_name, $email)
56
    {
57
        $url = $this->url;
58
        $api_key_secret = $this->api_key_secret;
59
        $addProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=clients_add
60
      &client_fname=$first_name&client_lname=$last_name&client_email=$email&client_status=1");
61
    }
62
63
    /*
64
   *  Edit Product
65
   */
66
    public function editProduct($product_name, $product_sku)
67
    {
68
        $productId = $this->searchProductId($product_sku);
69
        $url = $this->url;
70
        $api_key_secret = $this->api_key_secret;
71
        $addProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=products_edit
72
      &product_id=$productId&product_title=$product_name&product_sku=$product_sku&product_status=1");
73
    }
74
75
    /*
76
   *  Search for product id while updating client
77
   */
78
    public function searchProductId($product_sku)
79
    {
80
        try {
81
            $productId = '';
82
            $url = $this->url;
83
            $api_key_secret = $this->api_key_secret;
84
            $getProductId = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search
85
            &search_type=product&search_keyword=$product_sku");
86
            $details = json_decode($getProductId);
87
            if ($details->api_error_detected == 0 && is_array($details->page_message)) {//This is not true if Product_sku is updated
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 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...
88
                $productId = $details->page_message[0]->product_id;
89
            }
90
91
            return $productId;
92
        } catch (\Exception $ex) {
93
            $result = [$ex->getMessage()];
94
95
            return response()->json(compact('result'), 500);
96
        }
97
    }
98
99
    public function deleteProductFromAPL($product)
100
    {
101
        $url = $this->url;
102
        $api_key_secret = $this->api_key_secret;
103
        $productId = $this->searchProductId($product->product_sku);
104
        $productTitle =  $product->name;
105
        $productSku = $product->sku;
106
        $delProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=products_edit
107
         &product_id=$productId&product_title=$productTitle&product_sku=$productSku&product_status=1&delete_record=1");
108
    }
109
110
    /*
111
   *  Edit User
112
   */
113
    public function editUserInLicensing($first_name, $last_name, $email)
114
    {
115
        $userId = $this->searchForUserId($email);
116
        $url = $this->url;
117
        $api_key_secret = $this->api_key_secret;
118
        $addProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=clients_edit&client_id=$userId
119
      &client_fname=$first_name&client_lname=$last_name&client_email=$email&client_status=1");
120
    }
121
122
    /*
123
   *  Search for user id while updating client
124
   */
125
    public function searchForUserId($email)
126
    {
127
        $userId = '';
128
        $url = $this->url;
129
        $api_key_secret = $this->api_key_secret;
130
        $getUserId = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search
131
      &search_type=client&search_keyword=$email");
132
133
        $details = json_decode($getUserId);
134
        if ($details->api_error_detected == 0 && is_array($details->page_message)) {//This is not true if email is updated
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 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...
135
            $userId = $details->page_message[0]->client_id;
136
        }
137
138
        return $userId;
139
    }
140
141
    /*
142
    *  Create New License For User
143
    */
144
    public function createNewLicene($orderid, $product, $user_id,
145
        $licenseExpiry, $updatesExpiry, $supportExpiry, $serial_key)
146
    {
147
        $url = $this->url;
148
        $api_key_secret = $this->api_key_secret;
149
        $sku = Product::where('id', $product)->first()->product_sku;
150
        $licenseExpiry = ($licenseExpiry != '') ? $licenseExpiry->toDateString() : '';
151
        $updatesExpiry = ($updatesExpiry != '') ? $updatesExpiry->toDateString() : '';
152
        $supportExpiry = ($supportExpiry != '') ? $supportExpiry->toDateString() : '';
153
        $order = Order::where('id', $orderid)->first();
154
        $orderNo = $order->number;
155
        $domain = $order->domain;
156
        $productId = $this->searchProductId($sku);
157
        $addLicense = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=licenses_add&product_id=$productId&license_code=$serial_key&license_require_domain=1&license_status=1&license_order_number=$orderNo&license_domain=$domain&license_limit=6&license_expire_date=$licenseExpiry&license_updates_date=$updatesExpiry&license_support_date=$supportExpiry&license_disable_ip_verification=0");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 403 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...
158
    }
159
160
    /*
161
    *  Edit Existing License
162
    */
163
    public function updateLicensedDomain($licenseCode, $domain, $productId, $licenseExpiry, $updatesExpiry, $supportExpiry, $orderNo)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 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...
164
    {
165
        $l_expiry = '';
166
        $s_expiry = '';
167
        $u_expiry = '';
168
        if (strtotime($licenseExpiry) > 1) {
169
            $l_expiry = date('Y-m-d', strtotime($licenseExpiry));
170
        }
171
        if (strtotime($updatesExpiry) > 1) {
172
            $u_expiry = date('Y-m-d', strtotime($updatesExpiry));
173
        }
174
        if (strtotime($supportExpiry) > 1) {
175
            $s_expiry = date('Y-m-d', strtotime($supportExpiry));
176
        }
177
        $url = $this->url;
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
            $requiredomain = 0;
181
            $ip = $domain;
182
            $domain = '';
183
        } else {
184
            $requiredomain = 1;
185
            $domain = $domain;
186
            $ip = '';
187
        }
188
        $api_key_secret = $this->api_key_secret;
189
        $searchLicense = $this->searchLicenseId($licenseCode, $productId);
190
        $licenseId = $searchLicense['licenseId'];
191
        $productId = $searchLicense['productId'];
192
        $licenseCode = $searchLicense['code'];
193
        $updateLicense = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=licenses_edit&product_id=$productId&license_code=$licenseCode&license_id=$licenseId&license_order_number=$orderNo&license_require_domain=$requiredomain&license_status=1&license_expire_date=$l_expiry&license_updates_date=$u_expiry&license_support_date=$s_expiry&license_domain=$domain&license_ip=$ip");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 393 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...
194
    }
195
196
    public function searchLicenseId($licenseCode, $productId)
197
    {
198
        $license = '';
199
        $product = '';
200
        $code = '';
201
        $url = $this->url;
202
        $api_key_secret = $this->api_key_secret;
203
        $getLicenseId = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search
204
        &search_type=license&search_keyword=$licenseCode");
205
        $details = json_decode($getLicenseId);
206
        if ($details->api_error_detected == 0 && is_array($details->page_message)) {
207
            foreach ($details->page_message as $detail) {
208
                if ($detail->product_id == $productId) {
209
                    $license = $detail->license_id;
210
                    $product = $detail->product_id;
211
                    $code = $detail->license_code;
212
                }
213
            }
214
        }
215
216
        return ['productId'=>$product, 'code'=>$code, 'licenseId'=>$license];
217
    }
218
219
    //Update the Installation status as Inactive after Licensed Domain Is Chnaged
220
    public function updateInstalledDomain($licenseCode, $productId)
221
    {
222
        $installation_id = '';
223
        $installation_ip = '';
224
        $url = $this->url;
225
        $api_key_secret = $this->api_key_secret;
226
        //Search for the Installation Id
227
        $searchInstallationId = $this->searchInstallationId($licenseCode);
228
        $details = json_decode($searchInstallationId);
229
        if ($details->api_error_detected == 0 && is_array($details->page_message)) {
230
            foreach ($details->page_message as $detail) {
231
                if ($detail->product_id == $productId) {
232
                    $installation_id = $detail->installation_id;
233
                    $installation_ip = $detail->installation_ip;
234
                }
235
            }
236
        }
237
        // deactivate The Existing Installation
238
        $updateInstallation = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=installations_edit&installation_id=$installation_id&installation_ip=$installation_ip&installation_status=0");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 206 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...
239
    }
240
241
    public function searchInstallationId($licenseCode)
242
    {
243
        $url = $this->url;
244
        $api_key_secret = $this->api_key_secret;
245
        $getInstallId = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search
246
      &search_type=installation&search_keyword=$licenseCode");
247
248
        return $getInstallId;
249
    }
250
251
    //Update  Expiration Date After Renewal
252
    public function updateExpirationDate($licenseCode, $expiryDate, $productId, $domain, $orderNo, $licenseExpiry, $supportExpiry)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 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...
253
    {
254
        $url = $this->url;
255
        $isIP = (bool) ip2long($domain);
256
        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...
257
            $requiredomain = 0;
258
            $ip = $domain;
259
            $domain = '';
260
        } else {
261
            $requiredomain = 1;
262
            $domain = $domain;
263
            $ip = '';
264
        }
265
        $api_key_secret = $this->api_key_secret;
266
        $searchLicense = $this->searchLicenseId($licenseCode, $productId);
267
        $licenseId = $searchLicense['licenseId'];
268
        $productId = $searchLicense['productId'];
269
        $code = $searchLicense['code'];
270
        $updateLicense = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=licenses_edit&product_id=$productId&license_code=$code&license_id=$licenseId&license_order_number=$orderNo&license_domain=$domain&license_ip=$ip&license_require_domain=$requiredomain&license_status=1&license_expire_date=$licenseExpiry&license_updates_date=$expiryDate&license_support_date=$supportExpiry");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 398 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...
271
    }
272
}
273