| Total Complexity | 40 |
| Total Lines | 376 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TemplateController 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 TemplateController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class TemplateController extends BaseTemplateController |
||
| 23 | { |
||
| 24 | public $template; |
||
| 25 | public $type; |
||
| 26 | public $product; |
||
| 27 | public $price; |
||
| 28 | public $subscription; |
||
| 29 | public $plan; |
||
| 30 | public $licence; |
||
| 31 | public $tax_relation; |
||
| 32 | public $tax; |
||
| 33 | public $tax_class; |
||
| 34 | public $tax_rule; |
||
| 35 | public $currency; |
||
| 36 | |||
| 37 | public function __construct() |
||
| 38 | { |
||
| 39 | $this->middleware('auth', ['except' => ['show']]); |
||
| 40 | $this->middleware('admin', ['except' => ['show']]); |
||
| 41 | |||
| 42 | $template = new Template(); |
||
| 43 | $this->template = $template; |
||
| 44 | |||
| 45 | $type = new TemplateType(); |
||
| 46 | $this->type = $type; |
||
| 47 | |||
| 48 | $product = new Product(); |
||
| 49 | $this->product = $product; |
||
| 50 | |||
| 51 | $price = new Price(); |
||
| 52 | $this->price = $price; |
||
| 53 | |||
| 54 | $subscription = new Subscription(); |
||
| 55 | $this->subscription = $subscription; |
||
| 56 | |||
| 57 | $plan = new Plan(); |
||
| 58 | $this->plan = $plan; |
||
| 59 | |||
| 60 | $licence = new Licence(); |
||
| 61 | $this->licence = $licence; |
||
| 62 | |||
| 63 | $tax_relation = new TaxProductRelation(); |
||
| 64 | $this->tax_relation = $tax_relation; |
||
| 65 | |||
| 66 | $tax = new Tax(); |
||
| 67 | $this->tax = $tax; |
||
| 68 | |||
| 69 | $tax_class = new TaxClass(); |
||
| 70 | $this->tax_class = $tax_class; |
||
| 71 | |||
| 72 | $tax_rule = new TaxOption(); |
||
| 73 | $this->tax_rule = $tax_rule; |
||
| 74 | |||
| 75 | $currency = new Currency(); |
||
| 76 | $this->currency = $currency; |
||
| 77 | $this->smtp(); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function smtp() |
||
| 81 | { |
||
| 82 | $settings = new Setting(); |
||
| 83 | $fields = $settings->find(1); |
||
| 84 | $password = ''; |
||
| 85 | // $name = ''; |
||
| 86 | if ($fields) { |
||
| 87 | $driver = $fields->driver; |
||
| 88 | $port = $fields->port; |
||
| 89 | $host = $fields->host; |
||
| 90 | $enc = $fields->encryption; |
||
| 91 | $email = $fields->email; |
||
| 92 | $password = $fields->password; |
||
| 93 | $name = $fields->company; |
||
| 94 | } |
||
| 95 | |||
| 96 | return $this->smtpConfig($driver, $port, $host, $enc, $email, $password, $name); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function index() |
||
| 100 | { |
||
| 101 | try { |
||
| 102 | return view('themes.default1.common.template.inbox'); |
||
| 103 | } catch (\Exception $ex) { |
||
| 104 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | public function getTemplates() |
||
| 109 | { |
||
| 110 | return \DataTables::of($this->template->select('id', 'name', 'type')->get()) |
||
| 111 | ->addColumn('checkbox', function ($model) { |
||
| 112 | return "<input type='checkbox' class='template_checkbox' value=".$model->id.' name=select[] id=check>'; |
||
| 113 | }) |
||
| 114 | |||
| 115 | ->addColumn('name', function ($model) { |
||
| 116 | return $model->name; |
||
| 117 | }) |
||
| 118 | ->addColumn('type', function ($model) { |
||
| 119 | return $this->type->where('id', $model->type)->first()->name; |
||
| 120 | }) |
||
| 121 | ->addColumn('action', function ($model) { |
||
| 122 | return '<a href='.url('templates/'.$model->id.'/edit')." class='btn btn-sm btn-primary'>Edit</a>"; |
||
| 123 | }) |
||
| 124 | ->rawColumns(['checkbox', 'name', 'type', 'action']) |
||
| 125 | ->make(true); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function create() |
||
| 129 | { |
||
| 130 | try { |
||
| 131 | $controller = new ProductController(); |
||
| 132 | $url = $controller->GetMyUrl(); |
||
| 133 | $i = $this->template->orderBy('created_at', 'desc')->first()->id + 1; |
||
| 134 | $cartUrl = $url.'/'.$i; |
||
| 135 | $type = $this->type->pluck('name', 'id')->toArray(); |
||
| 136 | |||
| 137 | return view('themes.default1.common.template.create', compact('type', 'cartUrl')); |
||
| 138 | } catch (\Exception $ex) { |
||
| 139 | Bugsnag::notifyException($ex); |
||
| 140 | |||
| 141 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | public function store(Request $request) |
||
| 146 | { |
||
| 147 | $this->validate($request, [ |
||
| 148 | 'name' => 'required', |
||
| 149 | 'data' => 'required', |
||
| 150 | 'type' => 'required', |
||
| 151 | ]); |
||
| 152 | |||
| 153 | try { |
||
| 154 | $this->template->fill($request->input())->save(); |
||
| 155 | |||
| 156 | return redirect()->back()->with('success', \Lang::get('message.saved-successfully')); |
||
| 157 | } catch (\Exception $ex) { |
||
| 158 | Bugsnag::notifyException($ex); |
||
| 159 | |||
| 160 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | public function edit($id) |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | public function update($id, Request $request) |
||
| 184 | { |
||
| 185 | $this->validate($request, [ |
||
| 186 | 'name' => 'required', |
||
| 187 | 'data' => 'required', |
||
| 188 | 'type' => 'required', |
||
| 189 | ]); |
||
| 190 | |||
| 191 | try { |
||
| 192 | //dd($request); |
||
| 193 | $template = $this->template->where('id', $id)->first(); |
||
| 194 | $template->fill($request->input())->save(); |
||
| 195 | |||
| 196 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 197 | } catch (\Exception $ex) { |
||
| 198 | Bugsnag::notifyException($ex); |
||
| 199 | |||
| 200 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Remove the specified resource from storage. |
||
| 206 | * |
||
| 207 | * @param int $id |
||
| 208 | * |
||
| 209 | * @return \Response |
||
| 210 | */ |
||
| 211 | public function destroy(Request $request) |
||
| 212 | { |
||
| 213 | try { |
||
| 214 | $ids = $request->input('select'); |
||
| 215 | if (!empty($ids)) { |
||
| 216 | foreach ($ids as $id) { |
||
| 217 | $template = $this->template->where('id', $id)->first(); |
||
| 218 | if ($template) { |
||
| 219 | $template->delete(); |
||
| 220 | } else { |
||
| 221 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 222 | <i class='fa fa-ban'></i> |
||
| 223 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 224 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 225 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 226 | './* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
||
| 227 | </div>'; |
||
| 228 | //echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | echo "<div class='alert alert-success alert-dismissable'> |
||
| 232 | <i class='fa fa-ban'></i> |
||
| 233 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */\Lang::get('message.success').' |
||
| 234 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 235 | './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').' |
||
| 236 | </div>'; |
||
| 237 | } else { |
||
| 238 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 239 | <i class='fa fa-ban'></i> |
||
| 240 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 241 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 242 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 243 | './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').' |
||
| 244 | </div>'; |
||
| 245 | } |
||
| 246 | } catch (\Exception $e) { |
||
| 247 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 248 | <i class='fa fa-ban'></i> |
||
| 249 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 250 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 251 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 252 | '.$e->getMessage().' |
||
| 253 | </div>'; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | public function mailing($from, $to, $data, $subject, $replace = [], $type = '', $fromname = '', $toname = '', $cc = [], $attach = []) |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | |||
| 298 | public function checkPriceWithTaxClass($productid, $currency) |
||
| 299 | { |
||
| 300 | try { |
||
| 301 | $product = $this->product->findOrFail($productid); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | |||
| 321 | |||
| 322 | public function plans($url, $id) |
||
| 336 | } |
||
| 337 | |||
| 338 | public function leastAmount($id) |
||
| 368 | } |
||
| 369 | |||
| 370 | public function leastAmountService($id) |
||
| 402 |