| Total Complexity | 44 |
| Total Lines | 306 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like BaseProductController 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 BaseProductController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class BaseProductController extends ExtendedBaseProductController |
||
| 12 | { |
||
| 13 | public function getMyUrl() |
||
| 14 | { |
||
| 15 | $server = new Request(); |
||
| 16 | $url = $_SERVER['REQUEST_URI']; |
||
| 17 | $server = parse_url($url); |
||
| 18 | $server['path'] = dirname($server['path']); |
||
| 19 | $server = parse_url($server['path']); |
||
| 20 | $server['path'] = dirname($server['path']); |
||
| 21 | |||
| 22 | $server = 'http://'.$_SERVER['HTTP_HOST'].$server['path']; |
||
| 23 | |||
| 24 | return $server; |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Remove the specified resource from storage. |
||
| 29 | * |
||
| 30 | * @param int $id |
||
| 31 | * |
||
| 32 | * @return \Response |
||
| 33 | */ |
||
| 34 | public function fileDestroy(Request $request) |
||
| 35 | { |
||
| 36 | $ids = $request->input('select'); |
||
| 37 | if (!empty($ids)) { |
||
| 38 | foreach ($ids as $id) { |
||
| 39 | $product = ProductUpload::where('id', $id)->first(); |
||
| 40 | if ($product) { |
||
| 41 | $product->delete(); |
||
| 42 | } else { |
||
| 43 | echo "<div class='alert alert-success alert-dismissable'> |
||
| 44 | <i class='fa fa-ban'></i> |
||
| 45 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 46 | /* @scrutinizer ignore-type */\Lang::get('message.success').' |
||
| 47 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 48 | './* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
||
| 49 | </div>'; |
||
| 50 | //echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
||
| 51 | } |
||
| 52 | } |
||
| 53 | echo "<div class='alert alert-success alert-dismissable'> |
||
| 54 | <i class='fa fa-ban'></i> |
||
| 55 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 56 | /* @scrutinizer ignore-type */\Lang::get('message.success').' |
||
| 57 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 58 | './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').' |
||
| 59 | </div>'; |
||
| 60 | } else { |
||
| 61 | echo "<div class='alert alert-success alert-dismissable'> |
||
| 62 | <i class='fa fa-ban'></i> |
||
| 63 | <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> '. |
||
| 64 | /* @scrutinizer ignore-type */ \Lang::get('message.success').' |
||
| 65 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 66 | './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').' |
||
| 67 | </div>'; |
||
| 68 | //echo \Lang::get('message.select-a-row'); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | public function getProductQtyCheck($productid) |
||
| 73 | { |
||
| 74 | try { |
||
| 75 | $check = self::checkMultiProduct($productid); |
||
| 76 | if ($check == true) { |
||
| 77 | return "<div class='col-md-4 form-group'> |
||
| 78 | <label class='required'>"./* @scrutinizer ignore-type */ |
||
| 79 | \Lang::get('message.quantity')."</label> |
||
| 80 | <input type='text' name='quantity' class='form-control' id='quantity' value='1'> |
||
| 81 | </div>"; |
||
| 82 | } |
||
| 83 | } catch (\Exception $ex) { |
||
| 84 | Bugsnag::notifyException($ex); |
||
| 85 | |||
| 86 | return $ex->getMessage(); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | public function getSubscriptionCheck($productid, Request $request) |
||
| 91 | { |
||
| 92 | try { |
||
| 93 | $controller = new \App\Http\Controllers\Front\CartController(); |
||
| 94 | $check = $controller->allowSubscription($productid); |
||
| 95 | $field = ''; |
||
| 96 | $price = ''; |
||
| 97 | if ($check === true) { |
||
| 98 | $plan = new Plan(); |
||
| 99 | $plans = $plan->where('product',$productid)->pluck('name', 'id')->toArray(); |
||
| 100 | $script = ''; //$this->getSubscriptionCheckScript(); |
||
| 101 | $field = "<div class='col-md-4 form-group'> |
||
| 102 | <label class='required'>"./* @scrutinizer ignore-type */ |
||
| 103 | \Lang::get('message.subscription').'</label> |
||
| 104 | '.\Form::select('plan', ['' => 'Select', 'Plans' => $plans], null, |
||
| 105 | ['class' => 'form-control', 'id' => 'plan', 'onchange' => 'getPrice(this.value)']).' |
||
| 106 | </div>'.$script; |
||
| 107 | } else { |
||
| 108 | $userid = $request->input('user'); |
||
| 109 | $price = $controller->productCost($productid, $userid); |
||
| 110 | } |
||
| 111 | $field .= $this->getDescriptionField($productid); |
||
| 112 | $result = ['price' => $price, 'field' => $field]; |
||
| 113 | |||
| 114 | return response()->json($result); |
||
| 115 | } catch (\Exception $ex) { |
||
| 116 | Bugsnag::notifyException($ex); |
||
| 117 | |||
| 118 | return $ex->getMessage(); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | public function userDownload($uploadid, $userid, $invoice_number, $version_id = '') |
||
| 123 | { |
||
| 124 | try { |
||
| 125 | if (\Auth::user()->role != 'admin') { |
||
| 126 | if (\Auth::user()->id != $userid) { |
||
| 127 | throw new \Exception('This user has no permission for this action'); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | $user = new \App\User(); |
||
| 131 | $user = $user->findOrFail($userid); |
||
| 132 | |||
| 133 | $invoice = new \App\Model\Order\Invoice(); |
||
| 134 | $invoice = $invoice->where('number', $invoice_number)->first(); |
||
| 135 | |||
| 136 | if ($user && $invoice) { |
||
| 137 | if ($user->active == 1) { |
||
| 138 | $order = $invoice->order()->orderBy('id', 'desc')->select('product')->first(); |
||
| 139 | $product_id = $order->product; |
||
| 140 | $invoice_id = $invoice->id; |
||
| 141 | $release = $this->downloadProduct($uploadid, $userid, $invoice_id, $version_id); |
||
| 142 | if (is_array($release) && array_key_exists('type', $release)) { |
||
| 143 | $release = $release['release']; |
||
| 144 | |||
| 145 | return view('themes.default1.front.download', compact('release', 'form')); |
||
| 146 | } else { |
||
| 147 | header('Content-type: Zip'); |
||
| 148 | header('Content-Description: File Transfer'); |
||
| 149 | header('Content-Disposition: attachment; filename=Faveo.zip'); |
||
| 150 | //header("Content-type: application/zip"); |
||
| 151 | header('Content-Length: '.filesize($release)); |
||
| 152 | //ob_clean(); |
||
| 153 | flush(); |
||
| 154 | readfile("$release"); |
||
| 155 | } |
||
| 156 | } else { |
||
| 157 | return redirect('auth/login')->with('fails', \Lang::get('activate-your-account')); |
||
| 158 | } |
||
| 159 | } else { |
||
| 160 | return redirect('auth/login')->with('fails', \Lang::get('please-purcahse-a-product')); |
||
| 161 | } |
||
| 162 | } catch (\Exception $ex) { |
||
| 163 | Bugsnag::notifyException($ex); |
||
| 164 | |||
| 165 | return redirect('auth/login')->with('fails', $ex->getMessage()); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | public function getRelease($owner, $repository, $order_id, $file) |
||
| 170 | { |
||
| 171 | if ($owner && $repository) {//If the Product is downloaded from Github |
||
| 172 | $github_controller = new \App\Http\Controllers\Github\GithubController(); |
||
| 173 | $relese = $github_controller->listRepositories($owner, $repository, $order_id); |
||
| 174 | |||
| 175 | return ['release'=>$relese, 'type'=>'github']; |
||
| 176 | } elseif ($file) { |
||
| 177 | //If the Product is Downloaded from FileSystem |
||
| 178 | $fileName = $file->file; |
||
| 179 | $relese = storage_path().'/products'.'//'.$fileName; //For Local Server |
||
| 180 | // $relese = '/home/faveo/products/'.$file->file; |
||
| 181 | |||
| 182 | return $relese; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | public function getReleaseAdmin($owner, $repository, $file) |
||
| 187 | { |
||
| 188 | if ($owner && $repository) { |
||
| 189 | $github_controller = new \App\Http\Controllers\Github\GithubController(); |
||
| 190 | $relese = $github_controller->listRepositoriesAdmin($owner, $repository); |
||
| 191 | |||
| 192 | return ['release'=>$relese, 'type'=>'github']; |
||
| 193 | } elseif ($file->file) { |
||
| 194 | // $relese = storage_path().'\products'.'\\'.$file->file; |
||
| 195 | $relese = '/home/faveo/products/'.$file->file; |
||
| 196 | |||
| 197 | return $relese; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | // public function getLinkToDownload($role, $invoice, $id) |
||
| 202 | // { |
||
| 203 | // if ($role == 'user') { |
||
| 204 | // // dd($invoice); |
||
| 205 | // if ($invoice && $invoice != '') { |
||
| 206 | // return $this->downloadProductAdmin($id); |
||
| 207 | // } else { |
||
| 208 | // throw new \Exception('This user has no permission for this action'); |
||
| 209 | // } |
||
| 210 | // } |
||
| 211 | |||
| 212 | // return $this->downloadProductAdmin($id); |
||
| 213 | // } |
||
| 214 | |||
| 215 | public function downloadProductAdmin($id) |
||
| 216 | { |
||
| 217 | try { |
||
| 218 | $product = Product::findOrFail($id); |
||
| 219 | $type = $product->type; |
||
| 220 | $owner = $product->github_owner; |
||
| 221 | $repository = $product->github_repository; |
||
| 222 | $file = ProductUpload::where('product_id', '=', $id)->select('file') |
||
| 223 | ->orderBy('created_at', 'desc') |
||
| 224 | ->first(); |
||
| 225 | |||
| 226 | if ($type == 2) { |
||
| 227 | $relese = $this->getReleaseAdmin($owner, $repository, $file); |
||
| 228 | |||
| 229 | return $relese; |
||
| 230 | } |
||
| 231 | } catch (\Exception $e) { |
||
| 232 | Bugsnag::notifyException($e); |
||
| 233 | |||
| 234 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | public function getPrice(Request $request) |
||
| 239 | { |
||
| 240 | try { |
||
| 241 | $id = $request->input('product'); |
||
| 242 | // dd($id); |
||
| 243 | $userid = $request->input('user'); |
||
| 244 | $plan = $request->input('plan'); |
||
| 245 | $controller = new \App\Http\Controllers\Front\CartController(); |
||
| 246 | $price = $controller->cost($id, $userid, $plan); |
||
| 247 | $field = $this->getProductField($id).$this->getProductQtyCheck($id); |
||
| 248 | |||
| 249 | $result = ['price' => $price, 'field' => $field]; |
||
| 250 | |||
| 251 | return response()->json($result); |
||
| 252 | } catch (\Exception $ex) { |
||
| 253 | Bugsnag::notifyException($ex); |
||
| 254 | $result = ['price' => $ex->getMessage(), 'field' => '']; |
||
| 255 | |||
| 256 | return response()->json($result); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | public function updateVersionFromGithub($productid) |
||
| 261 | { |
||
| 262 | try { |
||
| 263 | if (\Input::has('github_owner') && \Input::has('github_repository')) { |
||
| 264 | $owner = \Input::get('github_owner'); |
||
| 265 | $repo = \Input::get('github_repository'); |
||
| 266 | $product = Product::find($productid); |
||
| 267 | $github_controller = new \App\Http\Controllers\Github\GithubController(); |
||
| 268 | $version = $github_controller->findVersion($owner, $repo); |
||
| 269 | $product->version = $version; |
||
| 270 | $product->save(); |
||
| 271 | } |
||
| 272 | } catch (\Exception $ex) { |
||
| 273 | Bugsnag::notifyException($ex); |
||
| 274 | |||
| 275 | throw new \Exception($ex->getMessage()); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | public static function checkMultiProduct($productid) |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | public function getDescriptionField($productid) |
||
| 298 | { |
||
| 299 | try { |
||
| 300 | $product = Product::find($productid); |
||
| 301 | $field = ''; |
||
| 302 | |||
| 303 | if ($product->retired == 1) { |
||
| 304 | $field .= "<div class='col-md-4 form-group'> |
||
| 320 |