Passed
Push — develop ( 6302a9...3db2fb )
by Septianata
04:45
created

DenominationController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 39
dl 0
loc 146
ccs 42
cts 42
cp 1
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A destroyImage() 0 10 1
A destroy() 0 8 1
A store() 0 14 2
A update() 0 14 2
A datatable() 0 5 1
A destroyMultiple() 0 8 1
A index() 0 3 1
A create() 0 3 1
A edit() 0 3 1
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\Denomination\StoreRequest;
7
use App\Http\Requests\Denomination\UpdateRequest;
8
use App\Http\Resources\DataTables\DenominationResource;
9
use App\Models\Denomination;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\Storage;
12
use Yajra\DataTables\Facades\DataTables;
13
14
class DenominationController extends Controller
15
{
16
    /**
17
     * Display a listing of the resource.
18
     *
19
     * @return \Illuminate\Contracts\Support\Renderable
20
     */
21 1
    public function index()
22
    {
23 1
        return view('admin.denomination.index');
24
    }
25
26
    /**
27
     * Return datatable server side response.
28
     *
29
     * @return \Illuminate\Http\JsonResponse
30
     */
31 1
    public function datatable()
32
    {
33 1
        return DataTables::eloquent(Denomination::query())
34 1
            ->setTransformer(fn ($model) => DenominationResource::make($model)->resolve())
35 1
            ->toJson();
36
    }
37
38
    /**
39
     * Show the form for creating a new resource.
40
     *
41
     * @return \Illuminate\Contracts\Support\Renderable
42
     */
43 1
    public function create()
44
    {
45 1
        return view('admin.denomination.create');
46
    }
47
48
    /**
49
     * Store a newly created resource in storage.
50
     *
51
     * @param  \App\Http\Requests\Denomination\StoreRequest  $request
52
     * @return \Illuminate\Http\RedirectResponse
53
     */
54 1
    public function store(StoreRequest $request)
55
    {
56 1
        $denomination = Denomination::make($request->all());
57
58 1
        if ($filename = $request->storeImage()) {
59 1
            $denomination->image = $filename;
60
        }
61
62 1
        $denomination->save();
63
64 1
        return redirect()->route('admin.denomination.index')->with([
65
            'alert' => [
66 1
                'type' => 'alert-success',
67 1
                'message' => trans('The :resource was created!', ['resource' => trans('admin-lang.denomination')]),
68
            ],
69
        ]);
70
    }
71
72
    /**
73
     * Show the form for editing the specified resource.
74
     *
75
     * @param  \App\Models\Denomination  $denomination
76
     * @return \Illuminate\Contracts\Support\Renderable
77
     */
78 1
    public function edit(Denomination $denomination)
79
    {
80 1
        return view('admin.denomination.edit', compact('denomination'));
81
    }
82
83
    /**
84
     * Update the specified resource in storage.
85
     *
86
     * @param  \App\Http\Requests\Denomination\UpdateRequest  $request
87
     * @param  \App\Models\Denomination  $denomination
88
     * @return \Illuminate\Http\RedirectResponse
89
     */
90 1
    public function update(UpdateRequest $request, Denomination $denomination)
91
    {
92 1
        $denomination->fill($request->all());
93
94 1
        if ($filename = $request->updateImage()) {
95 1
            $denomination->image = $filename;
96
        }
97
98 1
        $denomination->save();
99
100 1
        return redirect()->route('admin.denomination.index')->with([
101
            'alert' => [
102 1
                'type' => 'alert-success',
103 1
                'message' => trans('The :resource was updated!', ['resource' => trans('admin-lang.denomination')]),
104
            ],
105
        ]);
106
    }
107
108
    /**
109
     * Remove the specified resource from storage.
110
     *
111
     * @param  \App\Models\Denomination  $denomination
112
     * @return \Illuminate\Http\RedirectResponse
113
     */
114 1
    public function destroy(Denomination $denomination)
115
    {
116 1
        $denomination->delete();
117
118 1
        return redirect()->route('admin.denomination.index')->with([
119
            'alert' => [
120 1
                'type' => 'alert-success',
121 1
                'message' => trans('The :resource was deleted!', ['resource' => trans('admin-lang.denomination')]),
122
            ],
123
        ]);
124
    }
125
126
    /**
127
     * Remove the specified list of resource from storage.
128
     *
129
     * @param  \Illuminate\Http\Request  $request
130
     * @return \Illuminate\Http\RedirectResponse
131
     */
132 1
    public function destroyMultiple(Request $request)
133
    {
134 1
        Denomination::destroy($request->input('checkbox', []));
135
136 1
        return redirect()->route('admin.denomination.index')->with([
137
            'alert' => [
138 1
                'type' => 'alert-success',
139 1
                'message' => trans('The :resource was deleted!', ['resource' => trans('admin-lang.denomination')]),
140
            ],
141
        ]);
142
    }
143
144
    /**
145
     * Remove the image of specified resource from storage.
146
     *
147
     * @param  \App\Models\Denomination  $denomination
148
     * @return \Illuminate\Http\RedirectResponse
149
     */
150 1
    public function destroyImage(Denomination $denomination)
151
    {
152 1
        Storage::delete(Denomination::IMAGE_PATH . '/' . $denomination->getRawOriginal('image'));
0 ignored issues
show
Bug introduced by
Are you sure $denomination->getRawOriginal('image') of type array|mixed can be used in concatenation? ( Ignorable by Annotation )

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

152
        Storage::delete(Denomination::IMAGE_PATH . '/' . /** @scrutinizer ignore-type */ $denomination->getRawOriginal('image'));
Loading history...
153
154 1
        $denomination->update(['image' => null]);
155
156 1
        return redirect()->route('admin.denomination.edit', $denomination)->with([
157
            'alert' => [
158 1
                'type' => 'alert-success',
159 1
                'message' => trans('The :resource was deleted!', ['resource' => trans('This image')]),
160
            ],
161
        ]);
162
    }
163
}
164