RadioStationEditApplication::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 25
ccs 15
cts 15
cp 1
rs 9.8333
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Api\RadioStation;
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\SchemaValidatorInterface;
13
use Uxmp\Core\Orm\Repository\RadioStationRepositoryInterface;
14
15
/**
16
 * Edits a radio station
17
 */
18
final class RadioStationEditApplication extends AbstractApiApplication
19
{
20
    /**
21
     * @param SchemaValidatorInterface<array{name: string, url: string}> $schemaValidator
22
     */
23 2
    public function __construct(
24
        private readonly RadioStationRepositoryInterface $radioStationRepository,
25
        private readonly SchemaValidatorInterface $schemaValidator,
26
    ) {
27 2
    }
28
29 2
    protected function run(
30
        ServerRequestInterface $request,
31
        JsonEnabledResponseInterface $response,
32
        array $args
33
    ): ResponseInterface {
34 2
        $stationId = (int) ($args['stationId'] ?? 0);
35
36 2
        $station = $this->radioStationRepository->find($stationId);
37 2
        if ($station === null) {
38 1
            return $response->withStatus(Http::NOT_FOUND);
39
        }
40
41 1
        $body = $this->schemaValidator->getValidatedBody(
42 1
            $request,
43 1
            'RadioStationCreation.json',
44 1
        );
45
46 1
        $station
47 1
            ->setUrl($body['url'])
48 1
            ->setName($body['name']);
49
50 1
        $this->radioStationRepository->save($station);
51
52 1
        return $response->withJson([
53 1
            'result' => true,
54 1
        ]);
55
    }
56
}
57