Test Setup Failed
Push — development ( c5582b...48e5db )
by Ashutosh
10:10
created

BaseProductController   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 44
eloc 149
dl 0
loc 254
rs 8.8798
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getReleaseAdmin() 0 11 4
A getRelease() 0 14 4
A getLinkToDownload() 0 10 4
A checkMultiProduct() 0 14 4
B userDownload() 0 43 9
A getSubscriptionCheck() 0 26 3
A downloadProductAdmin() 0 18 3
A getPrice() 0 18 2
A updateVersionFromGithub() 0 16 4
A getDescriptionField() 0 18 3
A getProductQtyCheck() 0 13 3
A getMyUrl() 0 12 1

How to fix   Complexity   

Complex Class

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
2
3
namespace App\Http\Controllers\Product;
4
5
use Illuminate\Http\Request;
6
use App\Http\Controllers\Controller;
7
use App\Model\Product\Product;
8
use App\Model\Product\ProductUpload;
9
use App\Model\Payment\Plan;
10
use Bugsnag;
11
use Spatie\Activitylog\Models\Activity;
12
13
class BaseProductController extends Controller
14
{
15
   public function getMyUrl()
16
    {
17
        $server = new Request();
18
        $url = $_SERVER['REQUEST_URI'];
19
        $server = parse_url($url);
20
        $server['path'] = dirname($server['path']);
21
        $server = parse_url($server['path']);
22
        $server['path'] = dirname($server['path']);
23
24
        $server = 'http://'.$_SERVER['HTTP_HOST'].$server['path'];
25
26
        return $server;
27
    }
28
29
	public function getProductQtyCheck($productid)
30
	    {
31
	      try {
32
	            $check = self::checkMultiProduct($productid);
33
	            if ($check == true) {
34
	                return "<div class='col-md-4 form-group'>
35
	                        <label class='required'>"./* @scrutinizer ignore-type */\Lang::get('message.quantity')."</label>
36
	                        <input type='text' name='quantity' class='form-control' id='quantity' value='1'>
37
	                </div>";
38
	            }
39
	        } catch (\Exception $ex) {
40
	            Bugsnag::notifyException($ex);
41
	            return $ex->getMessage();
42
	        }
43
	    }
44
45
    public function getSubscriptionCheck($productid, Request $request)
46
    {
47
        try {
48
            $controller = new \App\Http\Controllers\Front\CartController();
49
            $check = $controller->allowSubscription($productid);
50
            $field = '';
51
            $price = '';
52
            if ($check === true) {
53
                $plan = new Plan();
54
                $plans = $plan->pluck('name', 'id')->toArray();
55
                $script = ''; //$this->getSubscriptionCheckScript();
56
                $field = "<div class='col-md-4 form-group'>
57
                        <label class='required'>"./* @scrutinizer ignore-type */ \Lang::get('message.subscription').'</label>
58
                       '.\Form::select('plan', ['' => 'Select', 'Plans' => $plans], null, ['class' => 'form-control', 'id' => 'plan', 'onchange' => 'getPrice(this.value)']).'
59
                </div>'.$script;
60
            } else {
61
                $userid = $request->input('user');
62
                $price = $controller->productCost($productid, $userid);
63
            }
64
            $field .= $this->getDescriptionField($productid);
65
            $result = ['price' => $price, 'field' => $field];
66
67
            return response()->json($result);
68
        } catch (\Exception $ex) {
69
            Bugsnag::notifyException($ex);
70
            return $ex->getMessage();
71
        }
72
    }
73
74
    public function userDownload($uploadid, $userid, $invoice_number, $version_id = '')
75
    {
76
        try {
77
            if (\Auth::user()->role != 'admin') {
78
                if (\Auth::user()->id != $userid) {
79
                    throw new \Exception('This user has no permission for this action');
80
                }
81
            }
82
            $user = new \App\User();
83
            $user = $user->findOrFail($userid);
84
85
            $invoice = new \App\Model\Order\Invoice();
86
            $invoice = $invoice->where('number', $invoice_number)->first();
87
88
            if ($user && $invoice) {
89
                if ($user->active == 1) {
90
                    $order = $invoice->order()->orderBy('id', 'desc')->select('product')->first();
91
                    $product_id = $order->product;
92
                    $invoice_id = $invoice->id;
93
                    $release = $this->downloadProduct($uploadid, $userid, $invoice_id, $version_id);
94
                    if (is_array($release) && array_key_exists('type', $release)) {
95
                        $release = $release['release'];
96
97
                        return view('themes.default1.front.download', compact('release', 'form'));
98
                    } else {
99
                        header('Content-type: Zip');
100
                        header('Content-Description: File Transfer');
101
                        header('Content-Disposition: attachment; filename=Faveo.zip');
102
                        //header("Content-type: application/zip");
103
                        header('Content-Length: '.filesize($release));
104
                        //ob_clean();
105
                        flush();
106
                        readfile("$release");
107
                    }
108
                } else {
109
                    return redirect('auth/login')->with('fails', \Lang::get('activate-your-account'));
110
                }
111
            } else {
112
                return redirect('auth/login')->with('fails', \Lang::get('please-purcahse-a-product'));
113
            }
114
        } catch (\Exception $ex) {
115
            Bugsnag::notifyException($ex);
116
            return redirect('auth/login')->with('fails', $ex->getMessage());
117
        }
118
    }
119
120
121
    public function getRelease($owner,$repository)
122
    {
123
     if ($owner && $repository) {//If the Product is downloaded from Github
124
        $github_controller = new \App\Http\Controllers\Github\GithubController();
125
        $relese = $github_controller->listRepositories($owner, $repository, $order_id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $order_id seems to be never defined.
Loading history...
126
127
            return ['release'=>$relese, 'type'=>'github'];
128
        } elseif ($file) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $file seems to be never defined.
Loading history...
129
            //If the Product is Downloaded from FileSystem
130
            $fileName = $file->file;
131
            $relese = storage_path().'/products'.'//'.$fileName; //For Local Server
132
            // $relese = '/home/faveo/products/'.$file->file;
133
134
            return $relese;
135
        }
136
    }
137
138
    public function getReleaseAdmin($owner, $repository)
139
    {
140
     if ($owner && $repository) {
141
        $github_controller = new \App\Http\Controllers\Github\GithubController();
142
        $relese = $github_controller->listRepositoriesAdmin($owner, $repository);
143
144
        return ['release'=>$relese, 'type'=>'github'];
145
            } elseif ($file->file) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $file seems to be never defined.
Loading history...
146
        // $relese = storage_path().'\products'.'\\'.$file->file;
147
        $relese = '/home/faveo/products/'.$file->file;
148
        return $relese;
149
            }
150
    }
