|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service\Api; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
|
6
|
|
|
use JMS\Serializer\DeserializationContext; |
|
7
|
|
|
use JMS\Serializer\Serializer; |
|
8
|
|
|
use Psr\Log\LoggerInterface; |
|
9
|
|
|
use Skobkin\Bundle\PointToolsBundle\DTO\Api\Auth; |
|
10
|
|
|
use Skobkin\Bundle\PointToolsBundle\DTO\Api\User as UserDTO; |
|
11
|
|
|
use Skobkin\Bundle\PointToolsBundle\Entity\User; |
|
12
|
|
|
use Skobkin\Bundle\PointToolsBundle\Exception\Api\ForbiddenException; |
|
13
|
|
|
use Skobkin\Bundle\PointToolsBundle\Exception\Api\InvalidResponseException; |
|
14
|
|
|
use Skobkin\Bundle\PointToolsBundle\Exception\Api\NotFoundException; |
|
15
|
|
|
use Skobkin\Bundle\PointToolsBundle\Exception\Api\UserNotFoundException; |
|
16
|
|
|
use Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Basic Point.im user API functions from /api/user/* |
|
20
|
|
|
*/ |
|
21
|
|
|
class UserApi extends AbstractApi |
|
22
|
|
|
{ |
|
23
|
|
|
private const PREFIX = '/api/user/'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var UserFactory |
|
27
|
|
|
*/ |
|
28
|
|
|
private $userFactory; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(ClientInterface $httpClient, Serializer $serializer, LoggerInterface $logger, UserFactory $userFactory) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct($httpClient, $serializer, $logger); |
|
33
|
|
|
|
|
34
|
|
|
$this->userFactory = $userFactory; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function isLoginAndPasswordValid(string $login, string $password): bool |
|
38
|
|
|
{ |
|
39
|
|
|
$this->logger->info('Checking user auth data via point.im API'); |
|
40
|
|
|
|
|
41
|
|
|
$auth = $this->authenticate($login, $password); |
|
42
|
|
|
|
|
43
|
|
|
if ($auth->isValid()) { |
|
44
|
|
|
$this->logger->debug('Authentication successfull. Logging out.'); |
|
45
|
|
|
|
|
46
|
|
|
$this->logout($auth); |
|
47
|
|
|
|
|
48
|
|
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function authenticate(string $login, string $password): Auth |
|
55
|
|
|
{ |
|
56
|
|
|
$this->logger->debug('Trying to authenticate user via Point.im API', ['login' => $login]); |
|
57
|
|
|
|
|
58
|
|
|
try { |
|
59
|
|
|
return $this->getPostJsonData( |
|
60
|
|
|
'/api/login', |
|
61
|
|
|
[ |
|
62
|
|
|
'login' => $login, |
|
63
|
|
|
'password' => $password, |
|
64
|
|
|
], |
|
65
|
|
|
Auth::class |
|
66
|
|
|
); |
|
67
|
|
|
} catch (NotFoundException $e) { |
|
68
|
|
|
throw new InvalidResponseException('API method not found', 0, $e); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @throws InvalidResponseException |
|
74
|
|
|
*/ |
|
75
|
|
|
public function logout(Auth $auth): bool |
|
76
|
|
|
{ |
|
77
|
|
|
$this->logger->debug('Trying to log user out via Point.im API'); |
|
78
|
|
|
|
|
79
|
|
|
try { |
|
80
|
|
|
$this->getPostResponseBody('/api/logout', ['csrf_token' => $auth->getCsRfToken()]); |
|
81
|
|
|
|
|
82
|
|
|
return true; |
|
83
|
|
|
} catch (NotFoundException $e) { |
|
84
|
|
|
throw new InvalidResponseException('API method not found', 0, $e); |
|
85
|
|
|
} catch (ForbiddenException $e) { |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get user subscribers by user login |
|
92
|
|
|
* |
|
93
|
|
|
* @return User[] |
|
94
|
|
|
* |
|
95
|
|
|
* @throws UserNotFoundException |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getUserSubscribersByLogin(string $login): array |
|
98
|
|
|
{ |
|
99
|
|
|
$this->logger->debug('Trying to get user subscribers by login', ['login' => $login]); |
|
100
|
|
|
|
|
101
|
|
|
try { |
|
102
|
|
|
$usersList = $this->getGetJsonData( |
|
103
|
|
|
self::PREFIX.urlencode($login).'/subscribers', |
|
104
|
|
|
[], |
|
105
|
|
|
'array<'.UserDTO::class.'>', |
|
106
|
|
|
DeserializationContext::create()->setGroups(['user_short']) |
|
107
|
|
|
); |
|
108
|
|
|
} catch (NotFoundException $e) { |
|
109
|
|
|
throw new UserNotFoundException('User not found', 0, $e, null, $login); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $this->userFactory->findOrCreateFromDTOArray($usersList); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get user subscribers by user id |
|
117
|
|
|
* |
|
118
|
|
|
* @return User[] |
|
119
|
|
|
* |
|
120
|
|
|
* @throws UserNotFoundException |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getUserSubscribersById(int $id): array |
|
123
|
|
|
{ |
|
124
|
|
|
$this->logger->debug('Trying to get user subscribers by id', ['id' => $id]); |
|
125
|
|
|
|
|
126
|
|
|
try { |
|
127
|
|
|
$usersList = $this->getGetJsonData( |
|
128
|
|
|
self::PREFIX.'id/'.$id.'/subscribers', |
|
129
|
|
|
[], |
|
130
|
|
|
'array<'.UserDTO::class.'>', |
|
131
|
|
|
DeserializationContext::create()->setGroups(['user_short']) |
|
132
|
|
|
); |
|
133
|
|
|
} catch (NotFoundException $e) { |
|
134
|
|
|
throw new UserNotFoundException('User not found', 0, $e, $id); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $this->userFactory->findOrCreateFromDTOArray($usersList); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Get full user info by login |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getUserByLogin(string $login): User |
|
144
|
|
|
{ |
|
145
|
|
|
$this->logger->debug('Trying to get user by login', ['login' => $login]); |
|
146
|
|
|
|
|
147
|
|
|
try { |
|
148
|
|
|
/** @var UserDTO $userInfo */ |
|
149
|
|
|
$userInfo = $this->getGetJsonData( |
|
150
|
|
|
self::PREFIX.'login/'.urlencode($login), |
|
151
|
|
|
[], |
|
152
|
|
|
UserDTO::class, |
|
153
|
|
|
DeserializationContext::create()->setGroups(['user_full']) |
|
154
|
|
|
); |
|
155
|
|
|
} catch (NotFoundException $e) { |
|
156
|
|
|
throw new UserNotFoundException('User not found', 0, $e, null, $login); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $this->userFactory->findOrCreateFromDTO($userInfo); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get full user info by id |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getUserById(int $id): User |
|
166
|
|
|
{ |
|
167
|
|
|
$this->logger->debug('Trying to get user by id', ['id' => $id]); |
|
168
|
|
|
|
|
169
|
|
|
try { |
|
170
|
|
|
/** @var UserDTO $userData */ |
|
171
|
|
|
$userData = $this->getGetJsonData( |
|
172
|
|
|
self::PREFIX.'id/'.$id, |
|
173
|
|
|
[], |
|
174
|
|
|
UserDTO::class, |
|
175
|
|
|
DeserializationContext::create()->setGroups(['user_full']) |
|
176
|
|
|
); |
|
177
|
|
|
} catch (NotFoundException $e) { |
|
178
|
|
|
throw new UserNotFoundException('User not found', 0, $e, $id); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $this->userFactory->findOrCreateFromDTO($userData); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|