1 | <?php |
||
2 | |||
3 | namespace App\Http\Controllers\API; |
||
4 | |||
5 | use App\Http\Requests\API\PlaylistStoreRequest; |
||
6 | use App\Http\Requests\API\PlaylistSyncRequest; |
||
7 | use App\Models\Playlist; |
||
8 | use App\Repositories\PlaylistRepository; |
||
9 | use App\Services\SmartPlaylistService; |
||
10 | use Exception; |
||
11 | use Illuminate\Auth\Access\AuthorizationException; |
||
12 | use Illuminate\Http\JsonResponse; |
||
13 | use Illuminate\Http\Request; |
||
14 | |||
15 | /** |
||
16 | * @group 4. Playlist management |
||
17 | */ |
||
18 | class PlaylistController extends Controller |
||
19 | { |
||
20 | private $playlistRepository; |
||
21 | private $smartPlaylistService; |
||
22 | |||
23 | 5 | public function __construct(PlaylistRepository $playlistRepository, SmartPlaylistService $smartPlaylistService) |
|
24 | { |
||
25 | 5 | $this->playlistRepository = $playlistRepository; |
|
26 | 5 | $this->smartPlaylistService = $smartPlaylistService; |
|
27 | 5 | } |
|
28 | |||
29 | /** |
||
30 | * Get current user's playlists. |
||
31 | * |
||
32 | * @responseFile responses/playlist.index.json |
||
33 | * |
||
34 | * @return JsonResponse |
||
35 | */ |
||
36 | 1 | public function index() |
|
37 | { |
||
38 | 1 | return response()->json($this->playlistRepository->getAllByCurrentUser()); |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * Create a new playlist. |
||
43 | * |
||
44 | * @bodyParam name string required Name of the playlist. Example: Sleepy Songs |
||
45 | * @bodyParam rules array An array of rules if creating a "smart playlist." |
||
46 | * @responseFile responses/playlist.store.json |
||
47 | * |
||
48 | * @return JsonResponse |
||
49 | */ |
||
50 | 1 | public function store(PlaylistStoreRequest $request) |
|
51 | { |
||
52 | /** @var Playlist $playlist */ |
||
53 | 1 | $playlist = $request->user()->playlists()->create([ |
|
54 | 1 | 'name' => $request->name, |
|
55 | 1 | 'rules' => $request->rules, |
|
56 | ]); |
||
57 | |||
58 | 1 | $songs = (array) $request->songs; |
|
59 | |||
60 | 1 | if ($songs) { |
|
0 ignored issues
–
show
|
|||
61 | 1 | $playlist->songs()->sync($songs); |
|
62 | } |
||
63 | |||
64 | 1 | $playlist->songs = $playlist->songs->pluck('id'); |
|
65 | |||
66 | 1 | return response()->json($playlist); |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * Rename a playlist. |
||
71 | * |
||
72 | * @bodyParam name string required New name of the playlist. Example: Catchy Songs |
||
73 | * @responseFile responses/playlist.update.json |
||
74 | * |
||
75 | * @throws AuthorizationException |
||
76 | * |
||
77 | * @return JsonResponse |
||
78 | */ |
||
79 | 1 | public function update(Request $request, Playlist $playlist) |
|
80 | { |
||
81 | 1 | $this->authorize('owner', $playlist); |
|
82 | |||
83 | 1 | $playlist->update($request->only('name', 'rules')); |
|
84 | |||
85 | 1 | return response()->json($playlist); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Replace a playlist's content. |
||
90 | * |
||
91 | * Instead of adding or removing songs individually, a playlist's content is replaced entirely with an array of song IDs. |
||
92 | * |
||
93 | * @bodyParam songs array required An array of song IDs. |
||
94 | * @response [] |
||
95 | * |
||
96 | * @throws AuthorizationException |
||
97 | * |
||
98 | * @return JsonResponse |
||
99 | */ |
||
100 | 1 | public function sync(PlaylistSyncRequest $request, Playlist $playlist) |
|
101 | { |
||
102 | 1 | $this->authorize('owner', $playlist); |
|
103 | |||
104 | 1 | abort_if($playlist->is_smart, 403, 'A smart playlist\'s content cannot be updated manually.'); |
|
105 | |||
106 | 1 | $playlist->songs()->sync((array) $request->songs); |
|
107 | |||
108 | 1 | return response()->json(); |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Get a playlist's songs. |
||
113 | * |
||
114 | * @response ["0146d01afb742b01f28ab8b556f9a75d", "c741133cb8d1982a5c60b1ce2a1e6e47", "..."] |
||
115 | * |
||
116 | * @throws AuthorizationException |
||
117 | * |
||
118 | * @return JsonResponse |
||
119 | */ |
||
120 | 1 | public function getSongs(Playlist $playlist) |
|
121 | { |
||
122 | 1 | $this->authorize('owner', $playlist); |
|
123 | |||
124 | 1 | return response()->json( |
|
125 | 1 | $playlist->is_smart |
|
126 | ? $this->smartPlaylistService->getSongs($playlist)->pluck('id') |
||
127 | 1 | : $playlist->songs->pluck('id') |
|
128 | ); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Delete a playlist. |
||
133 | * |
||
134 | * @response [] |
||
135 | * |
||
136 | * @throws Exception |
||
137 | * @throws AuthorizationException |
||
138 | * |
||
139 | * @return JsonResponse |
||
140 | */ |
||
141 | 1 | public function destroy(Playlist $playlist) |
|
142 | { |
||
143 | 1 | $this->authorize('owner', $playlist); |
|
144 | |||
145 | 1 | $playlist->delete(); |
|
146 | |||
147 | 1 | return response()->json(); |
|
148 | } |
||
149 | } |
||
150 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.