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

BundleController   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 231
Duplicated Lines 26.84 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 30
c 2
b 1
f 0
lcom 1
cbo 9
dl 62
loc 231
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A index() 0 8 2
B GetBundles() 0 24 2
A create() 8 10 2
B store() 0 16 5
A show() 0 4 1
B edit() 12 31 6
B update() 0 24 6
B destroy() 42 43 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
37
     */
38
    public function index()
39
    {
40
        try {
41
            return view('themes.default1.product.bundle.index');
42
        } catch (\Exception $e) {
43
            return redirect()->back()->with('fails', $ex->getMessage());
0 ignored issues
show
Bug introduced by
The variable $ex does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
44
        }
45
    }
46
47
    public function GetBundles()
48
    {
49
        return \Datatable::collection($this->bundle->select('id', 'name', 'valid_from', 'valid_till', 'uses', 'maximum_uses')->get())
0 ignored issues
show
Documentation Bug introduced by
The method select does not exist on object<App\Model\Product\ProductBundle>? 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...
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');
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Model\Product\ProductBundleRelation>? 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...
56
                            //dd($name);
57
                            $result = [];
58
                            foreach ($name as $key => $id) {
59
                                $result[$key] = (string) $this->product->where('id', $id)->first()->name;
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Model\Product\Product>? 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...
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>";
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
76
     */
77 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...
78
    {
79
        try {
80
            $products = $this->product->lists('name', 'id')->toArray();
0 ignored issues
show
Documentation Bug introduced by
The method lists does not exist on object<App\Model\Product\Product>? 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...
81
82
            return view('themes.default1.product.bundle.create', compact('products'));
83
        } catch (\Exception $e) {
84
            return redirect()->back()->with('fails', $ex->getMessage());
0 ignored issues
show
Bug introduced by
The variable $ex does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
85
        }
86
    }
87
88
    /**
89
     * Store a newly created resource in storage.
90
     *
91
     * @return Response
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\Request::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
Documentation introduced by
The property id does not exist on object<App\Model\Product\ProductBundle>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
101
                }
102
            }
103
104
            return redirect()->back()->with('success', \Lang::get('message.saved-successfully'));
105
        } catch (Exception $e) {
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...
106
            return redirect()->back()->with('fails', $ex->getMessage());
0 ignored issues
show
Bug introduced by
The variable $ex does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
107
        }
108
    }
109
110
    /**
111
     * Display the specified resource.
112
     *
113
     * @param int $id
114
     *
115
     * @return Response
116
     */
117
    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...
118
    {
119
        //
120
    }
121
122
    /**
123
     * Show the form for editing the specified resource.
124
     *
125
     * @param int $id
126
     *
127
     * @return Response
128
     */
129
    public function edit($id)
130
    {
131
        try {
132
            $bundle = $this->bundle->where('id', $id)->first();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Model\Product\ProductBundle>? 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...
133
            $products = $this->product->lists('name', 'id')->toArray();
0 ignored issues
show
Documentation Bug introduced by
The method lists does not exist on object<App\Model\Product\Product>? 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...
134
            $relation = $this->relation->where('bundle_id', $id)->lists('product_id', 'product_id')->toArray();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Model\Product\ProductBundleRelation>? 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 View Code Duplication
            if ($bundle->valid_till != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
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'));
149
        } catch (Exception $e) {
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...
150
            return redirect()->back()->with('fails', $ex->getMessage());
0 ignored issues
show
Bug introduced by
The variable $ex does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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'));
157
            }
158
        }
159
    }
160
161
    /**
162
     * Update the specified resource in storage.
163
     *
164
     * @param int $id
165
     *
166
     * @return Response
167
     */
168
    public function update($id, BundleRequest $request)
169
    {
170
        // dd($request);
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...
171
        try {
172
            $bundle = $this->bundle->where('id', $id)->first();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Model\Product\ProductBundle>? 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...
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();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Model\Product\ProductBundleRelation>? 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...
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]);
184
                }
185
            }
186
187
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
188
        } catch (Exception $e) {
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...
189
            return redirect()->back()->with('fails', $ex->getMessage());
0 ignored issues
show
Bug introduced by
The variable $ex does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
190
        }
191
    }
192
193
    /**
194
     * Remove the specified resource from storage.
195
     *
196
     *
197
     * @return Response
198
     */
199 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...
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();
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\Model\Product\ProductBundle>? 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...
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>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
212
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
213
                        '.\Lang::get('message.no-record').'
214
                </div>';
215
                        //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...
216
                    }
217
                }
218
                echo "<div class='alert alert-success alert-dismissable'>
219
                    <i class='fa fa-ban'></i>
220
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.success').'
221
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
222
                        '.\Lang::get('message.deleted-successfully').'
223
                </div>';
224
            } else {
225
                echo "<div class='alert alert-danger alert-dismissable'>
226
                    <i class='fa fa-ban'></i>
227
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
228
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
229
                        '.\Lang::get('message.select-a-row').'
230
                </div>';
231
                //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...
232
            }
233
        } catch (\Exception $e) {
234
            echo "<div class='alert alert-danger alert-dismissable'>
235
                    <i class='fa fa-ban'></i>
236
                    <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
237
                    <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
238
                        '.$e->getMessage().'
239
                </div>';
240
        }
241
    }
242
}
243