Passed
Push — master ( 041fe5...46bb46 )
by Matthijs
27:41 queued 21:16
created

ProductAmountSeriesController::create()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
1
<?php namespace App\Http\Controllers\Backend;
2
3
/**
4
 * ProductAmountSeriesController
5
 *
6
 * This is the controller of the product amount series of the shop
7
 * @author Matthijs Neijenhuijs <[email protected]>
8
 * @version 0.1
9
 */
10
11
use App\Http\Controllers\Controller;
12
use Hideyo\Ecommerce\Framework\Services\Product\Entity\ProductAmountSeriesRepository;
13
use Hideyo\Ecommerce\Framework\Services\Product\ProductFacade as ProductService;
14
use Illuminate\Http\Request;
15
use Notification;
16
use Datatables;
0 ignored issues
show
Bug introduced by
The type Datatables was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Form;
18
19
class ProductAmountSeriesController extends Controller
20
{
21
    public function __construct(ProductAmountSeriesRepository $productAmountSeries) 
22
    {
23
        $this->productAmountSeries = $productAmountSeries;
0 ignored issues
show
Bug Best Practice introduced by
The property productAmountSeries does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
24
    }
25
26
    public function index(Request $request, $productId)
27
    {
28
        $product = ProductService::find($productId);
29
        if ($request->wantsJson()) {
30
31
32
            $query = $this->productAmountSeries->getModel()->select(
33
                ['id', 'series_start', 'series_value', 'active','series_max']
34
            )->where('product_id', '=', $productId);
35
            
36
            $datatables = Datatables::of($query)
37
38
            ->addColumn('active', function ($query) {
39
                if ($query->active) {
40
                    return '<a href="#" class="change-active" data-url="/admin/html-block/change-active/'.$query->id.'"><span class="glyphicon glyphicon-ok icon-green"></span></a>';
41
                }
42
                
43
                return '<a href="#" class="change-active" data-url="/admin/html-block/change-active/'.$query->id.'"><span class="glyphicon glyphicon-remove icon-red"></span></a>';
44
            })
45
            ->addColumn('action', function ($query) use ($productId) {
46
                $deleteLink = Form::deleteajax(url()->route('product.product-amount-series.destroy', array('productId' => $productId, 'id' => $query->id)), 'Delete', '', array('class'=>'btn btn-default btn-sm btn-danger'));
47
                $links = '<a href="'.url()->route('product.product-amount-series.edit', array('productId' => $productId, 'id' => $query->id)).'" class="btn btn-default btn-sm btn-success"><i class="entypo-pencil"></i>Edit</a>  '.$deleteLink;
48
                
49
                return $links;
50
            });
51
52
            return $datatables->make(true);
53
54
        }
55
        
56
        return view('backend.product-amount-series.index')->with(array('product' => $product));
57
    }
58
59
    public function store(Request $request, $productId)
60
    {
61
        $result  = $this->productAmountSeries->create($request->all(), $productId);
62
 
63
        if (isset($result->id)) {
64
            Notification::success('The product amount series is updated.');
65
            return redirect()->route('product.product-amount-series.index', $productId);
66
        }
67
68
        if ($result) {
69
            foreach ($result->errors()->all() as $error) {
70
                \Notification::error($error);
71
            }
72
        } else {
73
            \Notification::error('amount series already exist');
74
        }
75
        
76
        return \redirect()->back()->withInput();
77
    }
78
79
    public function edit(Request $request, $productId, $id)
80
    {
81
        $product = ProductService::find($productId);
82
        $productAmountSeries = $this->productAmountSeries->find($id);
83
84
        return view('backend.product-amount-series.edit')->with(
85
            array(
86
                'productAmountSeries' => $productAmountSeries, 
87
                'product' => $product, 
88
            )
89
        );
90
    }
91
92
    public function update(Request $request, $productId, $id)
93
    {
94
        $result  = $this->productAmountSeries->updateById($request->all(), $productId, $id);
95
96
        if (!$result->id) {
0 ignored issues
show
Bug introduced by
The property id does not seem to exist on Illuminate\Validation\Validator.
Loading history...
97
            return redirect()->back()->withInput()->withErrors($result->errors()->all());
0 ignored issues
show
Bug introduced by
It seems like $result->errors()->all() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $provider of Illuminate\Http\RedirectResponse::withErrors() does only seem to accept Illuminate\Contracts\Sup...geProvider|string|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
            return redirect()->back()->withInput()->withErrors(/** @scrutinizer ignore-type */ $result->errors()->all());
Loading history...
98
        }
99
        
100
        Notification::success('The product amount series is updated.');
101
        return redirect()->route('product.product-amount-series.index', $productId);
102
    }
103
104
    public function destroy($productId, $id)
105
    {
106
        $result  = $this->productAmountSeries->destroy($id);
107
108
        if ($result) {
109
            Notification::success('The product amount series is deleted.');
110
            return redirect()->route('product.product-amount-series.index', $productId);
111
        }
112
    }
113
}
114