Passed
Push — main ( 98749a...e96c5f )
by Daniel
04:24
created

PlaybackHistoryItem::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 24
ccs 20
cts 20
cp 1
rs 9.6
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Api\Lib;
6
7
use JsonSerializable;
8
use Uxmp\Core\Component\Config\ConfigProviderInterface;
9
use Uxmp\Core\Orm\Model\PlaybackHistoryInterface;
10
11
final class PlaybackHistoryItem implements JsonSerializable
12
{
13 2
    public function __construct(
14
        private ConfigProviderInterface $config,
15
        private PlaybackHistoryInterface $playbackHistory,
16
    ) {
17 2
    }
18
19 1
    public function jsonSerialize()
20
    {
21 1
        $song = $this->playbackHistory->getSong();
22 1
        $album = $song->getDisc()->getAlbum();
23 1
        $user = $this->playbackHistory->getUser();
24 1
        $artist = $song->getArtist();
25
26 1
        $songId = $song->getId();
27 1
        $albumId = $album->getId();
28 1
        $baseUrl = $this->config->getBaseUrl();
29
30
        return [
31 1
            'id' => $songId,
32 1
            'name' => $song->getTitle(),
33 1
            'artistName' => $artist->getTitle(),
34 1
            'albumName' => $album->getTitle(),
35 1
            'trackNumber' => $song->getTrackNumber(),
36 1
            'playUrl' => sprintf('%s/play/%d', $baseUrl, $songId),
37 1
            'cover' => sprintf('%s/art/album/%d', $baseUrl, $albumId),
38 1
            'artistId' => $artist->getId(),
39 1
            'albumId' => $albumId,
40 1
            'length' => $song->getLength(),
41 1
            'userId' => $user->getId(),
42 1
            'userName' => $user->getName(),
43
        ];
44
    }
45
}
46