1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SolutionDrive\HipchatAPIv2Client\API; |
4
|
|
|
|
5
|
|
|
use SolutionDrive\HipchatAPIv2Client\ClientInterface; |
6
|
|
|
use SolutionDrive\HipchatAPIv2Client\Model\User; |
7
|
|
|
use SolutionDrive\HipchatAPIv2Client\Model\Message; |
8
|
|
|
use SolutionDrive\HipchatAPIv2Client\Model\UserInterface; |
9
|
|
|
|
10
|
|
|
class UserAPI implements UserAPIInterface |
11
|
|
|
{ |
12
|
|
|
/** @var ClientInterface */ |
13
|
|
|
protected $client; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Room api constructor |
17
|
|
|
* |
18
|
|
|
* @param ClientInterface $client that will be used to connect the server |
19
|
|
|
*/ |
20
|
7 |
|
public function __construct(ClientInterface $client) |
21
|
|
|
{ |
22
|
7 |
|
$this->client = $client; |
23
|
7 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
1 |
|
public function getAllUsers($parameters = array()) |
29
|
|
|
{ |
30
|
1 |
|
$response = $this->client->get('/v2/user', $parameters); |
31
|
|
|
|
32
|
1 |
|
$users = array(); |
33
|
1 |
|
foreach ($response['items'] as $response) { |
34
|
1 |
|
$users[] = new User($response); |
35
|
1 |
|
} |
36
|
|
|
|
37
|
1 |
|
return $users; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
1 |
|
public function getUser($userId) |
44
|
|
|
{ |
45
|
1 |
|
$response = $this->client->get(sprintf('/v2/user/%s', $userId)); |
46
|
|
|
|
47
|
1 |
|
return new User($response); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
1 |
|
public function createUser(UserInterface $user, $password) |
54
|
|
|
{ |
55
|
1 |
|
$request = $user->toJson(); |
56
|
1 |
|
$request['password'] = $password; |
57
|
1 |
|
$response = $this->client->post('/v2/user', $request); |
58
|
|
|
|
59
|
1 |
|
return $response['id']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
1 |
|
public function updateUser(UserInterface $user) |
66
|
|
|
{ |
67
|
1 |
|
$request = $user->toJson(); |
68
|
1 |
|
$this->client->put(sprintf('/v2/user/%s', $user->getId()), $request); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
74
|
1 |
|
public function deleteUser($userId) |
75
|
|
|
{ |
76
|
1 |
|
$this->client->delete(sprintf('/v2/user/%s', $userId)); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdoc |
81
|
|
|
*/ |
82
|
1 |
|
public function privateMessageUser($userId, $message) |
83
|
|
|
{ |
84
|
1 |
|
if (is_string($message)) { |
85
|
1 |
|
$content = array('message' => $message); |
86
|
1 |
|
} else { // Assuming its a Message |
87
|
|
|
$content = $message->toJson(); |
88
|
|
|
} |
89
|
1 |
|
$this->client->post(sprintf('/v2/user/%s/message', $userId), $content); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritdoc |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function getRecentPrivateChatHistory($userId, array $parameters = array()) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$response = $this->client->get( |
98
|
|
|
sprintf('/v2/user/%s/history/latest', $userId), |
99
|
|
|
$parameters |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$messages = array(); |
103
|
|
|
foreach ($response['items'] as $response) { |
104
|
|
|
$messages[] = new Message($response); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $messages; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritdoc |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function getPrivateChatMessage($user, $messageId, array $parameters = array()) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$response = $this->client->get( |
116
|
|
|
sprintf('/v2/user/%s/history/%s', $user, $messageId), |
117
|
|
|
$parameters |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$message = new Message($response['message']); |
121
|
|
|
|
122
|
|
|
return $message; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @inheritdoc |
127
|
|
|
*/ |
128
|
|
|
public function getPhoto($userId, $size) |
129
|
|
|
{ |
130
|
|
|
$response = $this->client->get( |
131
|
|
|
sprintf('/v2/user/%s/photo/%s', $userId, $size) |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
return $response; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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.