1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\User; |
6
|
|
|
|
7
|
|
|
use App\Exception\NotFoundException; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Yiisoft\DataResponse\DataResponseFactoryInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @OA\Tag( |
14
|
|
|
* name="user", |
15
|
|
|
* description="Users" |
16
|
|
|
* ) |
17
|
|
|
*/ |
18
|
|
|
final class UserController |
19
|
|
|
{ |
20
|
|
|
private DataResponseFactoryInterface $responseFactory; |
21
|
|
|
private UserRepository $userRepository; |
22
|
|
|
private UserFormatter $userFormatter; |
23
|
|
|
|
24
|
|
|
public function __construct( |
25
|
|
|
DataResponseFactoryInterface $responseFactory, |
26
|
|
|
UserRepository $userRepository, |
27
|
|
|
UserFormatter $userFormatter |
28
|
|
|
) { |
29
|
|
|
$this->responseFactory = $responseFactory; |
30
|
|
|
$this->userRepository = $userRepository; |
31
|
|
|
$this->userFormatter = $userFormatter; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @OA\Get( |
36
|
|
|
* tags={"user"}, |
37
|
|
|
* path="/users", |
38
|
|
|
* summary="Returns paginated users", |
39
|
|
|
* description="", |
40
|
|
|
* @OA\Response(response="200", description="Success") |
41
|
|
|
* ) |
42
|
|
|
*/ |
43
|
|
|
public function index(): ResponseInterface |
44
|
|
|
{ |
45
|
|
|
$dataReader = $this->userRepository->findAllOrderByLogin(); |
46
|
|
|
$result = []; |
47
|
|
|
foreach ($dataReader->read() as $user) { |
48
|
|
|
$result[] = $this->userFormatter->format($user); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->responseFactory->createResponse( |
52
|
|
|
[ |
53
|
|
|
'users' => $result, |
54
|
|
|
] |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @OA\Get( |
60
|
|
|
* tags={"user"}, |
61
|
|
|
* path="/users/{id}", |
62
|
|
|
* summary="Returns a user with a given ID", |
63
|
|
|
* description="", |
64
|
|
|
* @OA\Response(response="200", description="Success") |
65
|
|
|
* ) |
66
|
|
|
*/ |
67
|
|
|
public function view(ServerRequestInterface $request): ResponseInterface |
68
|
|
|
{ |
69
|
|
|
/** |
70
|
|
|
* @var User $user |
71
|
|
|
*/ |
72
|
|
|
$user = $this->userRepository->findByPK($request->getAttribute('id')); |
73
|
|
|
if ($user === null) { |
74
|
|
|
throw new NotFoundException(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->responseFactory->createResponse( |
78
|
|
|
[ |
79
|
|
|
'user' => $this->userFormatter->format($user), |
80
|
|
|
] |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|