AlbumListByGenreApplication::run()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.9666
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Api\Album;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Teapot\StatusCode\Http;
10
use Uxmp\Core\Api\AbstractApiApplication;
11
use Uxmp\Core\Api\Lib\Message\JsonEnabledResponseInterface;
12
use Uxmp\Core\Api\Lib\ResultItemFactoryInterface;
13
use Uxmp\Core\Orm\Repository\AlbumRepositoryInterface;
14
use Uxmp\Core\Orm\Repository\GenreRepositoryInterface;
15
16
/**
17
 * Builds a result containing all albums items having a certain genre
18
 */
19
final class AlbumListByGenreApplication extends AbstractApiApplication
20
{
21 2
    public function __construct(
22
        private readonly AlbumRepositoryInterface $albumRepository,
23
        private readonly GenreRepositoryInterface $genreRepository,
24
        private readonly ResultItemFactoryInterface $resultItemFactory,
25
    ) {
26 2
    }
27
28 2
    protected function run(
29
        ServerRequestInterface $request,
30
        JsonEnabledResponseInterface $response,
31
        array $args
32
    ): ResponseInterface {
33 2
        $genreId = (int) ($args['genreId'] ?? null);
34
35 2
        $genre = $this->genreRepository->find($genreId);
36 2
        if ($genre === null) {
37 1
            return $response->withStatus(Http::NOT_FOUND);
38
        }
39
40 1
        $list = [];
41
42 1
        foreach ($this->albumRepository->findByGenre($genre) as $album) {
43 1
            $list[] = $this->resultItemFactory->createAlbumListItem($album);
44
        }
45
46 1
        return $response->withJson(
47 1
            ['items' => $list]
48 1
        );
49
    }
50
}
51