SongController::play()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 4
crap 2
1
<?php
2
3
namespace App\Http\Controllers\API;
4
5
use App\Factories\StreamerFactory;
6
use App\Http\Requests\API\SongPlayRequest;
7
use App\Http\Requests\API\SongUpdateRequest;
8
use App\Models\Song;
9
use App\Repositories\AlbumRepository;
10
use App\Repositories\ArtistRepository;
11
use App\Services\MediaInformationService;
12
use Illuminate\Http\JsonResponse;
13
use Illuminate\Http\RedirectResponse;
14
use Illuminate\Routing\Redirector;
15
16
/**
17
 * @group 3. Song interactions
18
 */
19
class SongController extends Controller
20
{
21
    private $mediaInformationService;
22
    private $streamerFactory;
23
    private $artistRepository;
24
    private $albumRepository;
25
26 5
    public function __construct(
27
        MediaInformationService $mediaInformationService,
28
        StreamerFactory $streamerFactory,
29
        ArtistRepository $artistRepository,
30
        AlbumRepository $albumRepository
31
    ) {
32 5
        $this->mediaInformationService = $mediaInformationService;
33 5
        $this->streamerFactory = $streamerFactory;
34 5
        $this->artistRepository = $artistRepository;
35 5
        $this->albumRepository = $albumRepository;
36 5
    }
37
38
    /**
39
     * Play a song.
40
     *
41
     * The GET request to play/stream a song. By default Koel will serve the file as-is, unless it's a FLAC.
42
     * If the value of `transcode` is truthy, Koel will attempt to transcode the file into `bitRate`kbps using ffmpeg.
43
     *
44
     * @response {}
45
     *
46
     * @queryParam jwt-token required The JWT token.
47
     *
48
     * @link https://github.com/phanan/koel/wiki#streaming-music
49
     *
50
     * @param null|bool $transcode Whether to force transcoding the song.
51
     *                             If this is omitted, by default Koel will transcode FLAC.
52
     * @param null|int  $bitRate   The target bit rate to transcode, defaults to OUTPUT_BIT_RATE.
53
     *                             Only taken into account if $transcode is truthy.
54
     *
55
     * @return RedirectResponse|Redirector
56
     */
57
    public function play(SongPlayRequest $request, Song $song, ?bool $transcode = null, ?int $bitRate = null)
58
    {
59
        return $this->streamerFactory
60
            ->createStreamer($song, $transcode, $bitRate, floatval($request->time))
61
            ->stream();
62
    }
63
64
    /**
65
     * Update song information.
66
     *
67
     * @bodyParam songs array required An array of song IDs to be updated.
68
     * @bodyParam data object required The new data, with these supported fields: `title`, `artistName`, `albumName`, and `lyrics`.
69
     *
70
     * @group 5. Media information
71
     *
72
     * @return JsonResponse
73
     */
74 5
    public function update(SongUpdateRequest $request)
75
    {
76 5
        $updatedSongs = Song::updateInfo($request->songs, $request->data);
77
78 5
        return response()->json([
79 5
            'artists' => $this->artistRepository->getByIds($updatedSongs->pluck('artist_id')->all()),
80 5
            'albums' => $this->albumRepository->getByIds($updatedSongs->pluck('album_id')->all()),
81 5
            'songs' => $updatedSongs,
82
        ]);
83
    }
84
}
85