151
152
153
    public function getLinkToDownload($role,$invoice,$id)
154
    {
155
	  if ($role == 'user') {
156
        if ($invoice && $invoice != '') {
157
            return $this->downloadProductAdmin($id);
158
             } else {
159
            throw new \Exception('This user has no permission for this action');
160
        }
161
       }
162
       return $this->downloadProductAdmin($id);
163
	 }
164
     
165
166
    public function downloadProductAdmin($id)
167
    {
168
        try {
169
            $product = Product::findOrFail($id);
170
            $type = $product->type;
171
            $owner = $product->github_owner;
172
            $repository = $product->github_repository;
173
            $file = ProductUpload::where('product_id', '=', $id)->select('file')
174
            ->orderBy('created_at', 'desc')
175
            ->first();
176
177
            if ($type == 2) {
178
                $relese = $this->getReleaseAdmin($owner,$repository);
179
                return $relese;
180
            }
181
        } catch (\Exception $e) {
182
            Bugsnag::notifyException($e);
183
            return redirect()->back()->with('fails', $e->getMessage());
184
        }
185
    }
186
187
188
     public function getPrice(Request $request)
189
    {
190
        try {
191
            $id = $request->input('product');
192
            // dd($id);
193
            $userid = $request->input('user');
194
            $plan = $request->input('plan');
195
            $controller = new \App\Http\Controllers\Front\CartController();
196
            $price = $controller->cost($id, $userid, $plan);
197
            $field = $this->getProductField($id).$this->getProductQtyCheck($id);
198
199
            $result = ['price' => $price, 'field' => $field];
200
201
            return response()->json($result);
202
        } catch (\Exception $ex) {
203
            Bugsnag::notifyException($ex);
204
            $result = ['price' => $ex->getMessage(), 'field' => ''];
205
            return response()->json($result);
206
        }
207
    }
208
209
210
211
    public function updateVersionFromGithub($productid)
212
    {
213
        try {
214
            if (\Input::has('github_owner') && \Input::has('github_repository')) {
215
                $owner = \Input::get('github_owner');
216
                $repo = \Input::get('github_repository');
217
                $product = Product::find($productid);
218
                $github_controller = new \App\Http\Controllers\Github\GithubController();
219
                $version = $github_controller->findVersion($owner, $repo);
220
                $product->version = $version;
221
                $product->save();
222
            }
223
        } catch (\Exception $ex) {
224
            Bugsnag::notifyException($ex);
225
226
            throw new \Exception($ex->getMessage());
227
        }
228
    }
229
    
230
231
    public static function checkMultiProduct($productid)
232
    {
233
        try {
234
            $product = new Product();
235
            $product = $product->find($productid);
236
            if ($product) {
237
                // dd($product->multiple_qty == 1);
238
                if ($product->multiple_qty == 1) {
239
                    return true;
240
                }
241
            }
242
            return false;
243
        } catch (\Exception $ex) {
244
            Bugsnag::notifyException($ex);
245
        }
246
    }
247
     
248
249
	 public function getDescriptionField($productid)
250
	    {
251
	        try {
252
	            $product = Product::find($productid);
253
	            $field = '';
254
255
	            if ($product->retired == 1) {
256
	                $field .= "<div class='col-md-4 form-group'>
257
	                        <label class='required'>"./* @scrutinizer ignore-type */ \Lang::get('message.description')."</label>
258
	                        <textarea name='description' class='form-control' id='description' placeholder='Description'></textarea>
259
	                </div>";
260
	            }
261
262
	            return $field;
263
	        } catch (\Exception $ex) {
264
	            Bugsnag::notifyException($ex);
265
266
	            return $ex->getMessage();
267
	        }
268
	    }
269
270
}
271