AlbumSongsApplication   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 19
c 1
b 0
f 0
dl 0
loc 43
ccs 23
cts 23
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 35 4
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
15
final class AlbumSongsApplication extends AbstractApiApplication
16
{
17 2
    public function __construct(
18
        private readonly AlbumRepositoryInterface $albumRepository,
19
        private readonly ResultItemFactoryInterface $resultItemFactory
20
    ) {
21 2
    }
22
23 2
    protected function run(
24
        ServerRequestInterface $request,
25
        JsonEnabledResponseInterface $response,
26
        array $args
27
    ): ResponseInterface {
28 2
        $albumId = (int) ($args['albumId'] ?? 0);
29
30 2
        $album = $this->albumRepository->find($albumId);
31
32 2
        if ($album === null) {
33 1
            return $response->withStatus(Http::NOT_FOUND);
34
        }
35
36 1
        $discsData = [];
37
38 1
        foreach ($album->getDiscs() as $disc) {
39 1
            $songData = [];
40
41 1
            foreach ($disc->getSongs() as $song) {
42 1
                $songData[] = $this->resultItemFactory->createSongListItem(
43 1
                    $song,
44 1
                    $album
45 1
                );
46
            }
47
48 1
            $discsData[] = [
49 1
                'id' => $disc->getId(),
50 1
                'songs' => $songData,
51 1
                'length' => $disc->getLength(),
52 1
                'number' => $disc->getNumber(),
53 1
            ];
54
        }
55
56 1
        return $response->withJson(
57 1
            ['items' => $discsData]
58 1
        );
59
    }
60
}
61