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