|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Minepic\Http\Controllers; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Http\JsonResponse; |
|
8
|
|
|
use Illuminate\Http\Response; |
|
9
|
|
|
use Laravel\Lumen\Http\ResponseFactory; |
|
10
|
|
|
use Laravel\Lumen\Routing\Controller as BaseController; |
|
11
|
|
|
use League\Fractal; |
|
12
|
|
|
use League\Fractal\Manager; |
|
13
|
|
|
use League\Fractal\Serializer\ArraySerializer; |
|
14
|
|
|
use Minepic\Exceptions\NotFoundHttpJsonException; |
|
15
|
|
|
use Minepic\Models\Account; |
|
16
|
|
|
use Minepic\Models\AccountStats; |
|
17
|
|
|
use Minepic\Repositories\AccountRepository; |
|
18
|
|
|
use Minepic\Resolvers\UsernameResolver; |
|
19
|
|
|
use Minepic\Resolvers\UuidResolver; |
|
20
|
|
|
use Minepic\Transformers\Account\AccountBasicDataTransformer; |
|
21
|
|
|
use Minepic\Transformers\Account\AccountTypeaheadTransformer; |
|
22
|
|
|
|
|
23
|
|
|
class JsonController extends BaseController |
|
24
|
|
|
{ |
|
25
|
|
|
private const USER_NOT_FOUND_MESSAGE = 'User not found'; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct( |
|
28
|
|
|
private AccountRepository $accountRepository, |
|
29
|
|
|
private UuidResolver $uuidResolver, |
|
30
|
|
|
private Manager $dataManger, |
|
31
|
|
|
private ResponseFactory $responseFactory, |
|
32
|
|
|
private UsernameResolver $usernameResolver |
|
33
|
|
|
) { |
|
34
|
|
|
$this->dataManger->setSerializer(new ArraySerializer()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* User info. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function user(string $uuid = ''): JsonResponse |
|
41
|
|
|
{ |
|
42
|
|
|
if (!$this->uuidResolver->resolve($uuid)) { |
|
43
|
|
|
$httpStatus = 404; |
|
44
|
|
|
$response = [ |
|
45
|
|
|
'ok' => false, |
|
46
|
|
|
'message' => self::USER_NOT_FOUND_MESSAGE, |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
return $this->responseFactory->json($response, $httpStatus); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$httpStatus = 200; |
|
53
|
|
|
$account = $this->uuidResolver->getAccount(); |
|
54
|
|
|
$resource = new Fractal\Resource\Item($account, new AccountBasicDataTransformer()); |
|
55
|
|
|
|
|
56
|
|
|
$response = [ |
|
57
|
|
|
'ok' => true, |
|
58
|
|
|
'data' => $this->dataManger->createData($resource)->toArray(), |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
return $this->responseFactory->json($response, $httpStatus); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @throws \Throwable |
|
66
|
|
|
*/ |
|
67
|
|
|
public function userWithUsername(string $username): JsonResponse |
|
68
|
|
|
{ |
|
69
|
|
|
$uuid = $this->usernameResolver->resolve($username); |
|
70
|
|
|
if ($uuid === null) { |
|
71
|
|
|
throw new NotFoundHttpJsonException(self::USER_NOT_FOUND_MESSAGE); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->user($uuid); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Update User data. |
|
79
|
|
|
* |
|
80
|
|
|
* @throws \Throwable |
|
81
|
|
|
*/ |
|
82
|
|
|
public function updateUser(string $uuid): JsonResponse |
|
83
|
|
|
{ |
|
84
|
|
|
// Force user update |
|
85
|
|
|
$this->uuidResolver->setForceUpdate(true); |
|
86
|
|
|
|
|
87
|
|
|
// Check if user exists |
|
88
|
|
|
if (!$this->uuidResolver->resolve($uuid)) { |
|
89
|
|
|
return $this->responseFactory->json( |
|
90
|
|
|
['ok' => false, 'message' => self::USER_NOT_FOUND_MESSAGE], |
|
91
|
|
|
Response::HTTP_NOT_FOUND |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// Check if data has been updated |
|
96
|
|
|
if ($this->uuidResolver->userDataUpdated()) { |
|
97
|
|
|
return $this->responseFactory->json( |
|
98
|
|
|
['ok' => true, 'message' => 'Data updated'], |
|
99
|
|
|
Response::HTTP_OK |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$userdata = $this->uuidResolver->getAccount(); |
|
104
|
|
|
$dateString = $userdata->updated_at->toW3cString(); |
|
105
|
|
|
|
|
106
|
|
|
$response = [ |
|
107
|
|
|
'ok' => false, |
|
108
|
|
|
'message' => 'Cannot update user, userdata has been updated recently', |
|
109
|
|
|
'last_update' => $dateString, |
|
110
|
|
|
]; |
|
111
|
|
|
|
|
112
|
|
|
return $this->responseFactory->json($response, Response::HTTP_FORBIDDEN); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Username Typeahead. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function userTypeahead(string $username): JsonResponse |
|
119
|
|
|
{ |
|
120
|
|
|
$accountsPagination = $this->accountRepository->filterPaginate(['term' => $username], 15); |
|
121
|
|
|
|
|
122
|
|
|
$resource = new Fractal\Resource\Collection( |
|
123
|
|
|
$accountsPagination->items(), |
|
124
|
|
|
new AccountTypeaheadTransformer() |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
return $this->responseFactory->json( |
|
128
|
|
|
$this->dataManger->createData($resource)->toArray() |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Get most wanted account list. |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getMostWantedUsers(): JsonResponse |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->responseFactory->json([ |
|
138
|
|
|
'ok' => true, |
|
139
|
|
|
'data' => AccountStats::getMostWanted(), |
|
140
|
|
|
]); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Check system status |
|
145
|
|
|
*/ |
|
146
|
|
|
public function check(): JsonResponse |
|
147
|
|
|
{ |
|
148
|
|
|
$account = Account::whereUuid('be1cac3b60f04e0dba12c77cc8e0ec21') |
|
149
|
|
|
->take(1) |
|
150
|
|
|
->first(); |
|
151
|
|
|
|
|
152
|
|
|
return $this->responseFactory->json([ |
|
153
|
|
|
'ok' => $account instanceof Account, |
|
154
|
|
|
]); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|