| Total Complexity | 44 |
| Total Lines | 527 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ProductController 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 ProductController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class ProductController extends BaseProductController |
||
| 27 | { |
||
| 28 | public $product; |
||
| 29 | public $price; |
||
| 30 | public $type; |
||
| 31 | public $subscription; |
||
| 32 | public $currency; |
||
| 33 | public $group; |
||
| 34 | public $plan; |
||
| 35 | public $tax; |
||
| 36 | public $tax_relation; |
||
| 37 | public $tax_class; |
||
| 38 | public $product_upload; |
||
| 39 | |||
| 40 | public function __construct() |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Display a listing of the resource. |
||
| 84 | * |
||
| 85 | * @return \Response |
||
| 86 | */ |
||
| 87 | public function index() |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Display a listing of the resource. |
||
| 100 | * |
||
| 101 | * @return \Response |
||
| 102 | */ |
||
| 103 | public function getProducts() |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | // Save file Info in Modal popup |
||
| 168 | public function save(Request $request) |
||
| 169 | { |
||
| 170 | try { |
||
| 171 | $product_id = Product::where('name', '=', $request->input('product'))->select('id')->first(); |
||
| 172 | |||
| 173 | $this->product_upload->product_id = $product_id->id; |
||
| 174 | $this->product_upload->title = $request->input('title'); |
||
| 175 | $this->product_upload->description = $request->input('description'); |
||
| 176 | $this->product_upload->version = $request->input('version'); |
||
| 177 | |||
| 178 | if ($request->file) { |
||
| 179 | $file = $request->file('file')->getClientOriginalName(); |
||
| 180 | |||
| 181 | $destination = storage_path().'/products'; |
||
| 182 | $request->file('file')->move($destination, $file); |
||
| 183 | $this->product_upload->file = $file; |
||
| 184 | } |
||
| 185 | $this->product_upload->save(); |
||
| 186 | |||
| 187 | return redirect()->back()->with('success', \Lang::get('message.saved-successfully')); |
||
| 188 | } catch (\Exception $e) { |
||
| 189 | Bugsnag::notifyException($e); |
||
| 190 | |||
| 191 | return redirect()->with('fails', $e->getMessage()); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Show the form for creating a new resource. |
||
| 197 | * |
||
| 198 | * @return \Response |
||
| 199 | */ |
||
| 200 | public function create() |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Store a newly created resource in storage. |
||
| 229 | * |
||
| 230 | * @return \Response |
||
| 231 | */ |
||
| 232 | public function store(Request $request) |
||
| 233 | { |
||
| 234 | $input = $request->all(); |
||
| 235 | // dd($input); |
||
| 236 | $v = \Validator::make($input, [ |
||
| 237 | 'name' => 'required|unique:products,name', |
||
| 238 | 'type' => 'required', |
||
| 239 | 'group' => 'required', |
||
| 240 | 'description'=> 'required', |
||
| 241 | 'image' => 'sometimes | mimes:jpeg,jpg,png,gif | max:1000', |
||
| 242 | // 'version' => 'required', |
||
| 243 | ]); |
||
| 244 | if ($v->fails()) { |
||
| 245 | // $currency = $input['currency']; |
||
| 246 | |||
| 247 | return redirect()->back() |
||
| 248 | ->withErrors($v) |
||
| 249 | ->withInput() |
||
| 250 | ->with('currency'); |
||
| 251 | } |
||
| 252 | |||
| 253 | try { |
||
| 254 | if ($request->hasFile('image')) { |
||
| 255 | $image = $request->file('image')->getClientOriginalName(); |
||
| 256 | $imagedestinationPath = 'dist/product/images'; |
||
| 257 | $request->file('image')->move($imagedestinationPath, $image); |
||
| 258 | $this->product->image = $image; |
||
| 259 | } |
||
| 260 | |||
| 261 | $product = $this->product; |
||
| 262 | $product->fill($request->except('image', 'file'))->save(); |
||
| 263 | // Product::where('id',$product->id)->update(['name'=>$request->names]); |
||
| 264 | |||
| 265 | $product_id = $product->id; |
||
| 266 | $subscription = $request->input('subscription'); |
||
| 267 | |||
| 268 | $price = $request->input('price'); |
||
| 269 | // $price= |
||
| 270 | |||
| 271 | $sales_price = $request->input('sales_price'); |
||
| 272 | $currencies = $request->input('currency'); |
||
| 273 | $taxes = $request->input('tax'); |
||
| 274 | if ($taxes) { |
||
| 275 | foreach ($taxes as $key=>$value) { |
||
| 276 | $newtax = new TaxProductRelation(); |
||
| 277 | $newtax->product_id = $product_id; |
||
| 278 | $newtax->tax_class_id = $value; |
||
| 279 | $newtax->save(); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | return redirect()->back()->with('success', \Lang::get('message.saved-successfully')); |
||
| 284 | } catch (\Exception $e) { |
||
| 285 | Bugsnag::notifyException($e); |
||
| 286 | |||
| 287 | return redirect()->with('fails', $e->getMessage()); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Show the form for editing the specified resource. |
||
| 293 | * |
||
| 294 | * @param int $id |
||
| 295 | * |
||
| 296 | * @return \Response |
||
| 297 | */ |
||
| 298 | public function edit($id) |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Update the specified resource in storage. |
||
| 329 | * |
||
| 330 | * @param int $id |
||
| 331 | * |
||
| 332 | * @return \Response |
||
| 333 | */ |
||
| 334 | public function update($id, Request $request) |
||
| 335 | { |
||
| 336 | $input = $request->all(); |
||
| 337 | $v = \Validator::make($input, [ |
||
| 338 | 'name' => 'required', |
||
| 339 | 'type' => 'required', |
||
| 340 | 'group' => 'required', |
||
| 341 | 'image' => 'sometimes | mimes:jpeg,jpg,png,gif | max:1000', |
||
| 342 | ]); |
||
| 343 | |||
| 344 | if ($v->fails()) { |
||
| 345 | return redirect()->back()->with('errors', $v->errors()); |
||
| 346 | } |
||
| 347 | |||
| 348 | try { |
||
| 349 | $product = $this->product->where('id', $id)->first(); |
||
| 350 | if ($request->hasFile('image')) { |
||
| 351 | $image = $request->file('image')->getClientOriginalName(); |
||
| 352 | $imagedestinationPath = 'dist/product/images'; |
||
| 353 | $request->file('image')->move($imagedestinationPath, $image); |
||
| 354 | $product->image = $image; |
||
| 355 | } |
||
| 356 | if ($request->hasFile('file')) { |
||
| 357 | $file = $request->file('file')->getClientOriginalName(); |
||
| 358 | $filedestinationPath = storage_path().'/products'; |
||
| 359 | $request->file('file')->move($filedestinationPath, $file); |
||
| 360 | $product->file = $file; |
||
| 361 | } |
||
| 362 | $product->fill($request->except('image', 'file'))->save(); |
||
| 363 | $this->updateVersionFromGithub($product->id); |
||
| 364 | |||
| 365 | $product_id = $product->id; |
||
| 366 | $subscription = $request->input('subscription'); |
||
| 367 | $cost = $request->input('price'); |
||
| 368 | $sales_price = $request->input('sales_price'); |
||
| 369 | $currencies = $request->input('currency'); |
||
| 370 | |||
| 371 | //add tax class to tax_product_relation table |
||
| 372 | $taxes = $request->input('tax'); |
||
| 373 | $newTax = $this->saveTax($taxes, $product_id); |
||
| 374 | |||
| 375 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 376 | } catch (\Exception $e) { |
||
| 377 | Bugsnag::notifyException($e); |
||
| 378 | |||
| 379 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Remove the specified resource from storage. |
||
| 385 | * |
||
| 386 | * @param int $id |
||
| 387 | * |
||
| 388 | * @return \Response |
||
| 389 | */ |
||
| 390 | public function destroy(Request $request) |
||
| 445 | </div>'; |
||
| 446 | } |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Remove the specified resource from storage. |
||
| 451 | * |
||
| 452 | * @param int $id |
||
| 453 | * |
||
| 454 | * @return \Response |
||
| 455 | */ |
||
| 456 | public function fileDestroy(Request $request) |
||
| 505 | </div>'; |
||
| 506 | } |
||
| 507 | } |
||
| 508 | |||
| 509 | /* |
||
| 510 | * Download Files from Filesystem/Github |
||
| 511 | */ |
||
| 512 | public function downloadProduct($uploadid, $id, $invoice_id, $version_id = '') |
||
| 533 | } |
||
| 534 | } |
||
| 535 | |||
| 536 | |||
| 537 | |||
| 538 | public function getSubscriptionCheckScript() |
||
| 567 |