| Total Complexity | 43 |
| Total Lines | 422 |
| 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 |
||
| 23 | class TemplateController extends BaseTemplateController |
||
| 24 | { |
||
| 25 | public $template; |
||
| 26 | public $type; |
||
| 27 | public $product; |
||
| 28 | public $price; |
||
| 29 | public $subscription; |
||
| 30 | public $plan; |
||
| 31 | public $licence; |
||
| 32 | public $tax_relation; |
||
| 33 | public $tax; |
||
| 34 | public $tax_class; |
||
| 35 | public $tax_rule; |
||
| 36 | public $currency; |
||
| 37 | |||
| 38 | public function __construct() |
||
| 39 | { |
||
| 40 | $this->middleware('auth', ['except' => ['show']]); |
||
| 41 | $this->middleware('admin', ['except' => ['show']]); |
||
| 42 | |||
| 43 | $template = new Template(); |
||
| 44 | $this->template = $template; |
||
| 45 | |||
| 46 | $type = new TemplateType(); |
||
| 47 | $this->type = $type; |
||
| 48 | |||
| 49 | $product = new Product(); |
||
| 50 | $this->product = $product; |
||
| 51 | |||
| 52 | $price = new Price(); |
||
| 53 | $this->price = $price; |
||
| 54 | |||
| 55 | $subscription = new Subscription(); |
||
| 56 | $this->subscription = $subscription; |
||
| 57 | |||
| 58 | $plan = new Plan(); |
||
| 59 | $this->plan = $plan; |
||
| 60 | |||
| 61 | $licence = new Licence(); |
||
| 62 | $this->licence = $licence; |
||
| 63 | |||
| 64 | $tax_relation = new TaxProductRelation(); |
||
| 65 | $this->tax_relation = $tax_relation; |
||
| 66 | |||
| 67 | $tax = new Tax(); |
||
| 68 | $this->tax = $tax; |
||
| 69 | |||
| 70 | $tax_class = new TaxClass(); |
||
| 71 | $this->tax_class = $tax_class; |
||
| 72 | |||
| 73 | $tax_rule = new TaxOption(); |
||
| 74 | $this->tax_rule = $tax_rule; |
||
| 75 | |||
| 76 | $currency = new Currency(); |
||
| 77 | $this->currency = $currency; |
||
| 78 | $this->smtp(); |
||
| 79 | } |
||
| 80 | |||
| 81 | public function smtp() |
||
| 82 | { |
||
| 83 | $settings = new Setting(); |
||
| 84 | $fields = $settings->find(1); |
||
| 85 | $password = ''; |
||
| 86 | // $name = ''; |
||
| 87 | if ($fields) { |
||
| 88 | $driver = $fields->driver; |
||
| 89 | $port = $fields->port; |
||
| 90 | $host = $fields->host; |
||
| 91 | $enc = $fields->encryption; |
||
| 92 | $email = $fields->email; |
||
| 93 | $password = $fields->password; |
||
| 94 | $name = $fields->company; |
||
| 95 | } |
||
| 96 | |||
| 97 | return $this->smtpConfig($driver, $port, $host, $enc, $email, $password, $name); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function smtpConfig($driver, $port, $host, $enc, $email, $password, $name) |
||
| 101 | { |
||
| 102 | Config::set('mail.driver', $driver); |
||
| 103 | Config::set('mail.password', $password); |
||
| 104 | Config::set('mail.username', $email); |
||
| 105 | Config::set('mail.encryption', $enc); |
||
| 106 | Config::set('mail.from', ['address' => $email, 'name' => $name]); |
||
| 107 | Config::set('mail.port', intval($port)); |
||
| 108 | Config::set('mail.host', $host); |
||
| 109 | |||
| 110 | return 'success'; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function index() |
||
| 114 | { |
||
| 115 | try { |
||
| 116 | return view('themes.default1.common.template.inbox'); |
||
| 117 | } catch (\Exception $ex) { |
||
| 118 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | public function getTemplates() |
||
| 123 | { |
||
| 124 | return \DataTables::of($this->template->select('id', 'name', 'type')->get()) |
||
| 125 | ->addColumn('checkbox', function ($model) { |
||
| 126 | return "<input type='checkbox' class='template_checkbox' |
||
| 127 | value=".$model->id.' name=select[] id=check>'; |
||
| 128 | }) |
||
| 129 | |||
| 130 | ->addColumn('name', function ($model) { |
||
| 131 | return $model->name; |
||
| 132 | }) |
||
| 133 | ->addColumn('type', function ($model) { |
||
| 134 | return $this->type->where('id', $model->type)->first()->name; |
||
| 135 | }) |
||
| 136 | ->addColumn('action', function ($model) { |
||
| 137 | return '<a href='.url('templates/'.$model->id.'/edit'). |
||
| 138 | " class='btn btn-sm btn-primary btn-xs'><i class='fa fa-edit' |
||
| 139 | style='color:white;'> </i> Edit</a>"; |
||
| 140 | }) |
||
| 141 | ->rawColumns(['checkbox', 'name', 'type', 'action']) |
||
| 142 | ->make(true); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function create() |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | public function store(Request $request) |
||
| 163 | { |
||
| 164 | $this->validate($request, [ |
||
| 165 | 'name' => 'required', |
||
| 166 | 'data' => 'required', |
||
| 167 | 'type' => 'required', |
||
| 168 | ]); |
||
| 169 | |||
| 170 | try { |
||
| 171 | $this->template->fill($request->input())->save(); |
||
| 172 | |||
| 173 | return redirect()->back()->with('success', \Lang::get('message.saved-successfully')); |
||
| 174 | } catch (\Exception $ex) { |
||
| 175 | Bugsnag::notifyException($ex); |
||
| 176 | |||
| 177 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | public function edit($id) |
||
| 182 | { |
||
| 183 | try { |
||
| 184 | $controller = new ProductController(); |
||
| 185 | $url = $controller->GetMyUrl(); |
||
| 186 | |||
| 187 | $i = $this->template->orderBy('created_at', 'desc')->first()->id + 1; |
||
| 188 | $cartUrl = $url.'/'.$i; |
||
| 189 | $template = $this->template->where('id', $id)->first(); |
||
| 190 | $type = $this->type->pluck('name', 'id')->toArray(); |
||
| 191 | |||
| 192 | return view('themes.default1.common.template.edit', compact('type', 'template', 'cartUrl')); |
||
| 193 | } catch (\Exception $ex) { |
||
| 194 | Bugsnag::notifyException($ex); |
||
| 195 | |||
| 196 | return redirect()->back()->with('fails', $ex->getMessage()); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | public function update($id, Request $request) |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Remove the specified resource from storage. |
||
| 223 | * |
||
| 224 | * @param int $id |
||
| 225 | * |
||
| 226 | * @return \Response |
||
| 227 | */ |
||
| 228 | public function destroy(Request $request) |
||
| 271 | </div>'; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | public function mailing($from, $to, $data, $subject, $replace = [], |
||
| 276 | $type = '', $fromname = '', $toname = '', $cc = [], $attach = []) |
||
| 277 | { |
||
| 278 | try { |
||
| 279 | $transform = []; |
||
| 280 | $page_controller = new \App\Http\Controllers\Front\PageController(); |
||
| 281 | $transform[0] = $replace; |
||
| 282 | $data = $page_controller->transform($type, $data, $transform); |
||
| 283 | |||
| 284 | $settings = \App\Model\Common\Setting::find(1); |
||
| 285 | $fromname = $settings->company; |
||
| 286 | \Mail::send('emails.mail', ['data' => $data], function ($m) use ($from, $to, $subject, $fromname, $toname, $cc, $attach) { |
||
|
|
|||
| 287 | $m->from($from, $fromname); |
||
| 288 | |||
| 289 | $m->to($to, $toname)->subject($subject); |
||
| 290 | |||
| 291 | /* if cc is need */ |
||
| 292 | if (!empty($cc)) { |
||
| 293 | foreach ($cc as $address) { |
||
| 294 | $m->cc($address['address'], $address['name']); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | /* if attachment is need */ |
||
| 299 | if (!empty($attach)) { |
||
| 300 | foreach ($attach as $file) { |
||
| 301 | $m->attach($file['path'], $options = []); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | }); |
||
| 305 | \DB::table('email_log')->insert([ |
||
| 306 | 'date' => date('Y-m-d H:i:s'), |
||
| 307 | 'from' => $from, |
||
| 308 | 'to' => $to, |
||
| 309 | 'subject' => $subject, |
||
| 310 | 'body' => $data, |
||
| 311 | 'status' => 'success', |
||
| 312 | ]); |
||
| 313 | |||
| 314 | return 'success'; |
||
| 315 | } catch (\Exception $ex) { |
||
| 316 | \DB::table('email_log')->insert([ |
||
| 317 | 'date' => date('Y-m-d H:i:s'), |
||
| 318 | 'from' => $from, |
||
| 319 | 'to' => $to, |
||
| 320 | 'subject' => $subject, |
||
| 321 | 'body' => $data, |
||
| 322 | 'status' => 'failed', |
||
| 323 | ]); |
||
| 324 | Bugsnag::notifyException($ex); |
||
| 325 | if ($ex instanceof \Swift_TransportException) { |
||
| 326 | throw new \Exception('We can not reach to this email address'); |
||
| 327 | } |
||
| 328 | |||
| 329 | throw new \Exception('mailing problem'); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | public function checkPriceWithTaxClass($productid, $currency) |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | public function plans($url, $id) |
||
| 370 | } |
||
| 371 | |||
| 372 | public function leastAmount($id) |
||
| 407 | } |
||
| 408 | } |
||
| 409 | |||
| 410 | public function leastAmountService($id) |
||
| 448 |
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.