Passed
Push — main ( ab47dd...702890 )
by Daniel
12:30
created

AlbumRecentApplication::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.0014

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
nc 2
nop 3
dl 0
loc 24
ccs 13
cts 14
cp 0.9286
crap 2.0014
rs 9.8333
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 1
            $albumId = $album->getId();
35
36
            $list[] = [
37 1
                'id' => $albumId,
38 1
                'artistId' => $artist->getId(),
39 1
                'artistName' => $artist->getTitle(),
40 1
                'name' => $album->getTitle(),
41 1
                'cover' => sprintf('%s/art/album/%d', $baseUrl, $albumId),
42 1
                'length' => $album->getLength(),
43
            ];
44
        }
45
46 1
        return $this->asJson($response, ['items' => $list]);
47
    }
48
}
49