ApiSoundsController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 59
c 1
b 1
f 0
dl 0
loc 143
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A updateEnabled() 0 19 2
A index() 0 7 1
A destroy() 0 11 1
A updateAllSortOrder() 0 24 4
A uploadSound() 0 35 5
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 Illuminate\Support\Facades\File;
11
use jeremykenedy\LaravelLogger\App\Http\Traits\ActivityLogger;
12
13
class ApiSoundsController extends Controller
14
{
15
    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\ApiSoundsController.
Loading history...
16
17
    /**
18
     * Display a listing of the resource.
19
     *
20
     * @return \Illuminate\Http\Response
21
     */
22
    public function index()
23
    {
24
        $sounds = SoundServices::getEnabledSortedSounds();
25
26
        ActivityLogger::activity('All Sounds loaded from API');
27
28
        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...
29
    }
30
31
    /**
32
     * Update the sort order.
33
     *
34
     * @param \Illuminate\Http\Request $request The request
35
     *
36
     * @return \Illuminate\Http\Response
37
     */
38
    public function updateAllSortOrder(Request $request)
39
    {
40
        UserServices::checkIsUserAdminOrHigher($request->userId);
41
42
        $this->validate($request, [
43
            'sounds.*.sort_order' => 'required|numeric',
44
        ]);
45
46
        $sounds = SoundServices::getAllSounds();
47
48
        foreach ($sounds as $sound) {
49
            $id = $sound->id;
50
            foreach ($request->sounds as $soundsNew) {
51
                if ($soundsNew['id'] == $id) {
52
                    $sound->update(['sort_order' => $soundsNew['sort_order']]);
53
                }
54
            }
55
        }
56
57
        ActivityLogger::activity('Sounds sort order updated');
58
59
        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...
60
            'message' => trans('admin.messages.sort-order-updated'),
61
        ], 200);
62
    }
63
64
    /**
65
     * Update enabled/disabled status of a sound.
66
     *
67
     * @param \Illuminate\Http\Request $request The request
68
     * @param int                      $id      The identifier
69
     *
70
     * @return \Illuminate\Http\Response
71
     */
72
    public function updateEnabled(Request $request, $id)
73
    {
74
        UserServices::checkIsUserAdminOrHigher($request->userId);
75
76
        $this->validate($request, [
77
            'sound.enabled' => 'required|boolean',
78
        ]);
79
        $sound = SoundServices::updateSoundStatus($id, $request->sound['enabled']);
80
        $status = 'disabled';
81
        if ($sound->enabled) {
82
            $status = 'enabled';
83
        }
84
        $message = trans('admin.messages.status-updated', ['status' => $status, 'title' => $sound->title]);
85
86
        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

86
        ActivityLogger::activity(/** @scrutinizer ignore-type */ $message);
Loading history...
87
88
        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...
89
            'message' => $message,
90
        ], 200);
91
    }
92
93
    /**
94
     * Remove the specified resource from storage.
95
     *
96
     * @param \Illuminate\Http\Request $request The request
97
     * @param int                      $id
98
     *
99
     * @return \Illuminate\Http\Response
100
     */
101
    public function destroy(Request $request, $id)
102
    {
103
        UserServices::checkIsUserAdminOrHigher($request->userId);
104
105
        $sound = SoundServices::deleteSound(SoundServices::getSound($id));
106
107
        ActivityLogger::activity('Sounds deleted: '.$sound);
108
109
        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...
110
            'message' => trans('admin.messages.sound-deleted', ['title' => $sound->title]),
111
        ], 200);
112
    }
113
114
    /**
115
     * Handle uploads of new sound files.
116
     *
117
     * @param  Request
118
     *
119
     * @return [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
120
     */
121
    public function uploadSound(Request $request)
122
    {
123
        UserServices::checkIsUserAdminOrHigher($request->userId);
124
125
        $file = $request->file('audio_data');
126
127
        if ($request->hasFile('audio_data') && $file->isValid()) {
128
            $uniqueid = uniqid();
0 ignored issues
show
Unused Code introduced by
The assignment to $uniqueid is dead and can be removed.
Loading history...
129
            $original_name = $file->getClientOriginalName();
130
            $extension = 'wav';
131
            $name = $original_name.'.'.$extension;
132
            $path = config('soundboard.folders.uploads').'/'.config('soundboard.folders.recordings').'/';
133
134
            if (!File::exists($path)) {
135
                File::makeDirectory($path);
136
            }
137
138
            $files = scandir($path);
139
            if (in_array($name, array_map('strtolower', $files))) {
0 ignored issues
show
Bug introduced by
It seems like $files can also be of type false; however, parameter $arr1 of array_map() does only seem to accept 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

139
            if (in_array($name, array_map('strtolower', /** @scrutinizer ignore-type */ $files))) {
Loading history...
140
                return response()->json([
141
                    'error' => 'Filename already exists. Choose another filename.',
142
                ], 422);
143
            }
144
            $storedFile = $file->storeAs(config('soundboard.folders.recordings'), $name, config('soundboard.folders.uploads'));
0 ignored issues
show
Unused Code introduced by
The assignment to $storedFile is dead and can be removed.
Loading history...
145
146
            ActivityLogger::activity('New file recorded: '.$name);
147
148
            return response()->json([
149
                'message' => 'File Recorded: '.$name,
150
            ], 202);
151
        }
152
153
        return response()->json([
154
            'error' => 'missing file or is invalid',
155
        ], 422);
156
    }
157
}
158