Completed
Pull Request — development (#635)
by Ashutosh
09:42
created

LicenseController::editUserInLicensing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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
    public $api_key_secret;
14
    public $url;
15
    public $license;
16
17
    public function __construct()
18
    {
19
        $model = new ApiKey();
20
        $this->license = $model->firstOrFail();
21
22
        $this->api_key_secret = $this->license->license_api_secret;
23
        $this->url = $this->license->license_api_url;
24
    }
25
26
    public 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
87
            $details = json_decode($getProductId);
88
            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...
89
                $productId = $details->page_message[0]->product_id;
90
            }
91
92
            return $productId;
93
        } catch (\Exception $ex) {
94
            $result = [$ex->getMessage()];
95
96
            return response()->json(compact('result'), 500);
97
        }
98
    }
99
100
    /*
101
   *  Edit User
102
   */
103
    public function editUserInLicensing($first_name, $last_name, $email)
104
    {
105
        $userId = $this->searchForUserId($email);
106
        $url = $this->url;
107
        $api_key_secret = $this->api_key_secret;
108
        $addProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=clients_edit&client_id=$userId
109
      &client_fname=$first_name&client_lname=$last_name&client_email=$email&client_status=1");
110
    }
111
112
    /*
113
   *  Search for user id while updating client
114
   */
115
    public function searchForUserId($email)
116
    {
117
        $userId = '';
118
        $url = $this->url;
119
        $api_key_secret = $this->api_key_secret;
120
        $getUserId = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search
121
      &search_type=client&search_keyword=$email");
122
123
        $details = json_decode($getUserId);
124
        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...
125
            $userId = $details->page_message[0]->client_id;
126
        }
127
128
        return $userId;
129
    }
130
131
    /*
132
    *  Create New License For User
133
    */
134
    public function createNewLicene($orderid, $product, $user_id, $ends_at)
135
    {
136
        $url = $this->url;
137
        $api_key_secret = $this->api_key_secret;
138
        $sku = Product::where('id', $product)->first()->product_sku;
139
        $licenseExpirationCheck = Product::where('id', $product)->first()->perpetual_license;
140
        $expiry = ($licenseExpirationCheck == 1) ? $ends_at->toDateString() : '';
141
        $order = Order::where('id', $orderid)->first();
142
        $orderNo = $order->number;
143
        $domain = $order->domain;
144
        $email = User::where('id', $user_id)->first()->email;
145
        $userId = $this->searchForUserId($email);
146
        $productId = $this->searchProductId($sku);
147
        $addLicense = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=licenses_add&product_id=$productId&client_id=$userId
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 141 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...
148
      &license_require_domain=1&license_status=1&license_order_number=$orderNo&license_domain=$domain&license_limit=5&license_expire_date=$expiry&license_disable_ip_verification=0");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 182 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...
149
    }
150
151
    /*
152
    *  Edit Existing License
153
    */
154
    public function updateLicensedDomain($clientEmail, $domain)
155
    {
156
        $url = $this->url;
157
        $api_key_secret = $this->api_key_secret;
158
        $searchLicense = $this->searchLicenseId($clientEmail);
159
        $licenseId = $searchLicense['licenseId'];
160
        $productId = $searchLicense['productId'];
161
        $userId = $searchLicense['userId'];
162
        $updateLicense = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=licenses_edit&product_id=$productId&client_id=$userId&license_id=$licenseId&license_require_domain=1&license_status=1&license_domain=$domain");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 235 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...
163
    }
164
165
    public function searchLicenseId($email)
166
    {
167
        $url = $this->url;
168
        $api_key_secret = $this->api_key_secret;
169
        $getLicenseId = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search
170
      &search_type=license&search_keyword=$email");
171
172
        $details = json_decode($getLicenseId);
173
        if ($details->api_error_detected == 0 && is_array($details->page_message)) {
174
            $licenseId = $details->page_message[0]->license_id;
175
            $productId = $details->page_message[0]->product_id;
176
            $userId = $details->page_message[0]->client_id;
177
        }
178
179
        return ['productId'=>$productId, 'userId'=>$userId, 'licenseId'=>$licenseId];
180
    }
181
182
    //Update the Installation status as Inactive after Licensed Domain Is Chnaged
183
    public function updateInstalledDomain($email)
184
    {
185
        $installation_id = '';
186
        $installation_ip = '';
187
        $url = $this->url;
188
        $api_key_secret = $this->api_key_secret;
189
        //Search for the Installation Id
190
        $searchInstallationId = $this->searchInstallationId($email);
191
        $details = json_decode($searchInstallationId);
192
        if ($details->api_error_detected == 0 && is_array($details->page_message)) {
193
            $installation_id = $details->page_message[0]->installation_id;
194
            $installation_ip = $details->page_message[0]->installation_ip;
195
        }
196
        // delete The Existing Installation
197
        $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...
198
    }
199
200
    public function searchInstallationId($email)
201
    {
202
        $url = $this->url;
203
        $api_key_secret = $this->api_key_secret;
204
        $getInstallId = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search
205
      &search_type=installation&search_keyword=$email");
206
207
        return $getInstallId;
208
    }
209
210
    //Update Expiration Date After Renewal
211
    public function updateExpirationDate($clientEmail, $expiryDate)
212
    {
213
        $url = $this->url;
214
        $api_key_secret = $this->api_key_secret;
215
        $searchLicense = $this->searchLicenseId($clientEmail);
216
        $licenseId = $searchLicense['licenseId'];
217
        $productId = $searchLicense['productId'];
218
        $userId = $searchLicense['userId'];
219
        $updateLicense = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=licenses_edit&product_id=$productId&client_id=$userId&license_id=$licenseId&license_require_domain=1&license_status=1&license_expire_date=$expiryDate");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 244 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...
220
    }
221
}
222