1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Entity\User; |
6
|
|
|
use App\Repository\UserRepository; |
7
|
|
|
use App\Request\User\RegisterUserRequest; |
8
|
|
|
use App\Service\User\RegisterService; |
9
|
|
|
use FOS\RestBundle\Controller\FOSRestController; |
10
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
11
|
|
|
use Swagger\Annotations as SWG; |
12
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
13
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model; |
14
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
15
|
|
|
|
16
|
|
|
class UserController extends FOSRestController |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var RegisterService |
20
|
|
|
*/ |
21
|
|
|
private $registerService; |
22
|
|
|
|
23
|
|
|
private $translator; |
24
|
|
|
|
25
|
4 |
|
public function __construct(RegisterService $registerService, TranslatorInterface $translator) |
26
|
|
|
{ |
27
|
4 |
|
$this->registerService = $registerService; |
28
|
4 |
|
$this->translator = $translator; |
29
|
4 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Registration |
33
|
|
|
* |
34
|
|
|
* @Route("/api/users", methods={"POST"}) |
35
|
|
|
* @SWG\Parameter(name="registration.username", in="formData", type="string") |
36
|
|
|
* @SWG\Parameter(name="registration.password", in="formData", type="string") |
37
|
|
|
* @SWG\Parameter(name="registration.email", in="formData", type="string") |
38
|
|
|
* @SWG\Response( |
39
|
|
|
* description="Registration.", |
40
|
|
|
* response=202, |
41
|
|
|
* @Model(type=User::class) |
42
|
|
|
* ) |
43
|
|
|
* @param $request RegisterUserRequest |
44
|
|
|
* @return User |
45
|
|
|
*/ |
46
|
1 |
|
public function postUsers(RegisterUserRequest $request) |
47
|
|
|
{ |
48
|
1 |
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_ANONYMOUSLY'); |
49
|
|
|
|
50
|
1 |
|
return $this->registerService->registerByRequest($request); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get single user |
55
|
|
|
* |
56
|
|
|
* @Route("/api/users/{id}", methods={"GET"}) |
57
|
|
|
* @SWG\Response( |
58
|
|
|
* description="REST action which returns user by id.", |
59
|
|
|
* response=200, |
60
|
|
|
* @Model(type=User::class) |
61
|
|
|
* ) |
62
|
|
|
* |
63
|
|
|
* @param int $id |
64
|
|
|
* @return User |
65
|
|
|
*/ |
66
|
|
|
public function getUsers($id) |
67
|
|
|
{ |
68
|
|
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var $userRepository UserRepository |
72
|
|
|
*/ |
73
|
|
|
$userRepository = $this->getDoctrine()->getRepository(User::class); |
74
|
|
|
$user = $userRepository->find($id); |
75
|
|
|
|
76
|
|
|
if ($user === null) { |
77
|
|
|
throw new NotFoundHttpException($this->translator->trans('not_found_by_id', [ |
78
|
|
|
'user_id' => $id, |
79
|
|
|
], 'users')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $user; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get all users |
87
|
|
|
* |
88
|
|
|
* @Route("/api/users", methods={"GET"}) |
89
|
|
|
* @SWG\Response( |
90
|
|
|
* description="REST action which returns user by id.", |
91
|
|
|
* response=201, |
92
|
|
|
* @SWG\Schema( |
93
|
|
|
* type="array", |
94
|
|
|
* @SWG\Items(ref=@Model(type=User::class, groups={"full"})) |
95
|
|
|
* ) |
96
|
|
|
* ) |
97
|
|
|
* |
98
|
|
|
* @return User[] |
99
|
|
|
*/ |
100
|
2 |
|
public function getAll() |
101
|
|
|
{ |
102
|
2 |
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var $userRepository UserRepository |
106
|
|
|
*/ |
107
|
1 |
|
$userRepository = $this->getDoctrine()->getRepository(User::class); |
108
|
1 |
|
$users = $userRepository->findAll(); |
109
|
|
|
|
110
|
1 |
|
return $users; |
111
|
|
|
} |
112
|
|
|
} |