Passed
Push — master ( 3e245e...ef140d )
by Phan
07:34
created

API/ObjectStorage/S3/SongController.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace App\Http\Controllers\API\ObjectStorage\S3;
4
5
use App\Events\LibraryChanged;
6
use App\Http\Requests\API\ObjectStorage\S3\PutSongRequest;
7
use App\Http\Requests\API\ObjectStorage\S3\RemoveSongRequest;
8
use App\Models\Album;
9
use App\Models\Artist;
10
use App\Models\Song;
11
use App\Repositories\SongRepository;
12
use App\Services\HelperService;
13
use App\Services\MediaMetadataService;
14
use Exception;
15
use Illuminate\Http\JsonResponse;
16
17
/**
18
 * @group AWS integration
19
 *
20
 * These routes are meant for Amazon Web Services (AWS) integration with Koel. For more information, visit
21
 * [koel-aws](https://github.com/koel/koel-aws).
22
 */
23
class SongController extends Controller
24
{
25
    private $mediaMetadataService;
26
    private $songRepository;
27
    private $helperService;
28
29 2
    public function __construct(
30
        MediaMetadataService $mediaMetadataService,
31
        HelperService $helperService,
32
        SongRepository $songRepository
33
    ) {
34 2
        $this->mediaMetadataService = $mediaMetadataService;
35 2
        $this->songRepository = $songRepository;
36 2
        $this->helperService = $helperService;
37 2
    }
38
39
    /**
40
     * Store a song.
41
     *
42
     * Create a new song or update an existing one with data sent from AWS.
43
     *
44
     * @return JsonResponse
45
     */
46 1
    public function put(PutSongRequest $request)
47
    {
48 1
        $path = "s3://{$request->bucket}/{$request->key}";
49
50 1
        $tags = $request->tags;
51 1
        $artist = Artist::get(array_get($tags, 'artist'));
0 ignored issues
show
It seems like array_get($tags, 'artist') can also be of type null and string[]; however, parameter $name of App\Models\Artist::get() 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

51
        $artist = Artist::get(/** @scrutinizer ignore-type */ array_get($tags, 'artist'));
Loading history...
52
53 1
        $compilation = (bool) trim(array_get($tags, 'albumartist'));
0 ignored issues
show
It seems like array_get($tags, 'albumartist') can also be of type string[]; however, parameter $str of trim() 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

53
        $compilation = (bool) trim(/** @scrutinizer ignore-type */ array_get($tags, 'albumartist'));
Loading history...
54 1
        $album = Album::get($artist, array_get($tags, 'album'), $compilation);
0 ignored issues
show
It seems like array_get($tags, 'album') can also be of type null and string[]; however, parameter $name of App\Models\Album::get() 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

54
        $album = Album::get($artist, /** @scrutinizer ignore-type */ array_get($tags, 'album'), $compilation);
Loading history...
55
56 1
        if ($cover = array_get($tags, 'cover')) {
57
            $this->mediaMetadataService->writeAlbumCover($album, base64_decode($cover['data']), $cover['extension']);
58
        }
59
60 1
        $song = Song::updateOrCreate(['id' => $this->helperService->getFileHash($path)], [
61 1
            'path' => $path,
62 1
            'album_id' => $album->id,
63 1
            'artist_id' => $artist->id,
64 1
            'title' => trim(array_get($tags, 'title', '')),
65 1
            'length' => array_get($tags, 'duration', 0) ?: 0,
66 1
            'track' => (int) array_get($tags, 'track'),
67 1
            'lyrics' => array_get($tags, 'lyrics', '') ?: '',
68 1
            'mtime' => time(),
69
        ]);
70
71 1
        event(new LibraryChanged());
72
73 1
        return response()->json($song);
74
    }
75
76
    /**
77
     * Remove a song.
78
     *
79
     * Remove a song whose information matches with data sent from AWS.
80
     *
81
     * @throws Exception
82
     *
83
     * @return JsonResponse
84
     */
85 1
    public function remove(RemoveSongRequest $request)
86
    {
87 1
        $song = $this->songRepository->getOneByPath("s3://{$request->bucket}/{$request->key}");
88 1
        abort_unless((bool) $song, 404);
89
90 1
        $song->delete();
91 1
        event(new LibraryChanged());
92
93 1
        return response()->json();
94
    }
95
}
96