Users   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 157
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 14 3
A currentUserId() 0 4 1
A rooms() 0 7 1
A unreadItems() 0 7 1
A markAsRead() 0 8 1
A orgs() 0 7 1
A repos() 0 7 1
A channels() 0 7 1
1
<?php
2
/**
3
 * This file is part of GitterApi package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Gitter\Resources;
11
12
use Gitter\Route;
13
14
/**
15
 * User schema
16
 *  - id:               Gitter User ID.
17
 *  - username:         Gitter/GitHub username.
18
 *  - displayName:      Gitter/GitHub user real name.
19
 *  - url:              Path to the user on Gitter.
20
 *  - avatarUrlSmall:   User avatar URI (small).
21
 *  - avatarUrlMedium:  User avatar URI (medium).
22
 *
23
 * @package Gitter\Resources
24
 */
25
class Users extends AbstractResource
26
{
27
    /**
28
     * @var array|null
29
     */
30
    private $currentUser;
31
32
    /**
33
     * Returns the current user logged in.
34
     *
35
     * @return array
36
     * @throws \RuntimeException
37
     * @throws \InvalidArgumentException
38
     * @throws \Throwable
39
     * @throws \Exception
40
     */
41
    public function current(): array
42
    {
43
        if ($this->currentUser === null) {
44
            $users = $this->fetch(Route::get('user'));
45
46
            if (isset($users[0])) {
47
                $this->currentUser = $users[0];
48
            } else {
49
                throw new \RuntimeException('Can not fetch current user');
50
            }
51
        }
52
53
        return $this->currentUser;
54
    }
55
56
    /**
57
     * @return string
58
     * @throws \Throwable
59
     * @throws \RuntimeException
60
     * @throws \Exception
61
     * @throws \InvalidArgumentException
62
     */
63
    public function currentUserId(): string
64
    {
65
        return (string)($this->current()['id'] ?? null);
66
    }
67
68
    /**
69
     * List of Rooms the user is part of.
70
     *
71
     * @param string|null $userId User id
72
     * @return array
73
     * @throws \RuntimeException
74
     * @throws \InvalidArgumentException
75
     * @throws \Throwable
76
     * @throws \Exception
77
     */
78
    public function rooms(string $userId = null): array
79
    {
80
        return $this->fetch(
81
            Route::get('user/{userId}/rooms')
82
                ->with('userId', $userId ?? $this->currentUserId())
83
        );
84
    }
85
86
    /**
87
     * You can retrieve unread items and mentions using the following endpoint.
88
     *
89
     * @param string $roomId
90
     * @param string|null $userId
91
     * @return array
92
     * @throws \RuntimeException
93
     * @throws \InvalidArgumentException
94
     * @throws \Throwable
95
     * @throws \Exception
96
     */
97
    public function unreadItems(string $roomId, string $userId = null): array
98
    {
99
        return $this->fetch(
100
            Route::get('user/{userId}/rooms/{roomId}/unreadItems')
101
                ->withMany(['userId' => $userId ?? $this->currentUserId(), 'roomId' => $roomId])
102
        );
103
    }
104
105
    /**
106
     * There is an additional endpoint nested under rooms that you can use to mark chat messages as read
107
     *
108
     * @param string $roomId
109
     * @param array $messageIds
110
     * @param string|null $userId
111
     * @return array
112
     * @throws \RuntimeException
113
     * @throws \InvalidArgumentException
114
     * @throws \Throwable
115
     * @throws \Exception
116
     */
117
    public function markAsRead(string $roomId, array $messageIds, string $userId = null): array
118
    {
119
        return $this->fetch(
120
            Route::post('user/{userId}/rooms/{roomId}/unreadItems')
121
                ->withMany(['userId' => $userId ?? $this->currentUserId(), 'roomId' => $roomId])
122
                ->withBody('chat', $messageIds)
123
        );
124
    }
125
126
    /**
127
     * List of the user's GitHub Organisations and their respective Room if available.
128
     *
129
     * @param string|null $userId
130
     * @return array
131
     * @throws \RuntimeException
132
     * @throws \InvalidArgumentException
133
     * @throws \Throwable
134
     * @throws \Exception
135
     */
136
    public function orgs(string $userId = null): array
137
    {
138
        return $this->fetch(
139
            Route::get('user/{userId}/orgs')
140
                ->with('userId', $userId ?? $this->currentUserId())
141
        );
142
    }
143
144
    /**
145
     * List of the user's GitHub Repositories and their respective Room if available.
146
     *
147
     * Note: It'll return private repositories if the current user has granted Gitter privileges to access them.
148
     *
149
     * @param string|null $userId
150
     * @return array
151
     * @throws \RuntimeException
152
     * @throws \InvalidArgumentException
153
     * @throws \Throwable
154
     * @throws \Exception
155
     */
156
    public function repos(string $userId = null): array
157
    {
158
        return $this->fetch(
159
            Route::get('user/{userId}/repos')
160
                ->with('userId', $userId ?? $this->currentUserId())
161
        );
162
    }
163
164
    /**
165
     * List of Gitter channels nested under the current user.
166
     *
167
     * @param string|null $userId
168
     * @return array
169
     * @throws \RuntimeException
170
     * @throws \InvalidArgumentException
171
     * @throws \Throwable
172
     * @throws \Exception
173
     */
174
    public function channels(string $userId = null): array
175
    {
176
        return $this->fetch(
177
            Route::get('user/{userId}/channels')
178
                ->with('userId', $userId ?? $this->currentUserId())
179
        );
180
    }
181
}
182