Passed
Push — main ( 3af14e...84a818 )
by Daniel
04:35
created

UserSettingsEditApplication   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 10
c 1
b 0
f 0
dl 0
loc 32
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 21 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Api\User;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Uxmp\Core\Api\AbstractApiApplication;
10
use Uxmp\Core\Api\Lib\SchemaValidatorInterface;
11
use Uxmp\Core\Component\Session\SessionValidatorMiddleware;
12
use Uxmp\Core\Orm\Model\UserInterface;
13
use Uxmp\Core\Orm\Repository\UserRepositoryInterface;
14
15
/**
16
 * Update the user settings
17
 */
18
final class UserSettingsEditApplication extends AbstractApiApplication
19
{
20
    /**
21
     * @param SchemaValidatorInterface<array{language: string}> $schemaValidator
22
     */
23 1
    public function __construct(
24
        private SchemaValidatorInterface $schemaValidator,
25
        private UserRepositoryInterface $userRepository,
26
    ) {
27 1
    }
28
29 1
    protected function run(
30
        ServerRequestInterface $request,
31
        ResponseInterface $response,
32
        array $args
33
    ): ResponseInterface {
34 1
        $body = $this->schemaValidator->getValidatedBody(
35 1
            $request,
36 1
            'UserSettings.json',
37
        );
38
39
        /** @var UserInterface $user */
40 1
        $user = $request->getAttribute(SessionValidatorMiddleware::USER);
41
42 1
        $user->setLanguage($body['language']);
43
44 1
        $this->userRepository->save($user);
45
46 1
        return $this->asJson(
47 1
            $response,
48
            [
49 1
                'result' => true
50
            ]
51
        );
52
    }
53
}
54