AlbumListByGenreApplication   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
c 1
b 0
f 0
dl 0
loc 29
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A run() 0 20 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