Passed
Push — main ( e933de...5ef56a )
by Daniel
03:35
created

SongListItem::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 20
ccs 17
cts 17
cp 1
rs 9.7333
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\AlbumInterface;
10
use Uxmp\Core\Orm\Model\SongInterface;
11
12
final class SongListItem implements JsonSerializable
13
{
14 2
    public function __construct(
15
        private readonly ConfigProviderInterface $config,
16
        private readonly SongInterface $song,
17
        private readonly AlbumInterface $album
18
    ) {
19 2
    }
20
21
    /**
22
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
23
     *  id: int,
24
     *  name: string,
25
     *  artistName: null|string,
26
     *  albumName: null|string,
27
     *  trackNumber: int,
28
     *  playUrl: string,
29
     *  cover: string,
30
     *  artistId: int,
31
     *  albumId: int,
32
     *  length: int,
33
     *  year: int|null
34
     * }
35
     */
36 1
    public function jsonSerialize(): array
37
    {
38 1
        $songId = $this->song->getId();
39 1
        $albumId = $this->album->getId();
40 1
        $baseUrl = $this->config->getBaseUrl();
41
42 1
        $artist = $this->album->getArtist();
43
44 1
        return [
45 1
            'id' => $songId,
46 1
            'name' => $this->song->getTitle(),
47 1
            'artistName' => $artist->getTitle(),
48 1
            'albumName' => $this->album->getTitle(),
49 1
            'trackNumber' => $this->song->getTrackNumber(),
50 1
            'playUrl' => sprintf('%s/play/%d', $baseUrl, $songId),
51 1
            'cover' => sprintf('%s/art/album/%d', $baseUrl, $albumId),
52 1
            'artistId' => $artist->getId(),
53 1
            'albumId' => $albumId,
54 1
            'length' => $this->song->getLength(),
55 1
            'year' => $this->song->getYear(),
56 1
        ];
57
    }
58
}
59