Passed
Pull Request — master (#87)
by Dmitriy
03:13
created

Action::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 14
ccs 5
cts 5
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Infrastructure\IO\Http\User\GetView;
6
7
use App\Application\Exception\NotFoundException;
8
use App\Application\User\Entity\User;
9
use App\Application\User\Entity\UserRepository;
10
use App\Infrastructure\IO\Http\User\GetView\Response\ResponseFactory;
11
use OpenApi\Annotations as OA;
12
use Psr\Http\Message\ResponseInterface;
13
use Yiisoft\Router\CurrentRoute;
14
15
/**
16
 * @OA\Tag(
17
 *     name="user",
18
 *     description="Users"
19
 * )
20
 *
21
 * @OA\Get(
22
 *     tags={"user"},
23
 *     path="/users/{id}",
24
 *     summary="Returns a user with a given ID",
25
 *     description="",
26
 *     security={{"ApiKey": {}}},
27
 *     @OA\Parameter(
28
 *          @OA\Schema(type="int", example="2"),
29
 *          in="path",
30
 *          name="id",
31
 *          parameter="id"
32
 *     ),
33
 *     @OA\Response(
34
 *          response="200",
35
 *          description="Success",
36
 *          @OA\JsonContent(
37
 *              allOf={
38
 *                  @OA\Schema(ref="#/components/schemas/Response"),
39
 *                  @OA\Schema(
40
 *                      @OA\Property(
41
 *                          property="data",
42
 *                          type="object",
43
 *                          @OA\Property(
44
 *                              property="user",
45
 *                              type="object",
46
 *                              ref="#/components/schemas/UserViewResponse"
47
 *                          ),
48
 *                      ),
49
 *                  ),
50
 *              },
51
 *          )
52
 *    ),
53
 * )
54
 */
55
final class Action
56
{
57 2
    public function __invoke(
58
        CurrentRoute $currentRoute,
59
        UserRepository $userRepository,
60
        ResponseFactory $responseFactory,
61
    ): ResponseInterface {
62
        /**
63
         * @var User $user
64
         */
65 2
        $user = $userRepository->findByPK($currentRoute->getArgument('id'));
66 2
        if ($user === null) {
67 1
            throw new NotFoundException();
68
        }
69
70 1
        return $responseFactory->create($user);
71
    }
72
}
73