|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\User; |
|
6
|
|
|
|
|
7
|
|
|
use App\Exception\NotFoundException; |
|
8
|
|
|
use App\RestControllerTrait; |
|
9
|
|
|
use OpenApi\Annotations as OA; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Yiisoft\DataResponse\DataResponseFactoryInterface; |
|
12
|
|
|
use Yiisoft\RequestModel\Attribute\Route; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @OA\Tag( |
|
16
|
|
|
* name="user", |
|
17
|
|
|
* description="Users" |
|
18
|
|
|
* ) |
|
19
|
|
|
*/ |
|
20
|
|
|
final class UserController |
|
21
|
|
|
{ |
|
22
|
|
|
use RestControllerTrait; |
|
23
|
|
|
|
|
24
|
|
|
private DataResponseFactoryInterface $responseFactory; |
|
25
|
|
|
private UserRepository $userRepository; |
|
26
|
|
|
private UserFormatter $userFormatter; |
|
27
|
|
|
|
|
28
|
4 |
|
public function __construct( |
|
29
|
|
|
DataResponseFactoryInterface $responseFactory, |
|
30
|
|
|
UserRepository $userRepository, |
|
31
|
|
|
UserFormatter $userFormatter |
|
32
|
|
|
) { |
|
33
|
4 |
|
$this->responseFactory = $responseFactory; |
|
34
|
4 |
|
$this->userRepository = $userRepository; |
|
35
|
4 |
|
$this->userFormatter = $userFormatter; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @OA\Get( |
|
40
|
|
|
* tags={"user"}, |
|
41
|
|
|
* path="/users", |
|
42
|
|
|
* summary="Returns paginated users", |
|
43
|
|
|
* description="", |
|
44
|
|
|
* security={{"ApiKey": {}}}, |
|
45
|
|
|
* @OA\Response( |
|
46
|
|
|
* response="200", |
|
47
|
|
|
* description="Success", |
|
48
|
|
|
* @OA\JsonContent( |
|
49
|
|
|
* allOf={ |
|
50
|
|
|
* @OA\Schema(ref="#/components/schemas/Response"), |
|
51
|
|
|
* @OA\Schema( |
|
52
|
|
|
* @OA\Property( |
|
53
|
|
|
* property="data", |
|
54
|
|
|
* type="object", |
|
55
|
|
|
* @OA\Property( |
|
56
|
|
|
* property="users", |
|
57
|
|
|
* type="array", |
|
58
|
|
|
* @OA\Items(ref="#/components/schemas/User") |
|
59
|
|
|
* ), |
|
60
|
|
|
* ), |
|
61
|
|
|
* ), |
|
62
|
|
|
* }, |
|
63
|
|
|
* ) |
|
64
|
|
|
* ), |
|
65
|
|
|
* ) |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function list(): ResponseInterface |
|
68
|
|
|
{ |
|
69
|
1 |
|
$dataReader = $this->userRepository->findAllOrderByLogin(); |
|
70
|
1 |
|
$result = []; |
|
71
|
1 |
|
foreach ($dataReader->read() as $user) { |
|
72
|
1 |
|
$result[] = $this->userFormatter->format($user); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
return $this->responseFactory->createResponse( |
|
76
|
|
|
[ |
|
77
|
1 |
|
'users' => $result, |
|
78
|
|
|
] |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @OA\Get( |
|
84
|
|
|
* tags={"user"}, |
|
85
|
|
|
* path="/users/{id}", |
|
86
|
|
|
* summary="Returns a user with a given ID", |
|
87
|
|
|
* description="", |
|
88
|
|
|
* security={{"ApiKey": {}}}, |
|
89
|
|
|
* @OA\Parameter( |
|
90
|
|
|
* @OA\Schema(type="int", example="2"), |
|
91
|
|
|
* in="path", |
|
92
|
|
|
* name="id", |
|
93
|
|
|
* parameter="id" |
|
94
|
|
|
* ), |
|
95
|
|
|
* @OA\Response( |
|
96
|
|
|
* response="200", |
|
97
|
|
|
* description="Success", |
|
98
|
|
|
* @OA\JsonContent( |
|
99
|
|
|
* allOf={ |
|
100
|
|
|
* @OA\Schema(ref="#/components/schemas/Response"), |
|
101
|
|
|
* @OA\Schema( |
|
102
|
|
|
* @OA\Property( |
|
103
|
|
|
* property="data", |
|
104
|
|
|
* type="object", |
|
105
|
|
|
* @OA\Property( |
|
106
|
|
|
* property="user", |
|
107
|
|
|
* type="object", |
|
108
|
|
|
* ref="#/components/schemas/User" |
|
109
|
|
|
* ), |
|
110
|
|
|
* ), |
|
111
|
|
|
* ), |
|
112
|
|
|
* }, |
|
113
|
|
|
* ) |
|
114
|
|
|
* ), |
|
115
|
|
|
* ) |
|
116
|
|
|
*/ |
|
117
|
2 |
|
public function get(#[Route('id')] int $id): ResponseInterface |
|
118
|
|
|
{ |
|
119
|
|
|
/** |
|
120
|
|
|
* @var User $user |
|
121
|
|
|
*/ |
|
122
|
2 |
|
$user = $this->userRepository->findByPK($id); |
|
123
|
2 |
|
if ($user === null) { |
|
124
|
1 |
|
throw new NotFoundException(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
return $this->responseFactory->createResponse( |
|
128
|
|
|
[ |
|
129
|
1 |
|
'user' => $this->userFormatter->format($user), |
|
130
|
|
|
] |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|