MostPlayedApplication   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A run() 0 23 3
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