Test Failed
Push — master ( 600e01...d11dc7 )
by Kirill
06:38
created

Messages::allBeforeId()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
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
68
        do {
69
            $route = $this->routeForMessagesIterator($roomId, $limit, $query);
70
71
            if ($beforeId !== null) {
72
                $route->with('beforeId', $beforeId);
73
            }
74
75
            $response = array_reverse($this->fetch($route));
76
77
            foreach ($response as $message) {
78
                $beforeId = (string)$message['id'];
79
                yield $message;
80
            }
81
82
        } while (count($response) >= $limit);
83
    }
84
85
    /**
86
     * @param string $roomId
87
     * @param int $limit
88
     * @param string|null $query
89
     * @return Route
90
     */
91
    private function routeForMessagesIterator(string $roomId, int $limit, string $query = null): Route
92
    {
93
        $route = Route::get('rooms/{roomId}/chatMessages')
94
            ->with('roomId', $roomId)
95
            ->with('limit', (string)$limit);
96
97
        if ($query !== null) {
98
            $route->with('q', $query);
99
        }
100
101
        return $route;
102
    }
103
104
    /**
105
     * Returns all messages after target message id.
106
     *
107
     * @param string $roomId
108
     * @param string|null $afterId
109
     * @param string|null $query
110
     * @return \Generator
111
     * @throws \Exception
112
     * @throws \GuzzleHttp\Exception\ClientException
113
     * @throws \InvalidArgumentException
114
     * @throws \RuntimeException
115
     * @throws \Throwable
116
     */
117 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...
118
    {
119
        $limit = 100;
120
121
        do {
122
            $route = $this->routeForMessagesIterator($roomId, $limit, $query);
123
124
            if ($afterId !== null) {
125
                $route->with('afterId', $afterId);
126
            }
127
128
            $response = (array)$this->fetch($route);
129
130
            foreach ($response as $message) {
131
                $afterId = (string)$message['id'];
132
                yield $message;
133
            }
134
        } while (count($response) >= $limit);
135
    }
136
137
    /**
138
     * There is also a way to retrieve a single message using its id.
139
     *
140
     * @param string $roomId Room id
141
     * @param string $messageId Message id
142
     * @return array
143
     * @throws \RuntimeException
144
     * @throws \InvalidArgumentException
145
     * @throws \Throwable
146
     * @throws \Exception
147
     * @throws \GuzzleHttp\Exception\ClientException
148
     */
149
    public function find(string $roomId, string $messageId): array
150
    {
151
        return $this->fetch(
152
            Route::get('rooms/{roomId}/chatMessages/{messageId}')
153
                ->withMany(['roomId' => $roomId, 'messageId' => $messageId])
154
        );
155
    }
156
157
    /**
158
     * Send a message to a room.
159
     *
160
     * @param string $roomId Room id
161
     * @param string $content Message body
162
     * @return array
163
     * @throws \RuntimeException
164
     * @throws \InvalidArgumentException
165
     * @throws \Throwable
166
     * @throws \Exception
167
     * @throws \GuzzleHttp\Exception\ClientException
168
     */
169
    public function create(string $roomId, string $content): array
170
    {
171
        return $this->fetch(
172
            Route::post('rooms/{roomId}/chatMessages')
173
                ->with('roomId', $roomId)
174
                ->withBody('text', $content)
175
        );
176
    }
177
178
    /**
179
     * Delete a message.
180
     *
181
     * @param string $roomId
182
     * @param string $messageId
183
     * @return array
184
     * @throws \RuntimeException
185
     * @throws \InvalidArgumentException
186
     * @throws \Throwable
187
     * @throws \Exception
188
     * @throws \GuzzleHttp\Exception\ClientException
189
     */
190
    public function delete(string $roomId, string $messageId): array
191
    {
192
        return $this->update($roomId, $messageId, '');
193
    }
194
195
    /**
196
     * Update a message.
197
     *
198
     * @param string $roomId Room id
199
     * @param string $messageId Message id
200
     * @param string $content New message body
201
     * @return array
202
     * @throws \RuntimeException
203
     * @throws \InvalidArgumentException
204
     * @throws \Throwable
205
     * @throws \Exception
206
     * @throws \GuzzleHttp\Exception\ClientException
207
     */
208
    public function update(string $roomId, string $messageId, string $content): array
209
    {
210
        return $this->fetch(
211
            Route::put('rooms/{roomId}/chatMessages/{messageId}')
212
                ->withMany(['roomId' => $roomId, 'messageId' => $messageId])
213
                ->withBody('text', $content)
214
        );
215
    }
216
}
217