UserCreationApplication   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 PH7\Generator\Password;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Teapot\StatusCode\Http;
11
use Uxmp\Core\Api\Lib\Message\JsonEnabledResponseInterface;
12
use Uxmp\Core\Api\Lib\SchemaValidatorInterface;
13
use Uxmp\Core\Component\User\PrivilegeCheckerInterface;
14
use Uxmp\Core\Component\User\UserCreatorInterface;
15
use Uxmp\Core\Orm\Repository\UserRepositoryInterface;
16
17
/**
18
 * Adds a user
19
 */
20
final class UserCreationApplication extends AbstractSettingsApplication
21
{
22
    /**
23
     * @param SchemaValidatorInterface<array{name: string}> $schemaValidator
24
     */
25 2
    public function __construct(
26
        PrivilegeCheckerInterface $privilegeChecker,
27
        private readonly UserRepositoryInterface $userRepository,
28
        private readonly UserCreatorInterface $userCreator,
29
        private readonly SchemaValidatorInterface $schemaValidator,
30
    ) {
31 2
        parent::__construct(
32 2
            $privilegeChecker,
33 2
        );
34
    }
35
36 2
    protected function serve(
37
        ServerRequestInterface $request,
38
        JsonEnabledResponseInterface $response,
39
        array $args
40
    ): ResponseInterface {
41 2
        $body = $this->schemaValidator->getValidatedBody(
42 2
            $request,
43 2
            'UserCreation.json',
44 2
        );
45
46 2
        $userName = $body['name'];
47
48
        // check if a user with that name already exists
49 2
        $result = $this->userRepository->findOneBy(['name' => $userName]);
50 2
        if ($result !== null) {
51 1
            return $response->withStatus(Http::BAD_REQUEST);
52
        }
53
54
        // Adds user and sets a temporary password
55 1
        $user = $this->userCreator->create(
56 1
            $userName,
57 1
            Password::generate()
58 1
        );
59
60 1
        return $response->withJson(
61 1
            ['result' => $user->getId()]
62 1
        );
63
    }
64
}
65