BaseProductController::updateVersionFromGithub()   A
last analyzed

Complexity

Conditions 2
Paths 5

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
cc 2
nc 5
nop 3
1
<?php
2
3
namespace App\Http\Controllers\Product;
4
5
use App\Http\Controllers\License\LicensePermissionsController;
6
use App\Model\Common\Setting;
7
use App\Model\Payment\Plan;
8
use App\Model\Product\Product;
9
use App\Model\Product\ProductUpload;
10
use Bugsnag;
11
use Illuminate\Http\Request;
12
13
class BaseProductController extends ExtendedBaseProductController
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
        $server = 'http://'.$_SERVER['HTTP_HOST'].$server['path'];
24
25
        return $server;
26
    }
27
28
    /*
29
    * Get Product Qty if Product can be modified
30
     */
31
    public function getProductQtyCheck($productid, $planid)
32
    {
33
        try {
34
            $check = self::checkMultiProduct($productid);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Http\Controllers\Pro...er::checkMultiProduct() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
            /** @scrutinizer ignore-call */ 
35
            $check = self::checkMultiProduct($productid);
Loading history...
35
            if ($check == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
36
                $value = Product::find($productid)->planRelation->find($planid)->planPrice->first()->product_quantity;
0 ignored issues
show
Bug introduced by
The property planRelation does not seem to exist on App\Model\Product\Product. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
37
                $value = $value == null ? 1 : $value;
38
39
                return "<div>
40
	                        <label class='required'>"./* @scrutinizer ignore-type */
41
                            \Lang::get('message.quantity')."</label>
42
	                        <input type='text' name='quantity' class='form-control' id='quantity' value='$value'>
43
	                </div>";
44
            }
45
        } catch (\Exception $ex) {
46
            Bugsnag::notifyException($ex);
47
48
            return $ex->getMessage();
49
        }
50
    }
51
52
    /*
53
    * Check whether Product is allowed for Increasing the Quantity fromAdmin Panel
54
    * @param int $productid
55
    *
56
    * @return boolean
57
     */
58
    public function checkMultiProduct(int $productid)
59
    {
60
        $product = new Product();
61
        $product = $product->find($productid);
62
        if ($product) {
63
            if ($product->can_modify_quantity == 1) {
64
                return true;
65
            }
66
        }
67
68
        return false;
69
    }
70
71
    public function getAgentQtyCheck($productid, $planid)
72
    {
73
        try {
74
            $check = self::checkMultiAgent($productid);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Http\Controllers\Pro...ller::checkMultiAgent() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
            /** @scrutinizer ignore-call */ 
75
            $check = self::checkMultiAgent($productid);
Loading history...
75
            if ($check == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
76
                $value = Product::find($productid)->planRelation->find($planid)->planPrice->first()->no_of_agents;
0 ignored issues
show
Bug introduced by
The property planRelation does not seem to exist on App\Model\Product\Product. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
77
                $value = $value == null ? 0 : $value;
78
79
                return "<div>
80
                            <label class='required'>"./* @scrutinizer ignore-type */
81
                            \Lang::get('message.agent')."</label>
82
                            <input type='text' name='agents' class='form-control' id='agents' value='$value'>
83
                    </div>";
84
            }
85
        } catch (\Exception $ex) {
86
            Bugsnag::notifyException($ex);
87
88
            return $ex->getMessage();
89
        }
90
    }
91
92
    /*
93
    * Check whether No of the GAents can be modified or not fromAdmin Panel
94
    * @param int $productid
95
    *
96
    * @return boolean
97
     */
98
    public function checkMultiAgent(int $productid)
99
    {
100
        $product = new Product();
101
        $product = $product->find($productid);
102
        if ($product) {
103
            if ($product->can_modify_agent == 1) {
104
                return true;
105
            }
106
        }
107
108
        return false;
109
    }
110
111
    /**
112
     * Get the Subscription and Price Based on the Product Selected while generating Invoice (Admin Panel).
113
     *
114
     * @param int     $productid
115
     * @param Request $request
116
     *
117
     * @return [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
118
     */
119
    public function getSubscriptionCheck(int $productid, Request $request)
