NowPlayingUpdate::run()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 36
ccs 23
cts 23
cp 1
rs 9.6333
cc 3
nc 4
nop 3
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Api\Playback;
6
7
use DateTime;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Uxmp\Core\Api\AbstractApiApplication;
11
use Uxmp\Core\Api\Lib\Message\JsonEnabledResponseInterface;
12
use Uxmp\Core\Api\Lib\Middleware\SessionValidatorMiddleware;
13
use Uxmp\Core\Api\Lib\SchemaValidatorInterface;
14
use Uxmp\Core\Orm\Repository\PlaybackHistoryRepositoryInterface;
15
use Uxmp\Core\Orm\Repository\SongRepositoryInterface;
16
use Uxmp\Core\Orm\Repository\TemporaryPlaylistRepositoryInterface;
17
18
/**
19
 * Adds the song to the users' playback history and updates the temporary playlist
20
 */
21
final class NowPlayingUpdate extends AbstractApiApplication
22
{
23
    /**
24
     * @param SchemaValidatorInterface<array{
25
     *  songId: int,
26
     *  temporaryPlaylist: array{
27
     *    id: string|null,
28
     *    offset: int
29
     *  }
30
     * }> $schemaValidator
31
     */
32 1
    public function __construct(
33
        private readonly SchemaValidatorInterface $schemaValidator,
34
        private readonly TemporaryPlaylistRepositoryInterface $temporaryPlaylistRepository,
35
        private readonly PlaybackHistoryRepositoryInterface $playbackHistoryRepository,
36
        private readonly SongRepositoryInterface $songRepository,
37
    ) {
38 1
    }
39
40 1
    protected function run(
41
        ServerRequestInterface $request,
42
        JsonEnabledResponseInterface $response,
43
        array $args
44
    ): ResponseInterface {
45 1
        $body = $this->schemaValidator->getValidatedBody(
46 1
            $request,
47 1
            'NowPlayingUpdate.json',
48 1
        );
49 1
        $user = $request->getAttribute(SessionValidatorMiddleware::USER);
50
51 1
        $temporaryPlaylist = $this->temporaryPlaylistRepository->findOneBy([
52 1
            'id' => $body['temporaryPlaylist']['id'],
53 1
            'owner' => $user,
54 1
        ]);
55
56 1
        if ($temporaryPlaylist !== null) {
57 1
            $temporaryPlaylist->setPlaybackOffset($body['temporaryPlaylist']['offset']);
58
59 1
            $this->temporaryPlaylistRepository->save($temporaryPlaylist);
60
        }
61
62 1
        $song = $this->songRepository->find($body['songId']);
63
64 1
        if ($song !== null) {
65 1
            $history = $this->playbackHistoryRepository->prototype()
66 1
                ->setUser($user)
67 1
                ->setSong($song)
68 1
                ->setPlayDate(new DateTime());
69
70 1
            $this->playbackHistoryRepository->save($history);
71
        }
72
73 1
        return $response->withJson(
74 1
            [
75 1
                'result' => true,
76 1
            ]
77 1
        );
78
    }
79
}
80