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

PlaybackHistoryApplication::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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