Passed
Push — main ( 7c4b00...773019 )
by Daniel
03:38
created

AlbumSongsApplication::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
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 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
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;
10
use Uxmp\Core\Api\AbstractApiApplication;
11
use Uxmp\Core\Api\Lib\ResultItemFactoryInterface;
12
use Uxmp\Core\Orm\Repository\AlbumRepositoryInterface;
13
14
final class AlbumSongsApplication extends AbstractApiApplication
15
{
16 2
    public function __construct(
17
        private AlbumRepositoryInterface $albumRepository,
18
        private ResultItemFactoryInterface $resultItemFactory
19
    ) {
20 2
    }
21
22 2
    protected function run(
23
        ServerRequestInterface $request,
24
        ResponseInterface $response,
25
        array $args
26
    ): ResponseInterface {
27 2
        $albumId = (int) ($args['albumId'] ?? 0);
28
29 2
        $album = $this->albumRepository->find($albumId);
30
31 2
        if ($album === null) {
32 1
            return $response->withStatus(StatusCode::NOT_FOUND);
33
        }
34
35 1
        $discsData = [];
36
37 1
        foreach ($album->getDiscs() as $disc) {
38 1
            $songData = [];
39
40 1
            foreach ($disc->getSongs() as $song) {
41 1
                $songData[] = $this->resultItemFactory->createSongListItem(
42 1
                    $song,
43
                    $album
44
                );
45
            }
46
47
            $discsData[] = [
48 1
                'id' => $disc->getId(),
49 1
                'songs' => $songData,
50 1
                'length' => $disc->getLength(),
51
            ];
52
        }
53
54 1
        return $this->asJson(
55 1
            $response,
56 1
            ['items' => $discsData]
57
        );
58
    }
59
}
60