Passed
Push — main ( 495c0a...a633e0 )
by Daniel
05:04
created

UserPasswordUpdateApplication   A

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
eloc 15
c 1
b 0
f 0
dl 0
loc 42
ccs 13
cts 13
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A serve() 0 26 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Api\Settings;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Teapot\StatusCode;
10
use Uxmp\Core\Api\Lib\SchemaValidatorInterface;
11
use Uxmp\Core\Component\User\PasswordVerificatorInterface;
12
use Uxmp\Core\Component\User\PrivilegeCheckerInterface;
13
use Uxmp\Core\Orm\Repository\UserRepositoryInterface;
14
15
/**
16
 * Sets the user's password
17
 */
18
final class UserPasswordUpdateApplication extends AbstractSettingsApplication
19
{
20
    /**
21
     * @param SchemaValidatorInterface<array{userId: null|int, password: string}> $schemaValidator
22
     */
23 2
    public function __construct(
24
        PrivilegeCheckerInterface $privilegeChecker,
25
        private readonly UserRepositoryInterface $userRepository,
26
        private readonly PasswordVerificatorInterface $passwordVerificator,
27
        private readonly SchemaValidatorInterface $schemaValidator,
28
    ) {
29 2
        parent::__construct(
30
            $privilegeChecker,
31
        );
32
    }
33
34 2
    protected function serve(
35
        ServerRequestInterface $request,
36
        ResponseInterface $response,
37
        array $args
38
    ): ResponseInterface {
39 2
        $body = $this->schemaValidator->getValidatedBody(
40
            $request,
41
            'UserPasswordUpdate.json',
42
        );
43
44 2
        $userId = (int) $body['userId'];
45 2
        $password = $body['password'];
46
47
        // check if a user with that name already exists
48 2
        $user = $this->userRepository->find($userId);
49 2
        if ($user === null) {
50 1
            return $response->withStatus(StatusCode::NOT_FOUND);
51
        }
52
53 1
        $user->setPassword(
54 1
            $this->passwordVerificator->hash($password)
55
        );
56
57 1
        $this->userRepository->save($user);
58
59 1
        return $response;
60
    }
61
}
62