Passed
Push — main ( c61767...b346c5 )
by Daniel
03:42
created

AlbumRecentApplication::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2.0017

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
nc 2
nop 3
dl 0
loc 22
ccs 12
cts 13
cp 0.9231
crap 2.0017
rs 9.8666
c 1
b 0
f 0
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 Uxmp\Core\Api\AbstractApiApplication;
10
use Uxmp\Core\Component\Config\ConfigProviderInterface;
11
use Uxmp\Core\Orm\Repository\AlbumRepositoryInterface;
12
13
final class AlbumRecentApplication extends AbstractApiApplication
14
{
15
    private const ALBUM_LIMIT = 10;
16
17 1
    public function __construct(
18
        private AlbumRepositoryInterface $albumRepository,
19
        private ConfigProviderInterface $config
20
    ) {
21 1
    }
22
23 1
    protected function run(
24
        ServerRequestInterface $request,
25
        ResponseInterface $response,
26
        array $args
27
    ): ResponseInterface {
28 1
        $baseUrl = $this->config->getBaseUrl();
29 1
        $list = [];
30
31 1
        foreach ($this->albumRepository->findBy([], ['last_modified' => 'DESC'], static::ALBUM_LIMIT) as $album) {
32 1
            $artist = $album->getArtist();
33
34
            $list[] = [
35 1
                'id' => $album->getId(),
36 1
                'artistId' => $artist->getId(),
37 1
                'artistName' => $artist->getTitle(),
38 1
                'name' => $album->getTitle(),
39 1
                'cover' => sprintf('%s/art/album/%s', $baseUrl, $album->getMbid()),
40 1
                'length' => $album->getLength(),
41
            ];
42
        }
43
44 1
        return $this->asJson($response, ['items' => $list]);
45
    }
46
}
47