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

BundleController::update()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 18
nop 2
dl 0
loc 24
rs 8.9137
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\BundleRequest;
7
use App\Model\Product\Product;
8
use App\Model\Product\ProductBundle;
9
use App\Model\Product\ProductBundleRelation;
10
use Illuminate\Http\Request;
11
12
class BundleController extends Controller
13
{
14
    public $product;
15
    public $bundle;
16
    public $relation;
17
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
        $this->middleware('admin');
22
23
        $product = new Product();
24
        $this->product = $product;
25
26
        $bundle = new ProductBundle();
27
        $this->bundle = $bundle;
28
29
        $relation = new ProductBundleRelation();
30
        $this->relation = $relation;
31
    }
32
33
    /**
34
     * Display a listing of the resource.
35
     *
36
     * @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...
37
     */
38
    public function index()
39
    {
40
        try {
41
            return view('themes.default1.product.bundle.index');
42
        } catch (\Exception $ex) {
43
            return redirect()->back()->with('fails', $ex->getMessage());
44
        }
45
    }
46
47
    public function getBundles()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
48
    {
49
        return \Datatable::collection($this->bundle->select('id', 'name', 'valid_from', 'valid_till', 'uses', 'maximum_uses')->get())
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 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...
50
                        ->addColumn('#', function ($model) {
51
                            return "<input type='checkbox' value=".$model->id.' name=select[] id=check>';
52
                        })
53
                        ->showColumns('name', 'valid_from', 'valid_till', 'uses', 'maximum_uses')
54
                        ->addColumn('item', function ($model) {
55
                            $name = $this->relation->where('bundle_id', $model->id)->pluck('product_id');
56
                            //dd($name);
57
                            $result = [];
58
                            foreach ($name as $key => $id) {
59
                                $result[$key] = (string) $this->product->where('id', $id)->first()->name;
60
                            }
61
                            //dd($result);
62
                            return implode(',', $result);
63
                        })
64
                        ->addColumn('action', function ($model) {
65
                            return '<a href='.url('bundles/'.$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 124 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...
66
                        })
67
                        ->searchColumns('name', 'item')
68
                        ->orderColumns('name')
69
                        ->make();
70
    }
71
72
    /**
73
     * Show the form for creating a new resource.
74
     *
75
     * @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...
76
     */
77
    public function create()
78
    {
79
        try {
80
            $products = $this->product->lists('name', 'id')->toArray();
81
82
            return view('themes.default1.product.bundle.create', compact('products'));
83
        } catch (\Exception $ex) {
84
            return redirect()->back()->with('fails', $ex->getMessage());
85
        }
86
    }
87
88
    /**
89
     * Store a newly created resource in storage.
90
     *
91
     * @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...
92
     */
93
    public function store(BundleRequest $request)
94
    {
95
        try {
96
            $this->bundle->fill($request->input())->save();
0 ignored issues
show
Bug introduced by
It seems like $request->input() targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type string; however, Illuminate\Database\Eloquent\Model::fill() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
97
            $items = $request->input('items');
98
            if (is_array($items) && !empty($items)) {
99
                foreach ($items as $item) {
100
                    $this->relation->create(['product_id' => $item, 'bundle_id' => $this->bundle->id]);
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\Model\Product\ProductBundleRelation. 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...
101
                }
102
            }
103
104
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
105
        } catch (\Exception $ex) {
106
            return redirect()->back()->with('fails', $ex->getMessage());
107
        }
108
    }
109
110
    /**
111
     * Display the specified resource.
112
     *
113
     * @param int $id
114
     *
115
     * @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...
116
     */
117
    public function show($id)
118
    {
119
        //
120
    }
121
122
    /**
123
     * Show the form for editing the specified resource.
124
     *
125
     * @param int $id
126
     *
127
     * @return \Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...p\RedirectResponse|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...
128
     */
129
    public function edit($id)
130
    {
131
        try {
132
            $bundle = $this->bundle->where('id', $id)->first();
133
            $products = $this->product->lists('name', 'id')->toArray();
134
            $relation = $this->relation->where('bundle_id', $id)->lists('product_id', 'product_id')->toArray();
135 View Code Duplication
            if ($bundle->valid_till != 0) {
136
                $date = new \DateTime($bundle->valid_till);
137
                $till = \Carbon\Carbon::createFromFormat('d/m/Y', $date->format('d/m/Y'));
138
            } else {
139
                $till = null;
140
            }
141 View Code Duplication
            if ($bundle->valid_from != 0) {
142
                $date2 = new \DateTime($bundle->valid_from);
143
                $from = \Carbon\Carbon::createFromFormat('d/m/Y', $date2->format('d/m/Y'));
144
            } else {
145
                $from = null;
146
            }
147
148
            return view('themes.default1.product.bundle.edit', compact('products', 'bundle', 'relation', 'till', 'from'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 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...
149
        } catch (\Exception $e) {
150
            return redirect()->back()->with('fails', $e->getMessage());
151
        } catch (\InvalidArgumentException $e) {
152
            if ($e->getMessage() == 'Unexpected data found.') {
153
                $till = null;
154
                $from = null;
155
156
                return view('themes.default1.product.bundle.edit', compact('products', 'bundle', 'relation', 'till', 'from'));
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...
157
            }
158
        }
159
    }
160
161
    /**
162
     * Update the specified resource in storage.
163
     *
164
     * @param int $id
165
     *
166
     * @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...
167
     */
168
    public function update($id, BundleRequest $request)
169
    {
170
        // dd($request);
171
        try {
172
            $bundle = $this->bundle->where('id', $id)->first();
173
            $bundle->fill($request->input())->save();
174
175
            $items = $request->input('items');
176
            if (is_array($items) && !empty($items)) {
177
                $delete = $this->relation->where('bundle_id', $id)->get();
178
                foreach ($delete as $del) {
179
                    $del->delete();
180
                }
181
182
                foreach ($items as $item) {
183
                    $this->relation->create(['product_id' => $item, 'bundle_id' => $bundle->id]);
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\Model\Product\ProductBundleRelation. 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...
184
                }
185
            }
186
187
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
188
        } catch (\Exception $ex) {
189
            return redirect()->back()->with('fails', $ex->getMessage());
190
        }
191
    }
192
193
    /**
194
     * Remove the specified resource from storage.
195
     *
196
     *
197
     * @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...
198
     */
199 View Code Duplication
    public function destroy(Request $request)
200
    {
201
        try {
202
            $ids = $request->input('select');
203
            if (!empty($ids)) {
204
                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...
205
                    $bundle = $this->bundle->where('id', $id)->first();
206
                    if ($bundle) {
207
                        $bundle->delete();
208
                    } else {
209
                        echo "<div class='alert alert-danger alert-dismissable'>
210
                    <i class='fa fa-ban'></i>
211
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
212
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
213
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
214
                        './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
215
                </div>';
216
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
217
                    }
218
                }
219
                echo "<div class='alert alert-success alert-dismissable'>
220
                    <i class='fa fa-ban'></i>
221
222
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
223
                    /* @scrutinizer ignore-type */\Lang::get('message.success').'
224
225
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
226
                        './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
227
                </div>';
228
            } else {
229
                echo "<div class='alert alert-danger alert-dismissable'>
230
                    <i class='fa fa-ban'></i>
231
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
232
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
233
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
234
                        './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').'
235
                </div>';
236
                //echo \Lang::get('message.select-a-row');
237
            }
238
        } catch (\Exception $e) {
239
            echo "<div class='alert alert-danger alert-dismissable'>
240
                    <i class='fa fa-ban'></i>
241
                    <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
242
                    /* @scrutinizer ignore-type */\Lang::get('message.failed').'
243
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
244
                        '.$e->getMessage().'
245
                </div>';
246
        }
247
    }
248
}
249