Completed
Push — master ( ebed0b...7356ba )
by Jeremy
04:47
created

SoundsController::updateAllSortOrder()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 24
rs 9.8333
1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\Sound;
7
use App\Services\SoundServices;
8
use App\Services\UserServices;
9
use Illuminate\Http\Request;
10
use jeremykenedy\LaravelLogger\App\Http\Traits\ActivityLogger;
11
12
use Illuminate\Support\Facades\Log;
13
14
class SoundsController extends Controller
15
{
16
    use ActivityLogger;
0 ignored issues
show
Bug introduced by
The trait jeremykenedy\LaravelLogg...p\Traits\ActivityLogger requires the property $id which is not provided by App\Http\Controllers\Api\SoundsController.
Loading history...
17
18
    /**
19
     * Display a listing of the resource.
20
     *
21
     * @return \Illuminate\Http\Response
22
     */
23
    public function index()
24
    {
25
        $sounds = SoundServices::getEnabledSortedSounds();
26
27
        ActivityLogger::activity('All Sounds loaded from API');
28
29
        return response()->json($sounds);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($sounds) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
30
    }
31
32
    /**
33
     * Update the sort order
34
     *
35
     * @param \Illuminate\Http\Request  $request  The request
36
     *
37
     * @return \Illuminate\Http\Response
38
     */
39
    public function updateAllSortOrder(Request $request)
40
    {
41
        UserServices::checkIsUserAdminOrHigher($request->userId);
42
43
        $this->validate($request, [
44
            'sounds.*.sort_order' => 'required|numeric',
45
        ]);
46
47
        $sounds = SoundServices::getAllSounds();
48
49
        foreach ($sounds as $sound) {
50
            $id = $sound->id;
51
            foreach ($request->sounds as $soundsNew) {
52
                if ($soundsNew['id'] == $id) {
53
                    $sound->update(['sort_order' => $soundsNew['sort_order']]);
54
                }
55
            }
56
        }
57
58
        ActivityLogger::activity('Sounds sort order updated');
59
60
        return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...-order-updated')), 200) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
61
            'message' => trans('admin.messages.sort-order-updated')
62
        ], 200);
63
    }
64
65
    /**
66
     * Update enabled/disabled status of a sound
67
     *
68
     * @param \Illuminate\Http\Request  $request  The request
69
     * @param int $id The identifier
70
     *
71
     * @return \Illuminate\Http\Response
72
     */
73
    public function updateEnabled(Request $request, $id)
74
    {
75
        UserServices::checkIsUserAdminOrHigher($request->userId);
76
77
        $this->validate($request, [
78
            'sound.enabled' => 'required|boolean',
79
        ]);
80
        $sound = SoundServices::updateSoundStatus($id, $request->sound['enabled']);
81
        $status = 'disabled';
82
        if ($sound->enabled) {
83
            $status = 'enabled';
84
        }
85
        $message = trans('admin.messages.status-updated', ['status' => $status, 'title' => $sound->title]);
86
87
        ActivityLogger::activity($message);
0 ignored issues
show
Bug introduced by
It seems like $message can also be of type array; however, parameter $description of jeremykenedy\LaravelLogg...ivityLogger::activity() does only seem to accept string, 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

87
        ActivityLogger::activity(/** @scrutinizer ignore-type */ $message);
Loading history...
88
89
        return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...age' => $message), 200) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
90
            'message' => $message
91
        ], 200);
92
    }
93
94
    /**
95
     * Remove the specified resource from storage.
96
     *
97
     * @param \Illuminate\Http\Request  $request  The request
98
     * @param int $id
99
     *
100
     * @return \Illuminate\Http\Response
101
     */
102
    public function destroy(Request $request, $id)
103
    {
104
        UserServices::checkIsUserAdminOrHigher($request->userId);
105
106
        $sound = SoundServices::deleteSound(SoundServices::getSound($id));
107
108
        ActivityLogger::activity('Sounds deleted: ' . $sound);
109
110
        return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(... $sound->title))), 200) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
111
            'message' => trans('admin.messages.sound-deleted', ['title' => $sound->title])
112
        ], 200);
113
    }
114
}
115