UserAPI::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace GorkaLaucirica\HipchatAPIv2Client\API;
4
5
use GorkaLaucirica\HipchatAPIv2Client\Client;
6
use GorkaLaucirica\HipchatAPIv2Client\Model\User;
7
use GorkaLaucirica\HipchatAPIv2Client\Model\Message;
8
9
class UserAPI
10
{
11
    /** @var Client */
12
    protected $client;
13
14
    /**
15
     * Room api constructor
16
     *
17
     * @param Client $client that will be used to connect the server
18
     */
19
    public function __construct(Client $client)
20
    {
21
        $this->client = $client;
22
    }
23
24
    /**
25
     * List all users in the group
26
     * More info: https://www.hipchat.com/docs/apiv2/method/get_all_users
27
     *
28
     * @param array $parameters The following are accepted: start-index, max-results, include-guests, include-deleted
29
     *
30
     * @return array of Users
31
     */
32
    public function getAllUsers($parameters = array())
33
    {
34
        $response = $this->client->get('/v2/user', $parameters);
35
36
        $users = array();
37
        foreach ($response['items'] as $response) {
38
            $users[] = new User($response);
39
        }
40
41
        return $users;
42
    }
43
44
    /**
45
     * Gets user by id, email or mention name
46
     * More info: https://www.hipchat.com/docs/apiv2/method/view_user
47
     *
48
     * @param string $userId The id, email address, or mention name (beginning with an '@') of the user to view
49
     *
50
     * @return User
51
     */
52
    public function getUser($userId)
53
    {
54
        $response = $this->client->get(sprintf('/v2/user/%s', $userId));
55
56
        return new User($response);
57
    }
58
59
    /**
60
     * Creates a new user
61
     * More info: https://www.hipchat.com/docs/apiv2/method/create_user
62
     *
63
     * @param User   $user     User to be created
64
     * @param string $password User's password
65
     *
66
     * @return mixed
67
     */
68
    public function createUser(User $user, $password)
69
    {
70
        $request = $user->toJson();
71
        $request['password'] = $password;
72
        $response = $this->client->post('/v2/user', $request);
73
74
        return $response['id'];
75
    }
76
77
    /**
78
     * Update a user
79
     * More info: https://www.hipchat.com/docs/apiv2/method/update_user
80
     *
81
     * @param User $user User to be updated
82
     */
83
    public function updateUser(User $user)
84
    {
85
        $request = $user->toJson();
86
        $this->client->put(sprintf('/v2/user/%s', $user->getId()), $request);
87
    }
88
89
    /**
90
     * Delete a user.
91
     *
92
     * @param string $userId The id, email address, or mention name (beginning with an '@') of the user to delete
93
     */
94
    public function deleteUser($userId)
95
    {
96
        $this->client->delete(sprintf('/v2/user/%s', $userId));
97
    }
98
99
    /**
100
     * Sends a user a private message
101
     * More info: https://www.hipchat.com/docs/apiv2/method/private_message_user
102
     *
103
     * @param string $userId  The id, email address, or mention name (beginning with an '@') of the user to send a message to
104
     * @param mixed  $message The message to send as plain text
105
     */
106
    public function privateMessageUser($userId, $message)
107
    {
108
        if (is_string($message)) {
109
            $content = array('message' => $message);
110
        } else { // Assuming its a Message
111
            $content = $message->toJson();
112
        }
113
        $this->client->post(sprintf('/v2/user/%s/message', $userId), $content);
114
    }
115
116
    /**
117
     * Fetch latest chat history for the 1:1 chat with the user
118
     * More info: https://www.hipchat.com/docs/apiv2/method/view_recent_privatechat_history
119
     *
120
     * @param string $userId     The id, email address, or mention name (beginning with an '@') of the user
121
     * @param mixed  $parameters Optional parameters, check above documentation for more info
122
     *
123
     * @return array Message
124
     */
125 View Code Duplication
    public function getRecentPrivateChatHistory($userId, array $parameters = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
126
    {
127
        $response = $this->client->get(
128
            sprintf('/v2/user/%s/history/latest', $userId),
129
            $parameters
130
        );
131
132
        $messages = array();
133
        foreach ($response['items'] as $response) {
134
            $messages[] = new Message($response);
135
        }
136
137
        return $messages;
138
    }
139
140
    /**
141
     * Fetch one specific message by id
142
     * More info: https://www.hipchat.com/docs/apiv2/method/get_privatechat_message
143
     *
144
     * @param string $user       The id, email address, or mention name (beginning with an '@') of the user
145
     * @param string $messageId  The id of the message to retrieve
146
     * @param array  $parameters Optional parameters, check above documentation for more info
147
     *
148
     * @return Message
149
     */
150 View Code Duplication
    public function getPrivateChatMessage($user, $messageId, array $parameters = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
151
    {
152
        $response = $this->client->get(
153
            sprintf('/v2/user/%s/history/%s', $user, $messageId),
154
            $parameters
155
        );
156
157
        $message = new Message($response['message']);
158
159
        return $message;
160
    }
161
162
    /**
163
     * Gets a user photo
164
     * More info: https://www.hipchat.com/docs/apiv2/method/get_photo
165
     *
166
     * @param string $userId The id, email address, or mention name (beginning with an '@') of the user
167
     * @param string $size   The size to retrieve ("small" or "big")
168
     *
169
     * @return string
170
     */
171
    public function getPhoto($userId, $size)
172
    {
173
        $response = $this->client->get(
174
            sprintf('/v2/user/%s/photo/%s', $userId, $size)
175
        );
176
177
        return $response;
178
    }
179
}
180