Test Failed
Push — master ( 99762a...27f9c8 )
by Kirill
02:27
created

Messages::allBeforeId()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 11

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 8
nop 3
1
<?php
2
/**
3
 * This file is part of GitterApi package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace Gitter\Resources;
10
11
use Gitter\Route;
12
13
/**
14
 * Messages represent individual chat messages sent to a room. They are a sub-resource of a room.
15
 * Message schema:
16
 *  - id:           ID of the message.
17
 *  - text:         Original message in plain-text/markdown.
18
 *  - html:         HTML formatted message.
19
 *  - sent:         ISO formatted date of the message.
20
 *  - editedAt:     ISO formatted date of the message if edited.
21
 *  - fromUser:     (User)[user-resource] that sent the message.
22
 *  - unread:       Boolean that indicates if the current user has read the message.
23
 *  - readBy:       Number of users that have read the message.
24
 *  - urls:         List of URLs present in the message.
25
 *  - mentions:     List of @Mentions in the message.
26
 *  - issues:       List of #Issues referenced in the message.
27
 *  - meta:         Metadata. This is currently not used for anything.
28
 *  - v:            Version.
29
 *  - gv:           Stands for "Gravatar version" and is used for cache busting.
30
 * @package Gitter\Resources
31
 */
32
class Messages extends AbstractResource
33
{
34
    /**
35
     * List of messages in a room in historical reversed order.
36
     *
37
     * @param string $roomId Room id
38
     * @param string|null $query Optional search query
39
     * @return \Generator
40
     * @throws \RuntimeException
41
     * @throws \InvalidArgumentException
42
     * @throws \Throwable
43
     * @throws \Exception
44
     * @throws \GuzzleHttp\Exception\ClientException
45
     */
46
    public function all(string $roomId, string $query = null): \Generator
47
    {
48
        yield from $this->allBeforeId($roomId, null, $query);
49
    }
50
51
    /**
52
     * Returns all messages before target message id.
53
     *
54
     * @param string $roomId
55
     * @param string|null $beforeId
56
     * @param string|null $query
57
     * @return \Generator
58
     * @throws \Exception
59
     * @throws \GuzzleHttp\Exception\ClientException
60
     * @throws \InvalidArgumentException
61
     * @throws \RuntimeException
62
     * @throws \Throwable
63
     */
64 View Code Duplication
    public function allBeforeId(string $roomId, string $beforeId = null, string $query = null)
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...
65
    {
66
        $limit = 100;
67
        $route = $this->routeForMessagesIterator($roomId, $limit, $query);
68
69
        do {
70
            if ($beforeId !== null) {
71
                $route->with('beforeId', $beforeId);
72
            }
73
74
            $response = array_reverse($this->fetch($route));
75
76
            foreach ($response as $message) {
77
                yield $message;
78
            }
79
80
            $beforeId = count($response) > 0 ? end($response)['id'] : null;
81
        } while (count($response) >= $limit);
82
    }
83
84
    /**
85
     * @param string $roomId
86
     * @param int $limit
87
     * @param string|null $query
88
     * @return Route
89
     */
90
    private function routeForMessagesIterator(string $roomId, int $limit, string $query = null): Route
91
    {
92
        $route = Route::get('rooms/{roomId}/chatMessages')
93
            ->with('roomId', $roomId)
94
            ->with('limit', (string)$limit);
95
96
        if ($query !== null) {
97
            $route->with('q', $query);
98
        }
99
100
        return $route;
101
    }
102
103
    /**
104
     * Returns all messages after target message id.
105
     *
106
     * @param string $roomId
107
     * @param string|null $afterId
108
     * @param string|null $query
109
     * @return \Generator
110
     * @throws \Exception
111
     * @throws \GuzzleHttp\Exception\ClientException
112
     * @throws \InvalidArgumentException
113
     * @throws \RuntimeException
114
     * @throws \Throwable
115
     */
116 View Code Duplication
    public function allAfterId(string $roomId, string $afterId = null, string $query = null)
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...
117
    {
118
        $limit = 100;
119
        $route = $this->routeForMessagesIterator($roomId, $limit, $query);
120
121
        do {
122
            if ($afterId !== null) {
123
                $route->with('afterId', $afterId);
124
            }
125
126
            $response = (array)$this->fetch($route);
127
128
            foreach ($response as $message) {
129
                yield $message;
130
            }
131
132
            $afterId = count($response) > 0 ? end($response)['id'] : null;
133
        } while (count($response) >= $limit);
134
    }
135
136
    /**
137
     * There is also a way to retrieve a single message using its id.
138
     *
139
     * @param string $roomId Room id
140
     * @param string $messageId Message id
141
     * @return array
142
     * @throws \RuntimeException
143
     * @throws \InvalidArgumentException
144
     * @throws \Throwable
145
     * @throws \Exception
146
     * @throws \GuzzleHttp\Exception\ClientException
147
     */
148
    public function find(string $roomId, string $messageId): array
149
    {
150
        return $this->fetch(
151
            Route::get('rooms/{roomId}/chatMessages/{messageId}')
152
                ->withMany(['roomId' => $roomId, 'messageId' => $messageId])
153
        );
154
    }
155
156
    /**
157
     * Send a message to a room.
158
     *
159
     * @param string $roomId Room id
160
     * @param string $content Message body
161
     * @return array
162
     * @throws \RuntimeException
163
     * @throws \InvalidArgumentException
164
     * @throws \Throwable
165
     * @throws \Exception
166
     * @throws \GuzzleHttp\Exception\ClientException
167
     */
168
    public function create(string $roomId, string $content): array
169
    {
170
        return $this->fetch(
171
            Route::post('rooms/{roomId}/chatMessages')
172
                ->with('roomId', $roomId)
173
                ->withBody('text', $content)
174
        );
175
    }
176
177
    /**
178
     * Delete a message.
179
     *
180
     * @param string $roomId
181
     * @param string $messageId
182
     * @return array
183
     * @throws \RuntimeException
184
     * @throws \InvalidArgumentException
185
     * @throws \Throwable
186
     * @throws \Exception
187
     * @throws \GuzzleHttp\Exception\ClientException
188
     */
189
    public function delete(string $roomId, string $messageId): array
190
    {
191
        return $this->update($roomId, $messageId, '');
192
    }
193
194
    /**
195
     * Update a message.
196
     *
197
     * @param string $roomId Room id
198
     * @param string $messageId Message id
199
     * @param string $content New message body
200
     * @return array
201
     * @throws \RuntimeException
202
     * @throws \InvalidArgumentException
203
     * @throws \Throwable
204
     * @throws \Exception
205
     * @throws \GuzzleHttp\Exception\ClientException
206
     */
207
    public function update(string $roomId, string $messageId, string $content): array
208
    {
209
        return $this->fetch(
210
            Route::put('rooms/{roomId}/chatMessages/{messageId}')
211
                ->withMany(['roomId' => $roomId, 'messageId' => $messageId])
212
                ->withBody('text', $content)
213
        );
214
    }
215
}
216