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

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 1
cts 1
cp 1
crap 1
rs 10
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