Completed
Push — development ( 55bb16...2dbf65 )
by Ashutosh
09:53
created

TemplateController::mailing()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 37
rs 8.6346
c 0
b 0
f 0
cc 7
nc 11
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace App\Http\Controllers\Common;
4
5
use App\Http\Controllers\Product\ProductController;
6
use App\Model\Common\Setting;
7
use App\Model\Common\Template;
8
use App\Model\Common\TemplateType;
9
use App\Model\licence\Licence;
10
use App\Model\Payment\Currency;
11
use App\Model\Payment\Plan;
12
use App\Model\Payment\Tax;
13
use App\Model\Payment\TaxClass;
14
use App\Model\Payment\TaxOption;
15
use App\Model\Payment\TaxProductRelation;
16
use App\Model\Product\Price;
17
use App\Model\Product\Product;
18
use App\Model\Product\Subscription;
19
use Bugsnag;
20
use Illuminate\Http\Request;
21
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)
165
    {
166
        try {
167
            $controller = new ProductController();
168
            $url = $controller->GetMyUrl();
169
170
            $i = $this->template->orderBy('created_at', 'desc')->first()->id + 1;
171
            $cartUrl = $url.'/'.$i;
172
            $template = $this->template->where('id', $id)->first();
173
            $type = $this->type->pluck('name', 'id')->toArray();
174
175
            return view('themes.default1.common.template.edit', compact('type', 'template', 'cartUrl'));
176
        } catch (\Exception $ex) {
177
            Bugsnag::notifyException($ex);
178
179
            return redirect()->back()->with('fails', $ex->getMessage());
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>&times;</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>&times;</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>&times;</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>&times;</button>
252
                        '.$e->getMessage().'
253
                </div>';
254
        }
255
    }
256
257
    public function mailing($from, $to, $data, $subject, $replace = [], $type = '', $fromname = '', $toname = '', $cc = [], $attach = [])
258
    {
259
        try {
260
            $transform = [];
261
            $page_controller = new \App\Http\Controllers\Front\PageController();
262
            $transform[0] = $replace;
263
            $data = $page_controller->transform($type, $data, $transform);
264
            $settings = \App\Model\Common\Setting::find(1);
265
            $fromname = $settings->company;
266
            \Mail::send('emails.mail', ['data' => $data], function ($m) use ($from, $to, $subject, $fromname, $toname, $cc, $attach) {
267
                $m->from($from, $fromname);
268
269
                $m->to($to, $toname)->subject($subject);
270
271
                /* if cc is need  */
272
                if (!empty($cc)) {
273
                    foreach ($cc as $address) {
274
                        $m->cc($address['address'], $address['name']);
275
                    }
276
                }
277
278
                /*  if attachment is need */
279
                if (!empty($attach)) {
280
                    foreach ($attach as $file) {
281
                        $m->attach($file['path'], $options = []);
282
                    }
283
                }
284
            });
285
286
            return 'success';
287
        } catch (\Exception $ex) {
288
            Bugsnag::notifyException($ex);
289
            if ($ex instanceof \Swift_TransportException) {
290
                throw new \Exception('We can not reach to this email address');
291
            }
292
293
            throw new \Exception('mailing problem');
294
        }
295
    }
296
297
298
    public function checkPriceWithTaxClass($productid, $currency)
299
    {
300
        try {
301
            $product = $this->product->findOrFail($productid);
302
            // dd($product);
303
            if ($product->tax_apply == 1) {
304
                $price = $this->checkTax($product->id, $currency);
305
            } else {
306
                $price = $product->price()->where('currency', $currency)->first()->sales_price;
307
                if (!$price) {
308
                    $price = $product->price()->where('currency', $currency)->first()->price;
309
                }
310
            }
311
312
            return $price;
313
        } catch (\Exception $ex) {
314
            Bugsnag::notifyException($ex);
315
316
            throw new \Exception($ex->getMessage());
317
        }
318
    }
319
320
321
322
    public function plans($url, $id)
323
    {
324
        $plan = new Plan();
325
        $plan_form = 'Free'; //No Subscription
326
        $plans = $plan->where('product', '=', $id)->pluck('name', 'id')->toArray();
327
        $plans = $this->prices($id);
328
        if (count($plans) > 0) {
329
            $plan_form = \Form::select('subscription', ['Plans' => $plans], null);
330
        }
331
        $form = \Form::open(['method' => 'get', 'url' => $url]).
332
        $plan_form.
333
        \Form::hidden('id', $id);
334
335
        return $form;
336
    }
337
338
    public function leastAmount($id)
339
    {
340
        $cost = 'Free';
341
        $symbol = '';
342
        $price = '';
343
        $plan = new Plan();
344
        $plans = $plan->where('product', $id)->get();
345
346
        $cart_controller = new \App\Http\Controllers\Front\CartController();
347
        $currency = $cart_controller->currency();
348
349
        if ($plans->count() > 0) {
350
            foreach ($plans as $value) {
351
                $days = $value->min('days');
352
                $month = round($days / 30);
353
                $price = $value->planPrice()->where('currency', $currency)->min('add_price');
354
                $price = \App\Http\Controllers\Front\CartController::rounding($price);
355
                if ($currency == 'INR') {
356
                    $symbol = '₹';
357
                } else {
358
                    $symbol = '$';
359
                }
360
                // dd($price);
361
            }
362
            $cost = "$symbol $price";
363
        } else {
364
            $cost = 'Free';
365
        }
366
367
        return $cost;
368
    }
369
370
    public function leastAmountService($id)
371
    {
372
        $cost = 'Free';
373
        $plan = new Plan();
374
        $price = '';
375
        $plans = $plan->where('product', $id)->get();
376
377
        $cart_controller = new \App\Http\Controllers\Front\CartController();
378
        $currency = $cart_controller->currency();
379
380
        if ($plans->count() > 0) {
381
            foreach ($plans as $value) {
382
                $days = $value->min('days');
383
384
                $month = round($days / 30);
385
                $price = $value->planPrice()->where('currency', $currency)->min('add_price');
386
            }
387
            if ($currency == 'INR') {
388
                $symbol = '₹';
389
            } else {
390
                $symbol = '$';
391
            }
392
            $price = "$symbol $price";
393
        } else {
394
            $price = $cart_controller->productCost($id);
395
        }
396
397
        return $price;
398
    }
399
400
401
}
402