ScrobbleController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 11
c 0
b 0
f 0
dl 0
loc 31
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 13 4
A __construct() 0 3 1
1
<?php
2
3
namespace App\Http\Controllers\API;
4
5
use App\Models\Album;
6
use App\Models\Song;
7
use App\Services\LastfmService;
8
use Illuminate\Http\JsonResponse;
9
use Illuminate\Http\Request;
10
11
/**
12
 * @group Last.fm integration
13
 */
14
class ScrobbleController extends Controller
15
{
16
    private $lastfmService;
17
18 1
    public function __construct(LastfmService $lastfmService)
19
    {
20 1
        $this->lastfmService = $lastfmService;
21 1
    }
22
23
    /**
24
     * Scrobble a song.
25
     *
26
     * Create a [Last.fm scrobble entry](https://www.last.fm/api/scrobbling) for a song.
27
     *
28
     * @param string $timestamp The UNIX timestamp when the song started playing.
29
     *
30
     * @return JsonResponse
31
     */
32 1
    public function store(Request $request, Song $song, string $timestamp)
33
    {
34 1
        if (!$song->artist->is_unknown && $request->user()->connectedToLastfm()) {
35 1
            $this->lastfmService->scrobble(
36 1
                $song->artist->name,
37 1
                $song->title,
38 1
                (int) $timestamp,
39 1
                $song->album->name === Album::UNKNOWN_NAME ? '' : $song->album->name,
40 1
                $request->user()->lastfm_session_key
41
            );
42
        }
43
44 1
        return response()->json();
45
    }
46
}
47