Completed
Push — master ( f9282d...b5d17f )
by Jeremy
21:36
created

ApiSoundsController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

89
        ActivityLogger::activity(/** @scrutinizer ignore-type */ $message);
Loading history...
90
91
        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...
92
            'message' => $message,
93
        ], 200);
94
    }
95
96
    /**
97
     * Remove the specified resource from storage.
98
     *
99
     * @param \Illuminate\Http\Request $request The request
100
     * @param int                      $id
101
     *
102
     * @return \Illuminate\Http\Response
103
     */
104
    public function destroy(Request $request, $id)
105
    {
106
        UserServices::checkIsUserAdminOrHigher($request->userId);
107
108
        $sound = SoundServices::deleteSound(SoundServices::getSound($id));
109
110
        ActivityLogger::activity('Sounds deleted: '.$sound);
111
112
        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...
113
            'message' => trans('admin.messages.sound-deleted', ['title' => $sound->title]),
114
        ], 200);
115
    }
116
117
    /**
118
     * Handle uploads of new sound files
119
     *
120
     * @param  Request
121
     *
122
     * @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...
123
     */
124
    public function uploadSound(Request $request)
125
    {
126
        UserServices::checkIsUserAdminOrHigher($request->userId);
127
128
        $file = $request->file('audio_data');
129
130
        if($request->hasFile('audio_data') && $file->isValid()){
131
            $uniqueid=uniqid();
0 ignored issues
show
Unused Code introduced by
The assignment to $uniqueid is dead and can be removed.
Loading history...
132
            $original_name = $file->getClientOriginalName();
133
            $extension = 'wav';
134
            $name = $original_name.'.'.$extension;
135
136
            $path = "sound-files/recordings/";
137
            $files = scandir($path);
138
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
                Log::info('its bad');
141
                return response()->json([
142
                    'error' => 'Filename already exists. Choose another filename.',
143
                ], 422);
144
            }
145
146
            $storedFile = $file->storeAs('recordings', $name, 'sound-files');
0 ignored issues
show
Unused Code introduced by
The assignment to $storedFile is dead and can be removed.
Loading history...
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