Passed
Push — develop ( 6dcea6...5ae9a6 )
by Septianata
16:24
created

DenominationController::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 2
crap 2
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\Arr;
12
use Illuminate\Support\Facades\DB;
13
use Illuminate\Support\Facades\Storage;
14
use Yajra\DataTables\Facades\DataTables;
15
16
class DenominationController extends Controller
17
{
18
    /**
19
     * Create a new instance class.
20
     *
21
     * @return void
22
     */
23 9
    public function __construct()
24
    {
25 9
        $this->authorizeResource(Denomination::class, 'denomination');
26 9
    }
27
28
    /**
29
     * Display a listing of the resource.
30
     *
31
     * @return \Illuminate\Contracts\Support\Renderable
32
     */
33 1
    public function index()
34
    {
35 1
        return view('admin.denomination.index');
36
    }
37
38
    /**
39
     * Return datatable server side response.
40
     *
41
     * @return \Illuminate\Http\JsonResponse
42
     */
43 1
    public function datatable()
44
    {
45 1
        $this->authorize('viewAny', Denomination::class);
46
47 1
        return DataTables::eloquent(Denomination::query())
48 1
            ->setTransformer(fn ($model) => DenominationResource::make($model)->resolve())
49 1
            ->toJson();
50
    }
51
52
    /**
53
     * Show the form for creating a new resource.
54
     *
55
     * @return \Illuminate\Contracts\Support\Renderable
56
     */
57 1
    public function create()
58
    {
59 1
        return view('admin.denomination.create');
60
    }
61
62
    /**
63
     * Store a newly created resource in storage.
64
     *
65
     * @param  \App\Http\Requests\Denomination\StoreRequest  $request
66
     * @return \Illuminate\Http\RedirectResponse
67
     */
68 1
    public function store(StoreRequest $request)
69
    {
70
        /** @var \App\Models\Denomination $denomination */
71 1
        $denomination = Denomination::make(Arr::except($request->validated(), 'image'));
72
73 1
        if ($filename = $request->storeImage()) {
74 1
            $denomination->image = $filename;
75
        }
76
77 1
        $denomination->save();
78
79 1
        return redirect()->route('admin.denomination.index')->with([
80
            'alert' => [
81 1
                'type' => 'alert-success',
82 1
                'message' => trans('The :resource was created!', ['resource' => trans('admin-lang.denomination')]),
83
            ],
84
        ]);
85
    }
86
87
    /**
88
     * Display the specified resource.
89
     *
90
     * @param  \App\Models\Denomination  $denomination
91
     * @return \Illuminate\Contracts\Support\Renderable
92
     */
93
    public function show(Denomination $denomination)
94
    {
95
        return view('admin.denomination.show', compact('denomination'));
96
    }
97
98
    /**
99
     * Show the form for editing the specified resource.
100
     *
101
     * @param  \App\Models\Denomination  $denomination
102
     * @return \Illuminate\Contracts\Support\Renderable
103
     */
104 1
    public function edit(Denomination $denomination)
105
    {
106 1
        return view('admin.denomination.edit', compact('denomination'));
107
    }
108
109
    /**
110
     * Update the specified resource in storage.
111
     *
112
     * @param  \App\Http\Requests\Denomination\UpdateRequest  $request
113
     * @param  \App\Models\Denomination  $denomination
114
     * @return \Illuminate\Http\RedirectResponse
115
     */
116 1
    public function update(UpdateRequest $request, Denomination $denomination)
117
    {
118 1
        $denomination->fill(Arr::except($request->validated(), 'image'));
119
120 1
        if ($filename = $request->updateImage()) {
121 1
            $denomination->image = $filename;
122
        }
123
124 1
        $denomination->save();
125
126 1
        return redirect()->route('admin.denomination.edit', $denomination)->with([
127
            'alert' => [
128 1
                'type' => 'alert-success',
129 1
                'message' => trans('The :resource was updated!', ['resource' => trans('admin-lang.denomination')]),
130
            ],
131
        ]);
132
    }
133
134
    /**
135
     * Remove the specified resource from storage.
136
     *
137
     * @param  \App\Models\Denomination  $denomination
138
     * @return \Illuminate\Http\RedirectResponse
139
     */
140 1
    public function destroy(Denomination $denomination)
141
    {
142 1
        $denomination->delete();
143
144 1
        return redirect()->route('admin.denomination.index')->with([
145
            'alert' => [
146 1
                'type' => 'alert-success',
147 1
                'message' => trans('The :resource was deleted!', ['resource' => trans('admin-lang.denomination')]),
148
            ],
149
        ]);
150
    }
151
152
    /**
153
     * Remove the specified list of resource from storage.
154
     *
155
     * @param  \Illuminate\Http\Request  $request
156
     * @return \Illuminate\Http\RedirectResponse
157
     */
158 1
    public function destroyMultiple(Request $request)
159
    {
160 1
        DB::transaction(function () use ($request) {
161 1
            foreach ($request->input('checkbox', []) as $id) {
162 1
                $denomination = Denomination::find($id, ['id', 'image']);
163
164 1
                $this->authorize('delete', $denomination);
165
166 1
                $denomination->delete();
167
            }
168 1
        });
169
170 1
        return redirect()->route('admin.denomination.index')->with([
171
            'alert' => [
172 1
                'type' => 'alert-success',
173 1
                'message' => trans('The :resource was deleted!', ['resource' => trans('admin-lang.denomination')]),
174
            ],
175
        ]);
176
    }
177
178
    /**
179
     * Remove the image of specified resource from storage.
180
     *
181
     * @param  \App\Models\Denomination  $denomination
182
     * @return \Illuminate\Http\RedirectResponse
183
     */
184 1
    public function destroyImage(Denomination $denomination)
185
    {
186 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

186
        Storage::delete(Denomination::IMAGE_PATH . '/' . /** @scrutinizer ignore-type */ $denomination->getRawOriginal('image'));
Loading history...
187
188 1
        $denomination->update(['image' => null]);
189
190 1
        return redirect()->route('admin.denomination.edit', $denomination)->with([
191
            'alert' => [
192 1
                'type' => 'alert-success',
193 1
                'message' => trans('The :resource was deleted!', ['resource' => trans('This image')]),
194
            ],
195
        ]);
196
    }
197
}
198