Completed
Push — master ( fde4fb...b78da7 )
by vijay
111:36 queued 59:13
created

ProductController::getPrice()   B

Complexity

Conditions 3
Paths 9

Size

Total Lines 29
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 3
eloc 24
c 3
b 1
f 0
nc 9
nop 1
dl 0
loc 29
rs 8.8571
1
<?php
2
3
namespace App\Http\Controllers\Product;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Payment\Currency;
7
use App\Model\Payment\Plan;
8
use App\Model\Payment\Tax;
9
use App\Model\Payment\TaxClass;
10
use App\Model\Payment\TaxProductRelation;
11
use App\Model\Product\Price;
12
use App\Model\Product\Product;
13
use App\Model\Product\ProductGroup;
14
use App\Model\Product\Subscription;
15
use App\Model\Product\Type;
16
use Illuminate\Http\Request;
17
18
class ProductController extends Controller
19
{
20
    public $product;
21
    public $price;
22
    public $type;
23
    public $subscription;
24
    public $currency;
25
    public $group;
26
    public $plan;
27
    public $tax;
28
    public $tax_relation;
29
    public $tax_class;
30
31
    public function __construct()
32
    {
33
        $this->middleware('auth');
34
        $this->middleware('admin', ['except' => ['userDownload']]);
35
36
        $product = new Product();
37
        $this->product = $product;
38
39
        $price = new Price();
40
        $this->price = $price;
41
42
        $type = new Type();
43
        $this->type = $type;
44
45
        $subscription = new Subscription();
46
        $this->subscription = $subscription;
47
48
        $currency = new Currency();
49
        $this->currency = $currency;
50
51
        $group = new ProductGroup();
52
        $this->group = $group;
53
54
        $plan = new Plan();
55
        $this->plan = $plan;
56
57
        $tax = new Tax();
58
        $this->tax = $tax;
59
60
        $tax_relation = new TaxProductRelation();
61
        $this->tax_relation = $tax_relation;
62
63
        $tax_class = new TaxClass();
64
        $this->tax_class = $tax_class;
65
    }
66
67
    /**
68
     * Display a listing of the resource.
69
     *
70
     * @return Response
71
     */
72
    public function index()
73
    {
74
        try {
75
            return view('themes.default1.product.product.index');
76
        } catch (\Exception $e) {
77
            return redirect('/')->with('fails', $e->getMessage());
78
        }
79
    }
80
81
    public function GetProducts()
82
    {
83
84
        // try {
85
        return \Datatable::collection($this->product->select('id', 'name', 'type', 'group')->where('id', '!=', 1)->get())
86
                        ->addColumn('#', function ($model) {
87
                            return "<input type='checkbox' value=".$model->id.' name=select[] id=check>';
88
                        })
89
                        ->addColumn('name', function ($model) {
90
                            return ucfirst($model->name);
91
                        })
92 View Code Duplication
                        ->addColumn('type', function ($model) {
93
                            //dd($model->type());
94
                            if ($this->type->where('id', $model->type)->first()) {
95
                                return $this->type->where('id', $model->type)->first()->name;
96
                            } else {
97
                                return 'Not available';
98
                            }
99
                        })
100
                        ->addColumn('group', function ($model) {
101
                            //dd($model->type());
102
                            if ($this->group->where('id', $model->group)->first()) {
103
                                return $this->group->where('id', $model->group)->first()->name;
104
                            } else {
105
                                return 'Not available';
106
                            }
107
                        })
108 View Code Duplication
                        ->addColumn('price', function ($model) {
109
                            if ($this->price->where('product_id', $model->id)->first()) {
110
                                return $this->price->where('product_id', $model->id)->first()->price;
111
                            } else {
112
                                return 'Not available';
113
                            }
114
                        })
115 View Code Duplication
                        ->addColumn('currency', function ($model) {
116
                            if ($this->price->where('product_id', $model->id)->first()) {
117
                                return $this->price->where('product_id', $model->id)->first()->currency;
118
                            } else {
119
                                return 'Not available';
120
                            }
121
                        })
122
                        ->addColumn('action', function ($model) {
123
                            return '<a href='.url('products/'.$model->id.'/edit')." class='btn btn-sm btn-primary'>Edit</a>";
124
                        })
125
                        ->searchColumns('name', 'email')
126
                        ->orderColumns('name', 'email')
127
                        ->make();
128
//        } catch (\Exception $e) {
129
//            return redirect()->back()->with('fails', $e->getMessage());
130
//        }
131
    }
132
133
    /**
134
     * Show the form for creating a new resource.
135
     *
136
     * @return Response
137
     */
138
    public function create()
139
    {
140
        try {
141
            /*
142
             * server url
143
             */
144
            $url = $this->GetMyUrl();
145
            $i = $this->product->orderBy('created_at', 'desc')->first()->id + 1;
146
            $cartUrl = $url.'/pricing?id='.$i;
147
            $type = $this->type->lists('name', 'id')->toArray();
148
            $subscription = $this->plan->lists('name', 'id')->toArray();
149
            $currency = $this->currency->lists('name', 'code')->toArray();
150
            $group = $this->group->lists('name', 'id')->toArray();
151
            $products = $this->product->lists('name', 'id')->toArray();
152
            $taxes = $this->tax_class->lists('name', 'id')->toArray();
153
154
            return view('themes.default1.product.product.create', compact('subscription', 'type', 'currency', 'group', 'cartUrl', 'products', 'taxes'));
155
        } catch (\Exception $e) {
156
            return redirect()->back()->with('fails', $e->getMessage());
157
        }
158
    }
159
160
    /**
161
     * Store a newly created resource in storage.
162
     *
163
     * @return Response
164
     */
165
    public function store(Request $request)
166
    {
167
        $input = $request->all();
168
169
        $v = \Validator::make($input, [
170
                    'name'         => 'required',
171
                    'type'         => 'required',
172
                    'group'        => 'required',
173
                    'subscription' => 'required',
174
                    'currency.*'   => 'required',
175
                    'price.*'      => 'required',
176
        ]);
177 View Code Duplication
        $v->sometimes(['file', 'image', 'version'], 'required', function ($input) {
178
            return $input->type == 2 && $input->github_owner == '' && $input->github_repository == '';
179
        });
180
181 View Code Duplication
        $v->sometimes(['github_owner', 'github_repository'], 'required', function ($input) {
182
            return $input->type == 2 && $input->file == '' && $input->image == '';
183
        });
184
        if ($v->fails()) {
185
            return redirect()->back()->with('errors', $v->errors());
186
            //dd();
187
        }
188
        try {
189 View Code Duplication
            if ($request->hasFile('image')) {
190
                $image = $request->file('image')->getClientOriginalName();
191
                $imagedestinationPath = 'dist/product/images';
192
                $request->file('image')->move($imagedestinationPath, $image);
193
                $this->product->image = $image;
0 ignored issues
show
Documentation introduced by
The property image does not exist on object<App\Model\Product\Product>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
194
            }
195 View Code Duplication
            if ($request->hasFile('file')) {
196
                $file = $request->file('file')->getClientOriginalName();
197
                $filedestinationPath = storage_path().'/products';
198
                $request->file('file')->move($filedestinationPath, $file);
199
                $this->product->file = $file;
0 ignored issues
show
Documentation introduced by
The property file does not exist on object<App\Model\Product\Product>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
200
            }
201
202
            //dd($request->input('currency'));
203
204
            $product = $this->product;
205
            $product->fill($request->except('image', 'file'))->save();
206
207
            $this->updateVersionFromGithub($product->id);
208
209
            $product_id = $product->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Model\Product\Product>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
210
            $subscription = $request->input('subscription');
211
            $price = $request->input('price');
212
            $sales_price = $request->input('sales_price');
213
            $currencies = $request->input('currency');
214
215 View Code Duplication
            foreach ($currencies as $key => $currency) {
0 ignored issues
show
Bug introduced by
The expression $currencies of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
216
                $this->price->create(['product_id' => $product_id, 'currency' => $currency, 'subscription' => $subscription, 'price' => $price[$key], 'sales_price' => $sales_price[$key]]);
217
            }
218
219
            //add tax class to tax_product_relation table
220
            $taxes = $request->input('tax');
221
            if ($taxes) {
222
                $this->tax_relation->create(['product_id' => $product_id, 'tax_class_id' => $taxes]);
223
            }
224
225
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
226
        } catch (\Exception $e) {
227
            return redirect()->back()->with('fails', $e->getMessage());
228
        }
229
    }
230
231
    /**
232
     * Display the specified resource.
233
     *
234
     * @param int $id
235
     *
236
     * @return Response
237
     */
238
    public function show($id)
239
    {
240
        //
241
    }
242
243
    /**
244
     * Show the form for editing the specified resource.
245
     *
246
     * @param int $id
247
     *
248
     * @return Response
249
     */
250
    public function edit($id)
251
    {
252
        try {
253
            $type = $this->type->lists('name', 'id')->toArray();
254
            $subscription = $this->plan->lists('name', 'id')->toArray();
255
            $currency = $this->currency->lists('name', 'code')->toArray();
256
            $group = $this->group->lists('name', 'id')->toArray();
257
            $products = $this->product->lists('name', 'id')->toArray();
258
            $url = $this->GetMyUrl();
259
            $cartUrl = $url.'/cart?id='.$id;
260
            $product = $this->product->where('id', $id)->first();
261
            $price = $this->price->where('product_id', $product->id);
262
            foreach ($currency as $key => $value) {
263
                if ($this->price->where('product_id', $product->id)->where('currency', $key)->first()) {
264
                    $regular[$key] = $this->price->where('product_id', $product->id)->where('currency', $key)->first()->price;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$regular was never initialized. Although not strictly required by PHP, it is generally a good practice to add $regular = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
265
                    $sales[$key] = $this->price->where('product_id', $product->id)->where('currency', $key)->first()->sales_price;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$sales was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sales = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
266
                } else {
267
                    $regular[$key] = '';
0 ignored issues
show
Bug introduced by
The variable $regular does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
268
                    $sales[$key] = '';
0 ignored issues
show
Bug introduced by
The variable $sales does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
269
                }
270
            }
271
            //dd($regular);
272
            //dd($this->tax_class);
273
            $taxes = $this->tax_class->lists('name', 'id')->toArray();
274
            //dd($taxes);
275
            $saved_taxes = $this->tax_relation->where('product_id', $id)->get();
276
//            dd($saved_taxes);
277
            return view('themes.default1.product.product.edit', compact('product', 'type', 'subscription', 'currency', 'group', 'price', 'cartUrl', 'products', 'regular', 'sales', 'taxes', 'saved_taxes'));
278
        } catch (\Exception $e) {
279
            return redirect()->back()->with('fails', $e->getMessage());
280
        }
281
    }
282
283
    /**
284
     * Update the specified resource in storage.
285
     *
286
     * @param int $id
287
     *
288
     * @return Response
289
     */
290
    public function update($id, Request $request)
291
    {
292
        $input = $request->all();
293
        //dd($input);
294
        $v = \Validator::make($input, [
295
                    'name'         => 'required',
296
                    'type'         => 'required',
297
                    'group'        => 'required',
298
                    'subscription' => 'required',
299
                    'currency.*'   => 'required',
300
                    'price.*'      => 'required',
301
        ]);
302 View Code Duplication
        $v->sometimes(['file', 'image', 'version'], 'required', function ($input) {
303
            return $input->type == 2 && $input->github_owner == '' && $input->github_repository == '';
304
        });
305
306 View Code Duplication
        $v->sometimes(['github_owner', 'github_repository'], 'required', function ($input) {
307
            return $input->type == 2 && $input->file == '' && $input->image == '';
308
        });
309
        if ($v->fails()) {
310
            return redirect()->back()->with('errors', $v->errors());
311
            //dd();
312
        }
313
        try {
314
            $product = $this->product->where('id', $id)->first();
315 View Code Duplication
            if ($request->hasFile('image')) {
316
                $image = $request->file('image')->getClientOriginalName();
317
                $imagedestinationPath = 'dist/product/images';
318
                $request->file('image')->move($imagedestinationPath, $image);
319
                $product->image = $image;
320
            }
321 View Code Duplication
            if ($request->hasFile('file')) {
322
                $file = $request->file('file')->getClientOriginalName();
323
                $filedestinationPath = storage_path().'/products';
324
                $request->file('file')->move($filedestinationPath, $file);
325
                $product->file = $file;
326
            }
327
328
            $product->fill($request->except('image', 'file'))->save();
329
            $this->updateVersionFromGithub($product->id);
330
            $product_id = $product->id;
331
            $subscription = $request->input('subscription');
332
            $cost = $request->input('price');
333
            $sales_price = $request->input('sales_price');
334
            $currencies = $request->input('currency');
335
336
            $prices = $this->price->where('product_id', $product->id)->get();
337
            foreach ($prices as $price) {
338
                $price->delete();
339
            }
340
341 View Code Duplication
            foreach ($currencies as $key => $currency) {
0 ignored issues
show
Bug introduced by
The expression $currencies of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
342
                $this->price->create(['product_id' => $product_id, 'currency' => $currency, 'subscription' => $subscription, 'price' => $cost[$key], 'sales_price' => $sales_price[$key]]);
343
            }
344
            //add tax class to tax_product_relation table
345
            $taxes = $request->input('tax');
346
            //dd($taxes);
347
            if ($taxes) {
348
                $saved_taxes = $this->tax_relation->where('product_id', $product_id)->first();
349
                if ($saved_taxes) {
350
                    $saved_taxes->tax_class_id = $taxes;
351
                    $saved_taxes->save();
352
                } else {
353
                    $this->tax_relation->create(['product_id' => $product_id, 'tax_class_id' => $taxes]);
354
                }
355
            }
356
357
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
358
        } catch (\Exception $e) {
359
            dd($e);
360
361
            return redirect()->back()->with('fails', $e->getMessage());
362
        }
363
    }
364
365
    /**
366
     * Remove the specified resource from storage.
367
     *
368
     * @param int $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
369
     *
370
     * @return Response
371
     */
372 View Code Duplication
    public function destroy(Request $request)
373
    {
374
        try {
375
            $ids = $request->input('select');
376
            if (!empty($ids)) {
377
                foreach ($ids as $id) {
0 ignored issues
show
Bug introduced by
The expression $ids of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
378
                    if ($id != 1) {
379
                        $product = $this->product->where('id', $id)->first();
380
                        if ($product) {
381
                            $product->delete();
382
                        } else {
383
                            echo "<div class='alert alert-danger alert-dismissable'>
384
                    <i class='fa fa-ban'></i>
385
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
386
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
387
                        '.\Lang::get('message.no-record').'
388
                </div>';
389
                            //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
390
                        }
391
                        echo "<div class='alert alert-success alert-dismissable'>
392
                    <i class='fa fa-ban'></i>
393
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.success').'
394
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
395
                        '.\Lang::get('message.deleted-successfully').'
396
                </div>';
397
                    } else {
398
                        echo "<div class='alert alert-danger alert-dismissable'>
399
                    <i class='fa fa-ban'></i>
400
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
401
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
402
                        '.\Lang::get('message.can-not-delete-default').'
403
                </div>';
404
                    }
405
                }
406
            } else {
407
                echo "<div class='alert alert-danger alert-dismissable'>
408
                    <i class='fa fa-ban'></i>
409
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
410
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
411
                        '.\Lang::get('message.select-a-row').'
412
                </div>';
413
                //echo \Lang::get('message.select-a-row');
414
            }
415
        } catch (\Exception $e) {
416
            echo "<div class='alert alert-danger alert-dismissable'>
417
                    <i class='fa fa-ban'></i>
418
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
419
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
420
                        '.$e->getMessage().'
421
                </div>';
422
        }
423
    }
424
425
    public function GetMyUrl()
426
    {
427
        $server = new Request();
0 ignored issues
show
Unused Code introduced by
$server is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
428
        $url = $_SERVER['REQUEST_URI'];
429
        $server = parse_url($url);
430
        $server['path'] = dirname($server['path']);
431
        $server = parse_url($server['path']);
432
        $server['path'] = dirname($server['path']);
433
434
        $server = 'http://'.$_SERVER['HTTP_HOST'].$server['path'];
435
436
        return $server;
437
    }
438
439
    public function downloadProduct($id)
440
    {
441
        try {
442
            $product = $this->product->findOrFail($id);
443
            //dd($product);
444
            $type = $product->type;
445
            $owner = $product->github_owner;
446
            $repository = $product->github_repository;
447
            $file = $product->file;
448
449
            if ($type == 2) {
450
                if ($owner && $repository) {
451
                    //dd($repository);
452
                    $github_controller = new \App\Http\Controllers\Github\GithubController();
453
                    $relese = $github_controller->listRepositories($owner, $repository);
454
455
                    return $relese;
456
                } elseif ($file) {
457
                    $file = storage_path().'/products/'.$file;
458
459
                    return \Response::download($file);
460
                }
461
            }
462
        } catch (\Exception $e) {
463
            return redirect()->back()->with('fails', $e->getMessage());
464
        }
465
    }
466
467
    public function userDownload($userid, $invoice_number)
468
    {
469
        try {
470
            $user = new \App\User();
471
            $user = $user->findOrFail($userid);
472
            $invoice = new \App\Model\Order\Invoice();
473
            $invoice = $invoice->where('number', $invoice_number)->first();
474
            if ($user && $invoice) {
475
                if ($user->active == 1) {
476
                    $invoice_item = new \App\Model\Order\InvoiceItem();
477
                    $item = $invoice_item->where('invoice_id', $invoice->id)->first();
478
                    $product_id = $this->product->where('name', $item->product_name)->first()->id;
479
                    $release = $this->downloadProduct($product_id);
480
481
//                    $form = '';
482
//                    $form .= "<form action=$release method=get name=redirect>";
483
//                    $form .= '</form>';
484
//                    $form .= "<script language='javascript'>document.redirect.submit();</script>";
485
486
                    return view('themes.default1.front.download', compact('release', 'form'));
487
                } else {
488
                    return redirect('auth/login')->with('fails', \Lang::get('activate-your-account'));
489
                }
490
            } else {
491
                return redirect('auth/login')->with('fails', \Lang::get('please-purcahse-a-product'));
492
            }
493
        } catch (Exception $ex) {
0 ignored issues
show
Bug introduced by
The class App\Http\Controllers\Product\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
494
        }
495
    }
496
497
    public function getPrice(Request $request)
498
    {
499
        try {
500
            $id = $request->input('product');
501
            $userid = $request->input('user');
502
            $user = new \App\User();
503
            $user = $user->find($userid);
504
            $currency = $user->currency;
505
            //dd($currency);
506
            $product = $this->product->findOrFail($id);
507
            $price = $product
508
                    ->price()
509
                    ->where('product_id', $id)
510
                    ->where('currency',$currency)
511
                    ->first()
512
                    ->sales_price;
513
            if (!$price) {
514
                $price = $product
515
                    ->price()
516
                    ->where('product_id', $id)
517
                    ->where('currency',$currency)
518
                    ->first()
519
                    ->price;
520
            }
521
            echo $price;
522
        } catch (\Exception $ex) {
523
            echo $ex->getMessage();
524
        }
525
    }
526
527
    public function updateVersionFromGithub($productid)
528
    {
529
        try {
530
            if (\Input::has('github_owner') && \Input::has('github_repository')) {
531
                $owner = \Input::get('github_owner');
532
                $repo = \Input::get('github_repository');
533
                $product = $this->product->find($productid);
534
                $github_controller = new \App\Http\Controllers\Github\GithubController();
535
                $version = $github_controller->findVersion($owner, $repo);
536
                $product->version = $version;
537
                $product->save();
538
            }
539
        } catch (\Exception $ex) {
540
            throw new \Exception($ex->getMessage());
541
        }
542
    }
543
}
544