| Total Complexity | 48 |
| Total Lines | 301 |
| 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 |
||
| 12 | class BaseProductController extends Controller |
||
| 13 | { |
||
| 14 | public function getMyUrl() |
||
| 26 | } |
||
| 27 | |||
| 28 | |||
| 29 | /** |
||
| 30 | * Remove the specified resource from storage. |
||
| 31 | * |
||
| 32 | * @param int $id |
||
| 33 | * |
||
| 34 | * @return Response |
||
|
|
|||
| 35 | */ |
||
| 36 | public function fileDestroy(Request $request) |
||
| 69 | </div>'; |
||
| 70 | //echo \Lang::get('message.select-a-row'); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | public function getProductQtyCheck($productid) |
||
| 75 | { |
||
| 76 | try { |
||
| 77 | $check = self::checkMultiProduct($productid); |
||
| 78 | if ($check == true) { |
||
| 79 | return "<div class='col-md-4 form-group'> |
||
| 80 | <label class='required'>"./* @scrutinizer ignore-type */\Lang::get('message.quantity')."</label> |
||
| 81 | <input type='text' name='quantity' class='form-control' id='quantity' value='1'> |
||
| 82 | </div>"; |
||
| 83 | } |
||
| 84 | } catch (\Exception $ex) { |
||
| 85 | Bugsnag::notifyException($ex); |
||
| 86 | |||
| 87 | return $ex->getMessage(); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | public function getSubscriptionCheck($productid, Request $request) |
||
| 92 | { |
||
| 93 | try { |
||
| 94 | $controller = new \App\Http\Controllers\Front\CartController(); |
||
| 95 | $check = $controller->allowSubscription($productid); |
||
| 96 | $field = ''; |
||
| 97 | $price = ''; |
||
| 98 | if ($check === true) { |
||
| 99 | $plan = new Plan(); |
||
| 100 | $plans = $plan->pluck('name', 'id')->toArray(); |
||
| 101 | $script = ''; //$this->getSubscriptionCheckScript(); |
||
| 102 | $field = "<div class='col-md-4 form-group'> |
||
| 103 | <label class='required'>"./* @scrutinizer ignore-type */ \Lang::get('message.subscription').'</label> |
||
| 104 | '.\Form::select('plan', ['' => 'Select', 'Plans' => $plans], null, ['class' => 'form-control', 'id' => 'plan', 'onchange' => 'getPrice(this.value)']).' |
||
| 105 | </div>'.$script; |
||
| 106 | } else { |
||
| 107 | $userid = $request->input('user'); |
||
| 108 | $price = $controller->productCost($productid, $userid); |
||
| 109 | } |
||
| 110 | $field .= $this->getDescriptionField($productid); |
||
| 111 | $result = ['price' => $price, 'field' => $field]; |
||
| 112 | |||
| 113 | return response()->json($result); |
||
| 114 | } catch (\Exception $ex) { |
||
| 115 | Bugsnag::notifyException($ex); |
||
| 116 | |||
| 117 | return $ex->getMessage(); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | public function userDownload($uploadid, $userid, $invoice_number, $version_id = '') |
||
| 122 | { |
||
| 123 | try { |
||
| 124 | if (\Auth::user()->role != 'admin') { |
||
| 125 | if (\Auth::user()->id != $userid) { |
||
| 126 | throw new \Exception('This user has no permission for this action'); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | $user = new \App\User(); |
||
| 130 | $user = $user->findOrFail($userid); |
||
| 131 | |||
| 132 | $invoice = new \App\Model\Order\Invoice(); |
||
| 133 | $invoice = $invoice->where('number', $invoice_number)->first(); |
||
| 134 | |||
| 135 | if ($user && $invoice) { |
||
| 136 | if ($user->active == 1) { |
||
| 137 | $order = $invoice->order()->orderBy('id', 'desc')->select('product')->first(); |
||
| 138 | $product_id = $order->product; |
||
| 139 | $invoice_id = $invoice->id; |
||
| 140 | $release = $this->downloadProduct($uploadid, $userid, $invoice_id, $version_id); |
||
| 141 | if (is_array($release) && array_key_exists('type', $release)) { |
||
| 142 | $release = $release['release']; |
||
| 143 | |||
| 144 | return view('themes.default1.front.download', compact('release', 'form')); |
||
| 145 | } else { |
||
| 146 | header('Content-type: Zip'); |
||
| 147 | header('Content-Description: File Transfer'); |
||
| 148 | header('Content-Disposition: attachment; filename=Faveo.zip'); |
||
| 149 | //header("Content-type: application/zip"); |
||
| 150 | header('Content-Length: '.filesize($release)); |
||
| 151 | //ob_clean(); |
||
| 152 | flush(); |
||
| 153 | readfile("$release"); |
||
| 154 | } |
||
| 155 | } else { |
||
| 156 | return redirect('auth/login')->with('fails', \Lang::get('activate-your-account')); |
||
| 157 | } |
||
| 158 | } else { |
||
| 159 | return redirect('auth/login')->with('fails', \Lang::get('please-purcahse-a-product')); |
||
| 160 | } |
||
| 161 | } catch (\Exception $ex) { |
||
| 162 | Bugsnag::notifyException($ex); |
||
| 163 | |||
| 164 | return redirect('auth/login')->with('fails', $ex->getMessage()); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | public function getRelease($owner, $repository, $order_id, $file) |
||
| 169 | { |
||
| 170 | if ($owner && $repository) {//If the Product is downloaded from Github |
||
| 171 | $github_controller = new \App\Http\Controllers\Github\GithubController(); |
||
| 172 | $relese = $github_controller->listRepositories($owner, $repository, $order_id); |
||
| 173 | |||
| 174 | return ['release'=>$relese, 'type'=>'github']; |
||
| 175 | } elseif ($file) { |
||
| 176 | //If the Product is Downloaded from FileSystem |
||
| 177 | $fileName = $file->file; |
||
| 178 | $relese = storage_path().'/products'.'//'.$fileName; //For Local Server |
||
| 179 | // $relese = '/home/faveo/products/'.$file->file; |
||
| 180 | |||
| 181 | return $relese; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | public function getReleaseAdmin($owner, $repository, $file) |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | public function getLinkToDownload($role, $invoice, $id) |
||
| 211 | } |
||
| 212 | |||
| 213 | public function downloadProductAdmin($id) |
||
| 214 | { |
||
| 215 | try { |
||
| 216 | $product = Product::findOrFail($id); |
||
| 217 | $type = $product->type; |
||
| 218 | $owner = $product->github_owner; |
||
| 219 | $repository = $product->github_repository; |
||
| 220 | $file = ProductUpload::where('product_id', '=', $id)->select('file') |
||
| 221 | ->orderBy('created_at', 'desc') |
||
| 222 | ->first(); |
||
| 223 | |||
| 224 | if ($type == 2) { |
||
| 225 | $relese = $this->getReleaseAdmin($owner, $repository, $file); |
||
| 226 | |||
| 227 | return $relese; |
||
| 228 | } |
||
| 229 | } catch (\Exception $e) { |
||
| 230 | Bugsnag::notifyException($e); |
||
| 231 | |||
| 232 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | public function getPrice(Request $request) |
||
| 237 | { |
||
| 238 | try { |
||
| 239 | $id = $request->input('product'); |
||
| 240 | // dd($id); |
||
| 241 | $userid = $request->input('user'); |
||
| 242 | $plan = $request->input('plan'); |
||
| 243 | $controller = new \App\Http\Controllers\Front\CartController(); |
||
| 244 | $price = $controller->cost($id, $userid, $plan); |
||
| 245 | $field = $this->getProductField($id).$this->getProductQtyCheck($id); |
||
| 246 | |||
| 247 | $result = ['price' => $price, 'field' => $field]; |
||
| 248 | |||
| 249 | return response()->json($result); |
||
| 250 | } catch (\Exception $ex) { |
||
| 251 | Bugsnag::notifyException($ex); |
||
| 252 | $result = ['price' => $ex->getMessage(), 'field' => '']; |
||
| 253 | |||
| 254 | return response()->json($result); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | public function updateVersionFromGithub($productid) |
||
| 259 | { |
||
| 260 | try { |
||
| 261 | if (\Input::has('github_owner') && \Input::has('github_repository')) { |
||
| 262 | $owner = \Input::get('github_owner'); |
||
| 263 | $repo = \Input::get('github_repository'); |
||
| 264 | $product = Product::find($productid); |
||
| 265 | $github_controller = new \App\Http\Controllers\Github\GithubController(); |
||
| 266 | $version = $github_controller->findVersion($owner, $repo); |
||
| 267 | $product->version = $version; |
||
| 268 | $product->save(); |
||
| 269 | } |
||
| 270 | } catch (\Exception $ex) { |
||
| 271 | Bugsnag::notifyException($ex); |
||
| 272 | |||
| 273 | throw new \Exception($ex->getMessage()); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | public static function checkMultiProduct($productid) |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | public function getDescriptionField($productid) |
||
| 296 | { |
||
| 297 | try { |
||
| 313 | } |
||
| 314 | } |
||
| 316 |