Passed
Push — main ( 4f7426...d759d2 )
by Daniel
04:31
created

AlbumFavoriteApplication::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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\Component\Session\SessionValidatorMiddleware;
12
use Uxmp\Core\Orm\Model\UserInterface;
13
use Uxmp\Core\Orm\Repository\AlbumRepositoryInterface;
14
15
final class AlbumFavoriteApplication extends AbstractApiApplication
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
        /** @var UserInterface $user */
30 1
        $user = $request->getAttribute(SessionValidatorMiddleware::USER);
31 1
        $list = [];
32
33 1
        foreach ($this->albumRepository->getFavorites($user) as $album) {
34 1
            $artist = $album->getArtist();
35
36 1
            $albumId = $album->getId();
37
38
            $list[] = [
39 1
                'id' => $albumId,
40 1
                'artistId' => $artist->getId(),
41 1
                'artistName' => $artist->getTitle(),
42 1
                'name' => $album->getTitle(),
43 1
                'cover' => sprintf('%s/art/album/%d', $baseUrl, $albumId),
44 1
                'length' => $album->getLength(),
45
            ];
46
        }
47
48 1
        return $this->asJson($response, ['items' => $list]);
49
    }
50
}
51