Passed
Push — main ( f1ae37...870f33 )
by Daniel
04:25
created

UserRetrieveApplication   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A serve() 0 14 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;
10
use Uxmp\Core\Api\Lib\ResultItemFactoryInterface;
11
use Uxmp\Core\Component\User\PrivilegeCheckerInterface;
12
use Uxmp\Core\Orm\Repository\UserRepositoryInterface;
13
14
/**
15
 * User retrieval
16
 */
17
final class UserRetrieveApplication extends AbstractSettingsApplication
18
{
19 2
    public function __construct(
20
        private readonly UserRepositoryInterface $userRepository,
21
        PrivilegeCheckerInterface $privilegeChecker,
22
        private readonly ResultItemFactoryInterface $resultItemFactory,
23
    ) {
24 2
        parent::__construct(
25
            $privilegeChecker
26
        );
27
    }
28
29
    /**
30
     * @param array<string, scalar> $args
31
     */
32 2
    protected function serve(
33
        ServerRequestInterface $request,
34
        ResponseInterface $response,
35
        array $args
36
    ): ResponseInterface {
37 2
        $user = $this->userRepository->find((int) ($args['userId'] ?? 0));
38
39 2
        if ($user === null) {
40 1
            return $response->withStatus(StatusCode::NOT_FOUND);
41
        }
42
43 1
        return $this->asJson(
44
            $response,
45 1
            ['data' => $this->resultItemFactory->createUserListItem($user)]
46
        );
47
    }
48
}
49