PlaylistCreationApplication   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 16
c 1
b 0
f 0
dl 0
loc 42
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 27 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Api\Playlist;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Teapot\StatusCode\Http;
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\Component\Playlist\PlaylistTypeEnum;
15
use Uxmp\Core\Orm\Repository\PlaylistRepositoryInterface;
16
use ValueError;
17
18
/**
19
 * Creates a playlist
20
 */
21
final class PlaylistCreationApplication extends AbstractApiApplication
22
{
23
    /**
24
     * @param SchemaValidatorInterface<array{
25
     *  name: string,
26
     *  url: string,
27
     *  typeId: integer
28
     * }> $schemaValidator
29
     */
30 2
    public function __construct(
31
        private readonly PlaylistRepositoryInterface $playlistRepository,
32
        private readonly SchemaValidatorInterface $schemaValidator,
33
    ) {
34 2
    }
35
36 2
    protected function run(
37
        ServerRequestInterface $request,
38
        JsonEnabledResponseInterface $response,
39
        array $args
40
    ): ResponseInterface {
41 2
        $body = $this->schemaValidator->getValidatedBody(
42 2
            $request,
43 2
            'PlaylistCreation.json',
44 2
        );
45
46
        try {
47 2
            $type = PlaylistTypeEnum::from($body['typeId']);
48 1
        } catch (ValueError) {
49 1
            return $response->withStatus(Http::BAD_REQUEST);
50
        }
51
52 1
        $playlist = $this->playlistRepository->prototype()
53 1
            ->setName($body['name'])
54 1
            ->setOwner($request->getAttribute(SessionValidatorMiddleware::USER))
55 1
            ->setType($type)
56 1
        ;
57
58 1
        $this->playlistRepository->save($playlist);
59
60 1
        return $response
61 1
            ->withJson(['result' => $playlist->getId()])
62 1
            ->withStatus(Http::CREATED);
63
    }
64
}
65