Test Setup Failed
Push — development ( 057645...ac5058 )
by Ashutosh
15:58
created

AddonController::update()   B

Complexity

Conditions 6
Paths 23

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 23
nop 2
dl 0
loc 27
rs 8.8657
c 0
b 0
f 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
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...e\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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();
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>";
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
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
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...e\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
71
     */
72 View Code Duplication
    public function create()
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) {
80
            return redirect()->back()->with('fails', $ex->getMessage());
81
        }
82
    }
83
84
    /**
85
     * Store a newly created resource in storage.
86
     *
87
     * @return \Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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]);
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\Model\Product\ProductAddonRelation. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
99
                    }
100
                }
101
            }
102
103
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
104
        } catch (\Exception $ex) {
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
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Contracts\Routing\ResponseFactory|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
115
     */
116
    public function show($id)
117
    {
118
        //
119
    }
120
121
    /**
122
     * Show the form for editing the specified resource.
123
     *
124
     * @param int $id
125
     *
126
     * @return \Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...e\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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();
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
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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();
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]);
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\Model\Product\ProductAddonRelation. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Contracts\Routing\ResponseFactory|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
184
     */
185 View Code Duplication
    public function destroy(Request $request)
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>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
198
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
199
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
200
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
201
                </div>';
202
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
203
                    }
204
                }
205
                echo "<div class='alert alert-success alert-dismissable'>
206
                    <i class='fa fa-ban'></i>
207
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
208
                    /* @scrutinizer ignore-type */\Lang::get('message.success').'
209
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
210
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
211
                </div>';
212
            } else {
213
                echo "<div class='alert alert-danger alert-dismissable'>
214
                    <i class='fa fa-ban'></i>
215
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
216
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
217
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
218
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
219
                </div>';
220
                //echo \Lang::get('message.select-a-row');
221
            }
222
        } catch (\Exception $e) {
223
            echo "<div class='alert alert-danger alert-dismissable'>
224
                    <i class='fa fa-ban'></i>
225
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
226
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
227
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
228
                        '.$e->getMessage().'
229
                </div>';
230
        }
231
    }
232
}
233