Passed
Push — main ( a633e0...3a2337 )
by Daniel
10:33
created

UserSettingsPasswordUpdateApplication   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A run() 0 22 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\Middleware\SessionValidatorMiddleware;
11
use Uxmp\Core\Api\Lib\SchemaValidatorInterface;
12
use Uxmp\Core\Component\User\PasswordVerificatorInterface;
13
use Uxmp\Core\Orm\Model\UserInterface;
14
use Uxmp\Core\Orm\Repository\UserRepositoryInterface;
15
16
/**
17
 * Sets the current user's password
18
 */
19
final class UserSettingsPasswordUpdateApplication extends AbstractApiApplication
20
{
21
    /**
22
     * @param SchemaValidatorInterface<array{password: string}> $schemaValidator
23
     */
24 1
    public function __construct(
25
        private readonly UserRepositoryInterface $userRepository,
26
        private readonly PasswordVerificatorInterface $passwordVerificator,
27
        private readonly SchemaValidatorInterface $schemaValidator,
28
    ) {
29
    }
30
31 1
    protected function run(
32
        ServerRequestInterface $request,
33
        ResponseInterface $response,
34
        array $args
35
    ): ResponseInterface {
36 1
        $body = $this->schemaValidator->getValidatedBody(
37
            $request,
38
            'UserPasswordUpdate.json',
39
        );
40
41
        /** @var UserInterface $user */
42 1
        $user = $request->getAttribute(SessionValidatorMiddleware::USER);
43
44 1
        $password = $body['password'];
45
46 1
        $user->setPassword(
47 1
            $this->passwordVerificator->hash($password)
48
        );
49
50 1
        $this->userRepository->save($user);
51
52 1
        return $response;
53
    }
54
}
55