Passed
Push — main ( 7dcca1...e70bce )
by Daniel
04:22
created

PlaybackHistoryItem   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 21
c 1
b 0
f 0
dl 0
loc 48
ccs 23
cts 23
cp 1
rs 10
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 readonly class PlaybackHistoryItem implements JsonSerializable
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 11 at column 6
Loading history...
12
{
13 2
    public function __construct(
14
        private ConfigProviderInterface $config,
15
        private PlaybackHistoryInterface $playbackHistory,
16
    ) {
17 2
    }
18
19
    /**
20
     * @return array{
21
     *  id: int,
22
     *  name: string,
23
     *  artistName: string|null,
24
     *  albumName: string|null,
25
     *  trackNumber: int,
26
     *  playUrl: string,
27
     *  cover: string,
28
     *  artistId: int,
29
     *  albumId: int,
30
     *  length: int,
31
     *  userId: int,
32
     *  userName: string
33
     * }
34
     */
35 1
    public function jsonSerialize(): array
36
    {
37 1
        $song = $this->playbackHistory->getSong();
38 1
        $album = $song->getDisc()->getAlbum();
39 1
        $user = $this->playbackHistory->getUser();
40 1
        $artist = $song->getArtist();
41
42 1
        $songId = $song->getId();
43 1
        $albumId = $album->getId();
44 1
        $baseUrl = $this->config->getBaseUrl();
45
46 1
        return [
47 1
            'id' => $songId,
48 1
            'name' => $song->getTitle(),
49 1
            'artistName' => $artist->getTitle(),
50 1
            'albumName' => $album->getTitle(),
51 1
            'trackNumber' => $song->getTrackNumber(),
52 1
            'playUrl' => sprintf('%s/play/%d', $baseUrl, $songId),
53 1
            'cover' => sprintf('%s/art/album/%d', $baseUrl, $albumId),
54 1
            'artistId' => $artist->getId(),
55 1
            'albumId' => $albumId,
56 1
            'length' => $song->getLength(),
57 1
            'userId' => $user->getId(),
58 1
            'userName' => $user->getName(),
59 1
        ];
60
    }
61
}
62