UserSettingsPasswordUpdateApplication   A
last analyzed

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
wmc 2
eloc 10
c 1
b 0
f 0
dl 0
loc 34
ccs 14
cts 14
cp 1
rs 10

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