AlbumFavoriteApplication   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 26 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\Api\Lib\Middleware\SessionValidatorMiddleware;
12
use Uxmp\Core\Component\Config\ConfigProviderInterface;
13
use Uxmp\Core\Orm\Model\UserInterface;
14
use Uxmp\Core\Orm\Repository\AlbumRepositoryInterface;
15
16
final class AlbumFavoriteApplication extends AbstractApiApplication
17
{
18 1
    public function __construct(
19
        private readonly AlbumRepositoryInterface $albumRepository,
20
        private readonly ConfigProviderInterface $config
21
    ) {
22 1
    }
23
24 1
    protected function run(
25
        ServerRequestInterface $request,
26
        JsonEnabledResponseInterface $response,
27
        array $args
28
    ): ResponseInterface {
29 1
        $baseUrl = $this->config->getBaseUrl();
30
        /** @var UserInterface $user */
31 1
        $user = $request->getAttribute(SessionValidatorMiddleware::USER);
32 1
        $list = [];
33
34 1
        foreach ($this->albumRepository->getFavorites($user) as $album) {
35 1
            $artist = $album->getArtist();
36
37 1
            $albumId = $album->getId();
38
39 1
            $list[] = [
40 1
                'id' => $albumId,
41 1
                'artistId' => $artist->getId(),
42 1
                'artistName' => $artist->getTitle(),
43 1
                'name' => $album->getTitle(),
44 1
                'cover' => sprintf('%s/art/album/%d', $baseUrl, $albumId),
45 1
                'length' => $album->getLength(),
46 1
            ];
47
        }
48
49 1
        return $response->withJson(['items' => $list]);
50
    }
51
}
52