Passed
Push — main ( 9f54e3...00f6b4 )
by Daniel
04:22
created

ArtistTopSongsApplication::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Api\Artist;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Teapot\StatusCode;
10
use Uxmp\Core\Api\AbstractApiApplication;
11
use Uxmp\Core\Api\Lib\ResultItemFactoryInterface;
12
use Uxmp\Core\Orm\Repository\ArtistRepositoryInterface;
13
use Uxmp\Core\Orm\Repository\SongRepositoryInterface;
14
15
final class ArtistTopSongsApplication extends AbstractApiApplication
16
{
17 2
    public function __construct(
18
        private readonly SongRepositoryInterface $songRepository,
19
        private readonly ArtistRepositoryInterface $artistRepository,
20
        private readonly ResultItemFactoryInterface $resultItemFactory
21
    ) {
22
    }
23
24 2
    protected function run(
25
        ServerRequestInterface $request,
26
        ResponseInterface $response,
27
        array $args
28
    ): ResponseInterface {
29 2
        $list = [];
30
31 2
        $artistId = (int) ($args['artistId'] ?? 0);
32
33 2
        $artist = $this->artistRepository->find($artistId);
34
35 2
        if ($artist === null) {
36 1
            return $response->withStatus(StatusCode::NOT_FOUND);
37
        }
38
39 1
        $result = $this->songRepository->getTopSongsByArtist($artist);
40
41 1
        foreach ($result as $song) {
42 1
            $list[] = $this->resultItemFactory->createSongListItem($song, $song->getAlbum());
43
        }
44
45 1
        return $this->asJson($response, ['items' => $list]);
46
    }
47
}
48