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

ArtistDataProvider::getArtist()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 34
ccs 25
cts 25
cp 1
rs 9.552
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\SubSonic;
6
7
use DateTimeInterface;
8
use Usox\HyperSonic\FeatureSet\V1161\Contract\ArtistDataProviderInterface;
9
use Uxmp\Core\Orm\Repository\ArtistRepositoryInterface;
10
11
/**
12
 * Provides data for a single artist
13
 */
14
final readonly class ArtistDataProvider implements ArtistDataProviderInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 14 at column 6
Loading history...
15
{
16 2
    public function __construct(
17
        private ArtistRepositoryInterface $artistRepository
18
    ) {
19 2
    }
20
21
    /**
22
     * @return null|array{
23
     *  id: int|string,
24
     *  name: string,
25
     *  coverArtId: string,
26
     *  albumCount: int,
27
     *  artistImageUrl: string,
28
     *  albums: array<array{
29
     *    id: int|string,
30
     *    name: string,
31
     *    coverArtId: string,
32
     *    songCount: int,
33
     *    createDate: DateTimeInterface|null,
34
     *    duration: int,
35
     *    artistName: string,
36
     *    artistId: int|string,
37
     *    year?: string,
38
     *    genre?: string,
39
     *    playCount?: int
40
     *  }>
41
     * }
42
     */
43 2
    public function getArtist(
44
        string $artistId
45
    ): ?array {
46 2
        $artist = $this->artistRepository->find((int) $artistId);
47
48 2
        if ($artist === null) {
49 1
            return null;
50
        }
51
52 1
        $albums = [];
53 1
        $artistName = (string) $artist->getTitle();
54
55 1
        foreach ($artist->getAlbums() as $album) {
56 1
            $albumId = $album->getId();
57
58 1
            $albums[] = [
59 1
                'id' => $albumId,
60 1
                'name' => (string) $album->getTitle(),
61 1
                'coverArtId' => 'album-'.$albumId,
62 1
                'songCount' => $album->getSongCount(),
63 1
                'createDate' => null, // @todo implement,
64 1
                'duration' => $album->getLength(),
65 1
                'artistName' => $artistName,
66 1
                'artistId' => $artistId,
67 1
            ];
68
        }
69
70 1
        return [
71 1
            'id' => $artistId,
72 1
            'name' => $artistName,
73 1
            'coverArtId' => 'artist-'.$artistId,
74 1
            'artistImageUrl' => '',
75 1
            'albumCount' => $artist->getAlbumCount(),
76 1
            'albums' => $albums,
77 1
        ];
78
    }
79
}
80