AlbumRecentApplication::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

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