Total Complexity | 49 |
Total Lines | 284 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 3 | Features | 0 |
Complex classes like BaseHomeController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseHomeController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class BaseHomeController extends Controller |
||
11 | { |
||
12 | public static function decryptByFaveoPrivateKey($encrypted) |
||
13 | { |
||
14 | $encrypted = json_decode($encrypted); |
||
15 | $sealed_data = $encrypted->seal; |
||
16 | $envelope = $encrypted->envelope; |
||
17 | $input = base64_decode($sealed_data); |
||
18 | $einput = base64_decode($envelope); |
||
19 | $path = storage_path('app'.DIRECTORY_SEPARATOR.'private.key'); |
||
20 | $key_content = file_get_contents($path); |
||
21 | $private_key = openssl_get_privatekey($key_content); |
||
22 | $plaintext = null; |
||
23 | openssl_open($input, $plaintext, $einput, $private_key); |
||
24 | |||
25 | return $plaintext; |
||
26 | } |
||
27 | |||
28 | public function getTotalSales() |
||
35 | } |
||
36 | |||
37 | public function checkDomain($request_url) |
||
49 | } |
||
50 | } |
||
51 | |||
52 | public function checkSerialKey($faveo_encrypted_key, $order_number) |
||
69 | } |
||
70 | } |
||
71 | |||
72 | public function verifyOrder($order_number, $serial_key) |
||
89 | } |
||
90 | } |
||
91 | |||
92 | public function index() |
||
97 | } |
||
98 | |||
99 | public function getDomain($url) |
||
100 | { |
||
101 | $pieces = parse_url($url); |
||
102 | $domain = isset($pieces['host']) ? $pieces['host'] : ''; |
||
103 | if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) { |
||
104 | return $regs['domain']; |
||
105 | } |
||
106 | |||
107 | return $domain; |
||
108 | } |
||
109 | |||
110 | public function verificationResult($order_number, $serial_key) |
||
111 | { |
||
112 | try { |
||
113 | if ($order_number && $serial_key) { |
||
114 | $order = $this->verifyOrder($order_number, $serial_key); |
||
115 | if ($order) { |
||
116 | return ['status' => 'success', 'message' => 'this-is-a-valid-request', |
||
117 | 'order_number' => $order_number, 'serial' => $serial_key, ]; |
||
118 | } else { |
||
119 | return ['status' => 'fails', 'message' => 'this-is-an-invalid-request']; |
||
120 | } |
||
121 | } else { |
||
122 | return ['status' => 'fails', 'message' => 'this-is-an-invalid-request']; |
||
123 | } |
||
124 | } catch (\Exception $ex) { |
||
125 | throw new \Exception($ex->getMessage()); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | public function getEncryptedData(Request $request) |
||
130 | { |
||
131 | $enc = $request->input('en'); |
||
132 | $result = self::decryptByFaveoPrivateKey($enc); |
||
133 | |||
134 | return response()->json($result); |
||
135 | } |
||
136 | |||
137 | public function checkUpdatesExpiry(Request $request) |
||
138 | { |
||
139 | // $v = \Validator::make($request->all(), [ |
||
140 | // 'order_number' => 'required', |
||
141 | // ]); |
||
142 | // if ($v->fails()) { |
||
143 | // $error = $v->errors(); |
||
144 | |||
145 | // return response()->json(compact('error')); |
||
146 | // } |
||
147 | |||
148 | try { |
||
149 | $order_number = $request->input('order_number'); |
||
150 | $licenseCode = $request->input('license_code'); |
||
151 | if ($order_number) { |
||
152 | $orderId = Order::where('number', 'LIKE', $order_number)->pluck('id')->first(); |
||
153 | if ($orderId) { |
||
154 | $expiryDate = Subscription::where('order_id', $orderId)->pluck('update_ends_at')->first(); |
||
155 | if (\Carbon\Carbon::now()->toDateTimeString() < $expiryDate) { |
||
156 | return ['status' => 'success', 'message' => 'allow-auto-update']; |
||
157 | } |
||
158 | } |
||
159 | } elseif ($licenseCode) { |
||
160 | $orderForLicense = Order::all()->filter(function ($order) use ($licenseCode) { |
||
161 | if ($order->serial_key == $licenseCode) { |
||
162 | return $order; |
||
163 | } |
||
164 | }); |
||
165 | if (count($orderForLicense) > 0) { |
||
166 | $expiryDate = Subscription::where('order_id', $orderForLicense->first()->id)->pluck('update_ends_at')->first(); |
||
167 | if (\Carbon\Carbon::now()->toDateTimeString() < $expiryDate) { |
||
168 | return ['status' => 'success', 'message' => 'allow-auto-update']; |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | |||
173 | return ['status' => 'fails', 'message' => 'do-not-allow-auto-update']; |
||
174 | } catch (\Exception $e) { |
||
175 | $result = ['status'=>'fails', 'error' => $e->getMessage()]; |
||
176 | |||
177 | return $result; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | public function updateLatestVersion(Request $request) |
||
182 | { |
||
183 | try { |
||
184 | $orderId = null; |
||
185 | $licenseCode = $request->input('licenseCode'); |
||
186 | $orderForLicense = Order::all()->filter(function ($order) use ($licenseCode) { |
||
187 | if ($order->serial_key == $licenseCode) { |
||
188 | return $order; |
||
189 | } |
||
190 | }); |
||
191 | if (count($orderForLicense) > 0) { |
||
192 | $latestVerison = Subscription::where('order_id', $orderForLicense->first()->id)->update(['version'=>$request->input('version')]); |
||
193 | |||
194 | return ['status' => 'success', 'message' => 'version-updated-successfully']; |
||
195 | } |
||
196 | |||
197 | return ['status' => 'fails', 'message' => 'version-not updated']; |
||
198 | } catch (\Exception $e) { |
||
199 | $result = ['status'=>'fails', 'error' => $e->getMessage()]; |
||
200 | |||
201 | return $result; |
||
202 | } |
||
203 | } |
||
204 | |||
205 | public function updateLicenseCode(Request $request) |
||
206 | { |
||
207 | try { |
||
208 | $licCode = $request->input('licenseCode'); //The license code already existing for older client |
||
209 | $lastFour = $this->getLastFourDigistsOfLicenseCode($request->input('product')); |
||
210 | $existingLicense = Order::select('id', 'client', 'product', 'serial_key')->get() |
||
211 | ->filter(function ($order) use ($licCode) { |
||
212 | return $order->serial_key == $licCode; |
||
213 | })->first(); |
||
214 | |||
215 | if ($existingLicense) {//If the license code that is sent in the request exists in billing |
||
216 | $cont = new \App\Http\Controllers\License\LicenseController(); |
||
217 | $cont->updateInstalledDomain($licCode, $existingLicense->product); //Delete the installation first for the current license before updating license so that no Faveo installation exists on the user domain/IP path |
||
218 | |||
219 | $serial_key = substr($licCode, 0, 12).$lastFour; //The new License Code |
||
220 | //Create new license in license manager with the new license code which has no. of agents in the last 4 digits. |
||
221 | $cont->createNewLicene( |
||
222 | $existingLicense->id, |
||
223 | $existingLicense->product, |
||
224 | $existingLicense->client, |
||
225 | $this->getLicenseExpiryDate($existingLicense), |
||
226 | $this->getUpdatesExpiryDate($existingLicense), |
||
227 | $this->getSupportExpiryDate($existingLicense), |
||
228 | $serial_key |
||
229 | ); |
||
230 | //Update the old license code with new one in billing. |
||
231 | $existingLicense->serial_key = \Crypt::encrypt(substr($licCode, 0, 12).$lastFour); |
||
232 | $existingLicense->save(); |
||
233 | //send the newly updated license code in response |
||
234 | $result = ['status'=>'success', 'updatedLicenseCode'=>$existingLicense->serial_key]; |
||
235 | |||
236 | return response()->json($result); |
||
237 | } |
||
238 | } catch (\Exception $ex) { |
||
239 | $result = ['status'=>'fails', 'error' => $ex->getMessage()]; |
||
240 | |||
241 | return response()->json($result); |
||
242 | } |
||
243 | } |
||
244 | |||
245 | public function getLastFourDigistsOfLicenseCode($productName) |
||
263 | } |
||
264 | } |
||
265 | |||
266 | public function getUpdatesExpiryDate($existingLicense) |
||
274 | } |
||
275 | |||
276 | public function getLicenseExpiryDate($existingLicense) |
||
284 | } |
||
285 | |||
286 | public function getSupportExpiryDate($existingLicense) |
||
294 | } |
||
295 | } |
||
296 |