120
    {
121
        try {
122
            $controller = new \App\Http\Controllers\Front\CartController();
123
            $field = '';
124
            $price = '';
125
126
            $plan = new Plan();
127
            $plans = $plan->where('product', $productid)->pluck('name', 'id')->toArray();
128
            if (count($plans) > 0) {//If Plan Exist For A product, Display Dropdown for Plans
129
                $field = "<div>
130
                        <label class='required'>"./* @scrutinizer ignore-type */
131
                        \Lang::get('message.subscription').'</label>
132
                       '.\Form::select(
133
                            'plan',
134
                            ['' => 'Select', 'Plans' => $plans],
135
                            null,
136
                            ['class' => 'form-control required', 'id' => 'plan', 'onchange' => 'getPrice(this.value)']
137
                        ).'
138
                </div>';
139
            } else {//If No Plan Exist For A Product
140
                $userid = $request->input('user');
141
                $price = $controller->cost($productid, $userid);
142
            }
143
            $result = ['price' => $price, 'field' => $field];
144
145
            return response()->json($result);
146
        } catch (\Exception $ex) {
147
            app('log')->error($ex->getMessage());
148
            Bugsnag::notifyException($ex);
149
150
            return $ex->getMessage();
151
        }
152
    }
153
154
    public function userDownload($uploadid, $userid, $invoice_number, $version_id = '')
155
    {
156
        try {
157
            if (\Auth::user()->role != 'admin') {
158
                if (\Auth::user()->id != $userid) {
159
                    throw new \Exception('This user has no permission for this action');
160
                }
161
            }
162
            $user = new \App\User();
163
            $user = $user->findOrFail($userid);
164
165
            $invoice = new \App\Model\Order\Invoice();
166
            $invoice = $invoice->where('number', $invoice_number)->first();
167
            $this->checkSubscriptionExpiry($invoice);
168
            if ($user && $invoice) {
169
                if ($user->active == 1) {
170
                    $product_id = $invoice->order()->value('product');
171
                    $name = Product::where('id', $product_id)->value('name');
172
                    $invoice_id = $invoice->id;
173
                    $release = $this->downloadProduct($uploadid, $userid, $invoice_id, $version_id);
174
                    if (is_array($release) && array_key_exists('type', $release)) {
175
                        $release = $release['release'];
176
177
                        return view('themes.default1.front.download', compact('release'));
178
                    } else {
179
                        header('Content-type: Zip');
180
                        header('Content-Description: File Transfer');
181
                        header('Content-Disposition: attachment; filename='.$name.'.zip');
182
                        header('Content-type: application/zip');
183
                        header('Content-Length: '.filesize($release));
184
                        readfile($release);
185
                        // ob_end_clean();
186
                        // flush();
187
                    }
188
                } else {
189
                    return redirect()->back()->with('fails', \Lang::get('activate-your-account'));
190
                }
191
            } else {
192
                throw new \Exception(\Lang::get('message.no_permission_for_action'));
193
            }
194
        } catch (\Exception $ex) {
195
            app('log')->error($ex->getMessage());
196
            Bugsnag::notifyException($ex);
197
198
            return redirect()->back()->with('fails', $ex->getMessage());
199
        }
200
    }
201
202
    public function getRelease($owner, $repository, $order_id, $file)
203
    {
204
        if ($owner && $repository) {//If the Product is downloaded from Github
205
            $github_controller = new \App\Http\Controllers\Github\GithubController();
206
            $relese = $github_controller->listRepositories($owner, $repository, $order_id);
207
208
            return ['release'=>$relese, 'type'=>'github'];
209
        } elseif ($file) {
210
            //If the Product is Downloaded from FileSystem
211
            $fileName = $file->file;
212
            $path = Setting::find(1)->value('file_storage');
213
            // $relese = storage_path().'/products'.'//'.$fileName; //For Local Server
214
            //$relese = '/home/faveo/products/'.$file->file;
215
            $relese = $path.'/'.$file->file;
216
217
            return $relese;
218
        }
219
    }
220
221
    public function getReleaseAdmin($owner, $repository, $file)
