| Total Complexity | 41 | 
| Total Lines | 274 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like LicenseController 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 LicenseController, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 11 | class LicenseController extends Controller | ||
| 12 | { | ||
| 13 | private $api_key_secret; | ||
| 14 | private $url; | ||
| 15 | private $license; | ||
| 16 | |||
| 17 | public function __construct() | ||
| 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 | return $result; | ||
| 39 | } | ||
| 40 | |||
| 41 | /* | ||
| 42 | * Add New Product | ||
| 43 | */ | ||
| 44 | public function addNewProduct($product_name, $product_sku) | ||
| 49 | } | ||
| 50 | |||
| 51 | /* | ||
| 52 | * Add New User | ||
| 53 | */ | ||
| 54 | public function addNewUser($first_name, $last_name, $email) | ||
| 55 |     { | ||
| 56 | $url = $this->url; | ||
| 57 | $api_key_secret = $this->api_key_secret; | ||
| 58 | $addProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=clients_add | ||
| 59 | &client_fname=$first_name&client_lname=$last_name&client_email=$email&client_status=1"); | ||
| 60 | } | ||
| 61 | |||
| 62 | /* | ||
| 63 | * Edit Product | ||
| 64 | */ | ||
| 65 | public function editProduct($product_name, $product_sku) | ||
| 66 |     { | ||
| 67 | $productId = $this->searchProductId($product_sku); | ||
| 68 | $url = $this->url; | ||
| 69 | $api_key_secret = $this->api_key_secret; | ||
| 70 | $addProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=products_edit | ||
| 71 | &product_id=$productId&product_title=$product_name&product_sku=$product_sku&product_status=1"); | ||
| 72 | } | ||
| 73 | |||
| 74 | /* | ||
| 75 | * Search for product id while updating client | ||
| 76 | */ | ||
| 77 | public function searchProductId($product_sku) | ||
| 78 |     { | ||
| 79 |         try { | ||
| 80 | $productId = ''; | ||
| 81 | $url = $this->url; | ||
| 82 | $api_key_secret = $this->api_key_secret; | ||
| 83 | $getProductId = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search | ||
| 84 | &search_type=product&search_keyword=$product_sku"); | ||
| 85 | $details = json_decode($getProductId); | ||
| 86 |             if ($details->api_error_detected == 0 && is_array($details->page_message)) {//This is not true if Product_sku is updated | ||
| 87 | $productId = $details->page_message[0]->product_id; | ||
| 88 | } | ||
| 89 | |||
| 90 | return $productId; | ||
| 91 |         } catch (\Exception $ex) { | ||
| 92 | $result = [$ex->getMessage()]; | ||
| 93 | |||
| 94 |             return response()->json(compact('result'), 500); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | public function deleteProductFromAPL($product) | ||
| 99 |     { | ||
| 100 | $url = $this->url; | ||
| 101 | $api_key_secret = $this->api_key_secret; | ||
| 102 | $productId = $this->searchProductId($product->product_sku); | ||
| 103 | $productTitle = $product->name; | ||
| 104 | $productSku = $product->sku; | ||
| 105 | $delProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=products_edit | ||
| 106 | &product_id=$productId&product_title=$productTitle&product_sku=$productSku&product_status=1&delete_record=1"); | ||
| 107 | } | ||
| 108 | |||
| 109 | /* | ||
| 110 | * Edit User | ||
| 111 | */ | ||
| 112 | public function editUserInLicensing($first_name, $last_name, $email) | ||
| 113 |     { | ||
| 114 | $userId = $this->searchForUserId($email); | ||
| 115 | $url = $this->url; | ||
| 116 | $api_key_secret = $this->api_key_secret; | ||
| 117 | $addProduct = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=clients_edit&client_id=$userId | ||
| 118 | &client_fname=$first_name&client_lname=$last_name&client_email=$email&client_status=1"); | ||
| 119 | } | ||
| 120 | |||
| 121 | /* | ||
| 122 | * Search for user id while updating client | ||
| 123 | */ | ||
| 124 | public function searchForUserId($email) | ||
| 125 |     { | ||
| 126 | $userId = ''; | ||
| 127 | $url = $this->url; | ||
| 128 | $api_key_secret = $this->api_key_secret; | ||
| 129 | $getUserId = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search | ||
| 130 | &search_type=client&search_keyword=$email"); | ||
| 131 | |||
| 132 | $details = json_decode($getUserId); | ||
| 133 |         if ($details->api_error_detected == 0 && is_array($details->page_message)) {//This is not true if email is updated | ||
| 134 | $userId = $details->page_message[0]->client_id; | ||
| 135 | } | ||
| 136 | |||
| 137 | return $userId; | ||
| 138 | } | ||
| 139 | |||
| 140 | /* | ||
| 141 | * Create New License For User | ||
| 142 | */ | ||
| 143 | public function createNewLicene($orderid, $product, $user_id, | ||
| 157 | } | ||
| 158 | |||
| 159 | /* | ||
| 160 | * Edit Existing License | ||
| 161 | */ | ||
| 162 | public function updateLicensedDomain($licenseCode, $domain, $productId, $licenseExpiry, $updatesExpiry, $supportExpiry, $orderNo) | ||
| 163 |     { | ||
| 164 | $l_expiry = ''; | ||
| 165 | $s_expiry = ''; | ||
| 166 | $u_expiry = ''; | ||
| 167 |         if (strtotime($licenseExpiry) > 1) { | ||
| 168 |             $l_expiry = date('Y-m-d', strtotime($licenseExpiry)); | ||
| 169 | } | ||
| 170 |         if (strtotime($updatesExpiry) > 1) { | ||
| 171 |             $u_expiry = date('Y-m-d', strtotime($updatesExpiry)); | ||
| 172 | } | ||
| 173 |         if (strtotime($supportExpiry) > 1) { | ||
| 174 |             $s_expiry = date('Y-m-d', strtotime($supportExpiry)); | ||
| 175 | } | ||
| 176 | $url = $this->url; | ||
| 177 | $isIP = (bool) ip2long($domain); | ||
| 178 |          if ($isIP == true) { | ||
| 179 | $requiredomain = 0; | ||
| 180 | $ip = $domain; | ||
| 181 | $domain = ''; | ||
| 182 |         } else { | ||
| 183 | $requiredomain = 1; | ||
| 184 | $domain = $domain; | ||
| 185 | $ip = ''; | ||
| 186 | } | ||
| 187 | $api_key_secret = $this->api_key_secret; | ||
| 188 | $searchLicense = $this->searchLicenseId($licenseCode, $productId); | ||
| 189 | $licenseId = $searchLicense['licenseId']; | ||
| 190 | $productId = $searchLicense['productId']; | ||
| 191 | $licenseCode = $searchLicense['code']; | ||
| 192 | $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&license_limit=2"); | ||
| 193 | } | ||
| 194 | |||
| 195 | public function searchLicenseId($licenseCode, $productId) | ||
| 216 | } | ||
| 217 | |||
| 218 | //Update the Installation status as Inactive after Licensed Domain Is Chnaged | ||
| 219 | public function updateInstalledDomain($licenseCode, $productId) | ||
| 233 | } | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | } | ||
| 238 | |||
| 239 | public function searchInstallationId($licenseCode) | ||
| 240 |     { | ||
| 241 | $url = $this->url; | ||
| 242 | $api_key_secret = $this->api_key_secret; | ||
| 243 | $getInstallId = $this->postCurl($url, "api_key_secret=$api_key_secret&api_function=search | ||
| 244 | &search_type=installation&search_keyword=$licenseCode"); | ||
| 245 | |||
| 246 | return $getInstallId; | ||
| 247 | } | ||
| 248 | |||
| 249 | public function searchInstallationPath($licenseCode, $productId) | ||
| 263 | } | ||
| 264 | |||
| 265 | //Update Expiration Date After Renewal | ||
| 266 | public function updateExpirationDate($licenseCode, $expiryDate, $productId, $domain, $orderNo, $licenseExpiry, $supportExpiry) | ||
| 285 | } | ||
| 286 | } | ||
| 287 | 
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.