Completed
Push — master ( e04365...86cad5 )
by vijay
09:15 queued 05:39
created

AddonController::GetAddons()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 24
rs 8.9713
cc 2
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Product;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\Product\AddonRequest;
7
use App\Model\Payment\Plan;
8
use App\Model\Product\Addon;
9
use App\Model\Product\Product;
10
use App\Model\Product\ProductAddonRelation;
11
use App\Model\Product\Subscription;
12
use Illuminate\Http\Request;
13
14
class AddonController extends Controller
15
{
16
    public function __construct()
17
    {
18
        $this->middleware('auth');
19
        $this->middleware('admin');
20
        $product = new Product();
21
        $this->product = $product;
0 ignored issues
show
Bug introduced by
The property product does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
        $plan = new plan();
23
        $this->plan = $plan;
0 ignored issues
show
Bug introduced by
The property plan does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
        $addon = new Addon();
25
        $this->addon = $addon;
0 ignored issues
show
Bug introduced by
The property addon does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
    }
27
28
    /**
29
     * Display a listing of the resource.
30
     *
31
     * @return Response
32
     */
33
    public function index()
34
    {
35
        try {
36
            return view('themes.default1.product.addon.index');
37
        } catch (\Exception $ex) {
38
            return redirect()->back()->with('fails', $ex->getMessage());
39
        }
40
    }
41
42
    public function GetAddons()
43
    {
44
        return \Datatable::collection($this->addon->get())
45
                        ->addColumn('#', function ($model) {
46
                            return "<input type='checkbox' value=".$model->id.' name=select[] id=check>';
47
                        })
48
                        ->showColumns('name', 'regular_price', 'selling_price')
49
                        ->addColumn('associated', function ($model) {
50
                            $relations = new ProductAddonRelation();
51
                            $relations = $relations->where('addon_id', $model->id)->get();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Model\Product\ProductAddonRelation>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
52
                            $products = [];
53
                            foreach ($relations as $key => $relation) {
54
                                $products[$key] = $this->product->where('id', $relation->product_id)->first()->name;
55
                            }
56
57
                            return implode(',', $products);
58
                        })
59
                        ->addColumn('action', function ($model) {
60
                            return '<a href='.url('addons/'.$model->id.'/edit')." class='btn btn-sm btn-primary'>Edit</a>";
61
                        })
62
                        ->searchColumns('name')
63
                        ->orderColumns('name')
64
                        ->make();
65
    }
66
67
    /**
68
     * Show the form for creating a new resource.
69
     *
70
     * @return Response
71
     */
72 View Code Duplication
    public function create()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        try {
75
            $product = $this->product->pluck('name', 'id')->toArray();
76
            $subscription = $this->plan->pluck('name', 'id')->toArray();
77
            //dd($subscription);
78
            return view('themes.default1.product.addon.create', compact('product', 'subscription'));
79
        } 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...
80
            return redirect()->back()->with('fails', $ex->getMessage());
81
        }
82
    }
83
84
    /**
85
     * Store a newly created resource in storage.
86
     *
87
     * @return Response
88
     */
89
    public function store(AddonRequest $request)
90
    {
91
        try {
92
            $this->addon->fill($request->input())->save();
93
            $products = $request->input('products');
94
            $relation = new ProductAddonRelation();
95
            if (is_array($products)) {
96
                foreach ($products as $product) {
97
                    if ($product) {
98
                        $relation->create(['addon_id' => $this->addon->id, 'product_id' => $product]);
99
                    }
100
                }
101
            }
102
103
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
104
        } 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...
105
            return redirect()->back()->with('fails', $ex->getMessage());
106
        }
107
    }
108
109
    /**
110
     * Display the specified resource.
111
     *
112
     * @param int $id
113
     *
114
     * @return Response
115
     */
116
    public function show($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
    {
118
        //
119
    }
120
121
    /**
122
     * Show the form for editing the specified resource.
123
     *
124
     * @param int $id
125
     *
126
     * @return Response
127
     */
128
    public function edit($id)
129
    {
130
        try {
131
            $product = $this->product->pluck('name', 'id')->toArray();
132
            $subscription = $this->plan->pluck('name', 'id')->toArray();
133
            $relation = new ProductAddonRelation();
134
            $relation = $relation->where('addon_id', $id)->pluck('product_id')->toArray();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Model\Product\ProductAddonRelation>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
135
            $addon = $this->addon->where('id', $id)->first();
136
137
            return view('themes.default1.product.addon.edit', compact('product', 'addon', 'subscription', 'relation'));
138
        } catch (\Exception $e) {
139
            return redirect()->back()->with('fails', $e->getMessage());
140
        }
141
    }
142
143
    /**
144
     * Update the specified resource in storage.
145
     *
146
     * @param int $id
147
     *
148
     * @return Response
149
     */
150
    public function update($id, AddonRequest $request)
151
    {
152
        try {
153
            $addon = $this->addon->where('id', $id)->first();
154
            $addon->fill($request->input())->save();
155
156
            $products = $request->input('products');
157
            $relation = new ProductAddonRelation();
158
            if (is_array($products)) {
159
                $delete = $relation->where('addon_id', $id)->get();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Model\Product\ProductAddonRelation>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
160
161
                foreach ($delete as $del) {
162
                    $del->delete();
163
                }
164
165
                foreach ($products as $product) {
166
                    if ($product) {
167
                        $relation->create(['addon_id' => $addon->id, 'product_id' => $product]);
168
                    }
169
                }
170
            }
171
172
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
173
        } catch (\Exception $e) {
174
            return redirect()->back()->with('fails', $e->getMessage());
175
        }
176
    }
177
178
    /**
179
     * Remove the specified resource from storage.
180
     *
181
     * @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...
182
     *
183
     * @return Response
184
     */
185 View Code Duplication
    public function destroy(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
    {
187
        try {
188
            $ids = $request->input('select');
189
            if (!empty($ids)) {
190
                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...
191
                    $addon = $this->addon->where('id', $id)->first();
192
                    if ($addon) {
193
                        $addon->delete();
194
                    } else {
195
                        echo "<div class='alert alert-danger alert-dismissable'>
196
                    <i class='fa fa-ban'></i>
197
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
198
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
199
                        '.\Lang::get('message.no-record').'
200
                </div>';
201
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
202
                    }
203
                }
204
                echo "<div class='alert alert-success alert-dismissable'>
205
                    <i class='fa fa-ban'></i>
206
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.success').'
207
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
208
                        '.\Lang::get('message.deleted-successfully').'
209
                </div>';
210
            } else {
211
                echo "<div class='alert alert-danger alert-dismissable'>
212
                    <i class='fa fa-ban'></i>
213
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
214
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
215
                        '.\Lang::get('message.select-a-row').'
216
                </div>';
217
                //echo \Lang::get('message.select-a-row');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
218
            }
219
        } catch (\Exception $e) {
220
            echo "<div class='alert alert-danger alert-dismissable'>
221
                    <i class='fa fa-ban'></i>
222
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
223
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
224
                        '.$e->getMessage().'
225
                </div>';
226
        }
227
    }
228
}
229