RandomSongsApplication::__construct()   A
last analyzed

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 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Api\Random;
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\ResultItemFactoryInterface;
12
use Uxmp\Core\Component\Song\RandomSongsRetrieverInterface;
13
14
final class RandomSongsApplication extends AbstractApiApplication
15
{
16
    /** @var int */
17
    private const DEFAULT_LIMIT = 100;
18
19 1
    public function __construct(
20
        private readonly RandomSongsRetrieverInterface $randomSongsRetriever,
21
        private readonly ResultItemFactoryInterface $resultItemFactory,
22
    ) {
23 1
    }
24
25 1
    protected function run(
26
        ServerRequestInterface $request,
27
        JsonEnabledResponseInterface $response,
28
        array $args
29
    ): ResponseInterface {
30 1
        $list = [];
31
32 1
        $limit = (int) ($args['limit'] ?? self::DEFAULT_LIMIT);
33
34 1
        foreach ($this->randomSongsRetriever->retrieve($limit) as $song) {
35 1
            $album = $song->getDisc()->getAlbum();
36
37 1
            $list[] = $this->resultItemFactory->createSongListItem($song, $album);
38
        }
39
40 1
        return $response->withJson(
41 1
            ['items' => $list]
42 1
        );
43
    }
44
}
45