Passed
Push — main ( b3043b...2a9756 )
by Daniel
04:51
created

AlbumApplication::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 25
ccs 14
cts 14
cp 1
rs 9.7998
cc 2
nc 2
nop 3
crap 2
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\Component\Config\ConfigProviderInterface;
12
use Uxmp\Core\Orm\Repository\AlbumRepositoryInterface;
13
14
final class AlbumApplication extends AbstractApiApplication
15
{
16 2
    public function __construct(
17
        private readonly AlbumRepositoryInterface $albumRepository,
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 17 at column 25
Loading history...
18
        private readonly ConfigProviderInterface $config,
19
    ) {
20
    }
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
        $artist = $album->getArtist();
36
37 1
        return $this->asJson(
38
            $response,
39
            [
40 1
                'id' => $albumId,
41 1
                'name' => $album->getTitle(),
42 1
                'artistId' => $artist->getId(),
43 1
                'artistName' => $artist->getTitle(),
44 1
                'cover' => sprintf('%s/art/album/%d', $this->config->getBaseUrl(), $albumId),
45 1
                'length' => $album->getLength(),
46 1
                'mbId' => $album->getMbid(),
47
            ]
48
        );
49
    }
50
}
51