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

AlbumRecentApplication   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 32
ccs 14
cts 15
cp 0.9333
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 22 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\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