MostPlayedApplication::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Api\Playback;
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\Orm\Repository\PlaybackHistoryRepositoryInterface;
13
use Uxmp\Core\Orm\Repository\SongRepositoryInterface;
14
15
/**
16
 * Retrieve the list of mosty played songs
17
 */
18
final class MostPlayedApplication extends AbstractApiApplication
19
{
20 1
    public function __construct(
21
        private readonly PlaybackHistoryRepositoryInterface $playbackHistoryRepository,
22
        private readonly SongRepositoryInterface $songRepository,
23
        private readonly ResultItemFactoryInterface $resultItemFactory,
24
    ) {
25 1
    }
26
27 1
    protected function run(
28
        ServerRequestInterface $request,
29
        JsonEnabledResponseInterface $response,
30
        array $args
31
    ): ResponseInterface {
32 1
        $mostPlayed = $this->playbackHistoryRepository->getMostPlayed();
33
34 1
        $result = [];
35
36 1
        foreach ($mostPlayed as $item) {
37 1
            $song = $this->songRepository->find($item['song_id']);
38 1
            if ($song === null) {
39 1
                continue;
40
            }
41
42 1
            $result[] = [
43 1
                'count' => $item['cnt'],
44 1
                'song' => $this->resultItemFactory->createSongListItem($song, $song->getAlbum()),
45 1
            ];
46
        }
47
48 1
        return $response->withJson(
49 1
            ['items' => $result],
50 1
        );
51
    }
52
}
53