Passed
Push — main ( 20ba13...98749a )
by Daniel
03:42
created

PlaybackHistoryApplication::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 30
ccs 0
cts 8
cp 0
rs 9.7
cc 2
nc 2
nop 3
crap 6
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\Orm\Repository\PlaybackHistoryRepositoryInterface;
11
12
final class PlaybackHistoryApplication extends AbstractApiApplication
13
{
14
    private const HISTORY_LIMIT = 15;
15
16
    public function __construct(
17
        private PlaybackHistoryRepositoryInterface $playbackHistoryRepository,
18
    ) {
19
    }
20
21
    protected function run(
22
        ServerRequestInterface $request,
23
        ResponseInterface $response,
24
        array $args
25
    ): ResponseInterface {
26
        $history = $this->playbackHistoryRepository->findBy(
27
            [],
28
            ['play_date' => 'DESC'],
29
            static::HISTORY_LIMIT
30
        );
31
32
        $result = [];
33
34
        foreach ($history as $item) {
35
            $song = $item->getSong();
36
            $user = $item->getUser();
37
38
            $result[] = [
39
                'songId' => $song->getId(),
40
                'songName' => $song->getTitle(),
41
                'songArtistName' => $song->getArtist()->getTitle(),
42
                'userId' => $user->getId(),
43
                'userName' => $user->getName(),
44
                'playDate' => $item->getPlayDate()->getTimestamp(),
45
            ];
46
        }
47
48
        return $this->asJson(
49
            $response,
50
            $result,
51
        );
52
    }
53
}
54