Messages::find()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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