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

UserRetrieveApplication::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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