222
    {
223
        if ($owner && $repository) {
224
            $github_controller = new \App\Http\Controllers\Github\GithubController();
225
            $relese = $github_controller->listRepositoriesAdmin($owner, $repository);
226
227
            return ['release'=>$relese, 'type'=>'github'];
228
        } elseif ($file->file) {
229
            // $relese = storage_path().'\products'.'\\'.$file->file;
230
            //    $relese = '/home/faveo/products/'.$file->file;
231
            $path = Setting::find(1)->value('file_storage');
232
            $relese = $path.'/'.$file->file;
233
234
            return $relese;
235
        }
236
    }
237
238
    public function downloadProductAdmin($id)
239
    {
240
        try {
241
            $product = Product::findOrFail($id);
242
            $type = $product->type;
243
            $owner = $product->github_owner;
244
            $repository = $product->github_repository;
245
            $file = ProductUpload::where('product_id', '=', $id)->select('file')
246
            ->orderBy('created_at', 'desc')
247
            ->first();
248
            $permissions = LicensePermissionsController::getPermissionsForProduct($id);
249
            if ($permissions['downloadPermission'] == 1) {
250
                $relese = $this->getReleaseAdmin($owner, $repository, $file);
251
252
                return $relese;
253
            }
254
        } catch (\Exception $e) {
255
            Bugsnag::notifyException($e);
256
257
            return redirect()->back()->with('fails', $e->getMessage());
258
        }
259
    }
260
261
    /**
262
     * Get Price For a Particular Plan Selected.
263
     *
264
     * get productid,userid,plan id as request
265
     *
266
     * @return json The final Price of the Prduct
267
     */
268
    public function getPrice(Request $request)
269
    {
270
        try {
271
            $id = $request->input('product');
272
            $userid = $request->input('user');
273
            $plan = $request->input('plan');
274
            $controller = new \App\Http\Controllers\Front\CartController();
275
            $price = $controller->cost($id, $userid, $plan);
276
            $field = $this->getProductField($id);
277
            $quantity = $this->getProductQtyCheck($id, $plan);
278
            $agents = $this->getAgentQtyCheck($id, $plan);
279
            $result = ['price' => $price, 'field' => $field, 'quantity'=>$quantity, 'agents'=>$agents];
280
281
            return response()->json($result);
282
        } catch (\Exception $ex) {
283
            Bugsnag::notifyException($ex);
284
            $result = ['price' => $ex->getMessage(), 'field' => ''];
285
286
            return response()->json($result);
287
        }
288
    }
289
290
    public function updateVersionFromGithub($productid, $github_owner, $github_repository)
291
    {
292
        try {
293
            $product = Product::find($productid)->select('version')->first();
294
            $github_controller = new \App\Http\Controllers\Github\GithubController();
295
            $version = $github_controller->findVersion($github_owner, $github_repository);
296
            $product->version = $version;
297
            $product->save();
298
        } catch (\Exception $ex) {
299
            Bugsnag::notifyException($ex);
300
301
            throw new \Exception($ex->getMessage());
302
        }
303
    }
304
305
    /**
306
     * Check Whether No. of Agents Allowed or Product Qunatity on cart.
307
     *
308
     * @author Ashutosh Pathak <[email protected]>
309
     *
310
     * @date   2019-01-11T00:18:49+0530
311
     *
312
     * @param int $productid
313
     *
314
     * @return bool
315
     */
316
    public function allowQuantityOrAgent(int $productid)
317
    {
318
        $product = Product::find($productid);
319
320
        return $product->show_agent;
321
    }
322
323
    /**
324
     * Checks Permission for Incresing the no. of Agents/Quantity in Cart.
325
     *
326
     *
327
     * @param int $productid The id of the Product added to the cart
328
     *
329
     * @return array The permissons for Agents and Quantity
330
     */
331
    public function isAllowedtoEdit(int $productid)
332
    {
333
        $product = Product::where('id', $productid)->first();
334
335
        $agentModifyPermission = $product->can_modify_agent;
336
        $quantityModifyPermission = $product->can_modify_quantity;
337
338
        return ['agent'=>$agentModifyPermission, 'quantity'=>$quantityModifyPermission];
339
    }
340
}
341