Test Setup Failed
Pull Request — master (#590)
by
unknown
11:30
created

SongController::play()   D

Complexity

Conditions 9
Paths 10

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
c 0
b 0
f 0
rs 4.909
cc 9
eloc 25
nc 10
nop 4
1
<?php
2
3
namespace App\Http\Controllers\API;
4
5
use App\Http\Requests\API\SongUpdateRequest;
6
use App\Models\Song;
7
use App\Services\Streamers\PHPStreamer;
8
use App\Services\Streamers\S3Streamer;
9
use App\Services\Streamers\TranscodingStreamer;
10
use App\Services\Streamers\XAccelRedirectStreamer;
11
use App\Services\Streamers\XSendFileStreamer;
12
use Illuminate\Http\JsonResponse;
13
use Illuminate\Http\RedirectResponse;
14
use Illuminate\Http\Request;
15
use Illuminate\Routing\Redirector;
16
use App\Services\Streamers\GCPStreamer;
17
18
class SongController extends Controller
19
{
20
    /**
21
     * Play/stream a song.
22
     *
23
     * @link https://github.com/phanan/koel/wiki#streaming-music
24
     *
25
     * @param Request   $request
26
     * @param Song      $song      The song to stream.
27
     * @param null|bool $transcode Whether to force transcoding the song.
28
     *                             If this is omitted, by default Koel will transcode FLAC.
29
     * @param null|int  $bitRate   The target bit rate to transcode, defaults to OUTPUT_BIT_RATE.
30
     *                             Only taken into account if $transcode is truthy.
31
     *
32
     * @return RedirectResponse|Redirector
33
     */
34
    public function play(Request $request, Song $song, $transcode = null, $bitRate = null)
35
    {
36
        if ($song->s3_params) {
37
            return (new S3Streamer($song))->stream();
38
        }
39
40
        if ($song->gcp_params) {
0 ignored issues
show
Documentation introduced by
The property gcp_params does not exist on object<App\Models\Song>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
41
            return (new GCPStreamer($song))->stream();
42
        }
43
		
44
        // If `transcode` parameter isn't passed, the default is to only transcode FLAC.
45
        if ($transcode === null && ends_with(mime_content_type($song->path), 'flac')) {
46
            $transcode = true;
47
        }
48
49
        $streamer = null;
0 ignored issues
show
Unused Code introduced by
$streamer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
51
        if ($transcode) {
52
            $streamer = new TranscodingStreamer(
53
                $song,
54
                $bitRate ?: config('koel.streaming.bitrate'),
55
                floatval($request->time)
56
            );
57
        } else {
58
            switch (config('koel.streaming.method')) {
59
                case 'x-sendfile':
60
                    $streamer = new XSendFileStreamer($song);
61
                    break;
62
                case 'x-accel-redirect':
63
                    $streamer = new XAccelRedirectStreamer($song);
64
                    break;
65
                default:
66
                    $streamer = new PHPStreamer($song);
67
                    break;
68
            }
69
        }
70
71
        $streamer->stream();
72
    }
73
74
    /**
75
     * Get extra information about a song via Last.fm.
76
     *
77
     * @param Song $song
78
     *
79
     * @return JsonResponse
80
     */
81
    public function show(Song $song)
82
    {
83
        return response()->json([
84
            'lyrics' => $song->lyrics,
85
            'album_info' => $song->album->getInfo(),
86
            'artist_info' => $song->artist->getInfo(),
87
            'youtube' => $song->getRelatedYouTubeVideos(),
88
        ]);
89
    }
90
91
    /**
92
     * Update songs info.
93
     *
94
     * @param SongUpdateRequest $request
95
     *
96
     * @return JsonResponse
97
     */
98
    public function update(SongUpdateRequest $request)
99
    {
100
        return response()->json(Song::updateInfo($request->songs, $request->data));
101
    }
102
}
103