1
|
|
|
<?php |
2
|
|
|
namespace Gvera\Controllers; |
3
|
|
|
|
4
|
|
|
use Gvera\Exceptions\NotFoundException; |
5
|
|
|
use Gvera\Exceptions\NotImplementedMethodException; |
6
|
|
|
use Gvera\Helpers\http\JSONResponse; |
7
|
|
|
use Gvera\Helpers\http\Response; |
8
|
|
|
use Gvera\Helpers\http\TransformerResponse; |
9
|
|
|
use Gvera\Helpers\locale\Locale; |
10
|
|
|
use Gvera\Helpers\transformers\UserTransformer; |
11
|
|
|
use Gvera\Models\User; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @OA\Info(title="My First API", version="0.1") |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Controller Class Doc Comment |
19
|
|
|
* |
20
|
|
|
* @category Class |
21
|
|
|
* @package src/controllers |
22
|
|
|
* @author Guido Vera |
23
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License |
24
|
|
|
* @link http://www.github.com/veraguido/gv |
25
|
|
|
* @method getEntityManager() |
26
|
|
|
*/ |
27
|
|
|
class Examples extends GvController |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @OA\Get( |
31
|
|
|
* path="/examples", |
32
|
|
|
* @OA\Response(response="200", description="An example resource") |
33
|
|
|
* ) |
34
|
|
|
*/ |
35
|
|
|
public function index() |
36
|
|
|
{ |
37
|
|
|
$this->viewParams = ['csrf' => $this->generateCSRFToken()]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @throws NotFoundException |
42
|
|
|
* @throws \ReflectionException |
43
|
|
|
*/ |
44
|
|
|
public function login() |
45
|
|
|
{ |
46
|
|
|
$this->httpRequest->validate(); |
47
|
|
|
$loginCommand = $this->getLoginCommand(); |
48
|
|
|
$loginCommand->setUsername($this->httpRequest->getParameter('username')); |
49
|
|
|
$loginCommand->setPassword($this->httpRequest->getParameter('password')); |
50
|
|
|
|
51
|
|
|
$loginCommand->execute(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function logout() |
55
|
|
|
{ |
56
|
|
|
$userService = $this->getUserService(); |
57
|
|
|
$userService->logout(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function asd() |
61
|
|
|
{ |
62
|
|
|
$this->httpResponse->response(new Response("trough routes.yml")); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
67
|
|
|
* @throws \Exception |
68
|
|
|
* @httpMethod("POST") |
69
|
|
|
*/ |
70
|
|
|
public function qwe() |
71
|
|
|
{ |
72
|
|
|
$this->validateCSRFToken($this->httpRequest->getParameter('csrf')); |
73
|
|
|
$registerUserCommand = $this->getCreateNewUserCommand(); |
74
|
|
|
$registerUserCommand |
75
|
|
|
->setName($this->httpRequest->getParameter('username')) |
76
|
|
|
->setEmail($this->httpRequest->getParameter('email')) |
77
|
|
|
->setPassword($this->httpRequest->getParameter('password')); |
78
|
|
|
|
79
|
|
|
$registerUserCommand->execute(); |
80
|
|
|
|
81
|
|
|
$file = $this->httpRequest->getFileByPropertyName("avatar-pic"); |
82
|
|
|
|
83
|
|
|
$this->httpRequest->moveFileToDirectory("/tmp/", $file); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function lorep() |
87
|
|
|
{ |
88
|
|
|
$this->httpResponse->response(Locale::getLocale("Hello world")); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function authorization() |
92
|
|
|
{ |
93
|
|
|
$user = $this->getEntityManager()->getRepository(User::class)->findOneBy(['username' => 'mod']); |
94
|
|
|
$service = $this->getUserService(); |
95
|
|
|
$this->httpResponse->response(new JSONResponse(['can all' => $service->userCan($user, 'all')])); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @throws NotImplementedMethodException |
100
|
|
|
*/ |
101
|
|
|
public function transformer() |
102
|
|
|
{ |
103
|
|
|
$entityManager = $this->getEntityManager(); |
104
|
|
|
$repository = $entityManager->getRepository(User::class); |
105
|
|
|
$user = $repository->findOneBy(['username' => 'asda']); |
106
|
|
|
|
107
|
|
|
$this->httpResponse->response(new TransformerResponse(new UserTransformer($user))); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
View Code Duplication |
public function basicAuth() |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
try { |
113
|
|
|
$this->mustPassBasicAuthentication(); |
114
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
115
|
|
|
$content = ['message' => $e->getMessage()]; |
116
|
|
|
$this->httpResponse->response(new JSONResponse($content, Response::HTTP_RESPONSE_UNAUTHORIZED)); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function jwt() |
121
|
|
|
{ |
122
|
|
|
try { |
123
|
|
|
$this->mustPassTokenAuthentication(); |
124
|
|
|
} catch (\Exception $e) { |
125
|
|
|
$content = ['message' => $e->getMessage()]; |
126
|
|
|
$this->httpResponse->response(new JSONResponse($content, Response::HTTP_RESPONSE_UNAUTHORIZED)); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.