1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of GitterBot package. |
5
|
|
|
* |
6
|
|
|
* @author Serafim <[email protected]> |
7
|
|
|
* @date 24.09.2015 00:00 |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Interfaces\Gitter\Http; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @TODO Перенести в роуты |
17
|
|
|
* |
18
|
|
|
* Class UrlStorage |
19
|
|
|
*/ |
20
|
|
|
class UrlStorage |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $token; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected static $routes = [ |
31
|
|
|
/* --------------------- |
32
|
|
|
* Streams |
33
|
|
|
* --------------------- */ |
34
|
|
|
'events' => 'https://stream.gitter.im/v1/rooms/{roomId}/events', |
35
|
|
|
'messages' => 'https://stream.gitter.im/v1/rooms/{roomId}/chatMessages', |
36
|
|
|
|
37
|
|
|
/* --------------------- |
38
|
|
|
* Messages |
39
|
|
|
* --------------------- */ |
40
|
|
|
// Additional arguments: limit, afterId, beforeId, skip |
41
|
|
|
'message.list' => 'https://api.gitter.im/v1/rooms/{roomId}/chatMessages', |
42
|
|
|
// POST: {"text": "message text"} |
|
|
|
|
43
|
|
|
'message.send' => 'https://api.gitter.im/v1/rooms/{roomId}/chatMessages', |
44
|
|
|
// PUT: {"text": "new text"} |
|
|
|
|
45
|
|
|
'message.update' => 'https://api.gitter.im/v1/rooms/{roomId}/chatMessages/{messageId}', |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/* --------------------- |
49
|
|
|
* Rooms |
50
|
|
|
* --------------------- */ |
51
|
|
|
'room.list' => 'https://api.gitter.im/v1/rooms', |
52
|
|
|
'room.info' => 'https://api.gitter.im/v1/rooms/{roomId}', |
53
|
|
|
'room.users' => 'https://api.gitter.im/v1/rooms/{roomId}/users', |
54
|
|
|
'room.channels' => 'https://api.gitter.im/v1/rooms/{roomId}/channels', |
55
|
|
|
// POST: {"uri": "username/repo"} |
|
|
|
|
56
|
|
|
'room.join' => 'https://api.gitter.im/v1/rooms/{roomId}', |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/* --------------------- |
60
|
|
|
* User |
61
|
|
|
* --------------------- */ |
62
|
|
|
'user' => 'https://api.gitter.im/v1/user/{userId}', |
63
|
|
|
'user.current' => 'https://api.gitter.im/v1/user', |
64
|
|
|
'user.rooms' => 'https://api.gitter.im/v1/user/{userId}/rooms', |
65
|
|
|
'user.orgs' => 'https://api.gitter.im/v1/user/{userId}/orgs', |
66
|
|
|
'user.repos' => 'https://api.gitter.im/v1/user/{userId}/repos', |
67
|
|
|
'user.channels' => 'https://api.gitter.im/v1/user/{userId}/channels', |
68
|
|
|
// POST: {"chat": [chatId, chatId]} |
|
|
|
|
69
|
|
|
'message.unread' => 'https://api.gitter.im/v1/user/{userId}/rooms/{roomId}/unreadItems', |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $token |
75
|
|
|
*/ |
76
|
|
|
public function __construct($token) |
77
|
|
|
{ |
78
|
|
|
$this->token = $token; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $name |
84
|
|
|
* @param array $args |
85
|
|
|
* @return mixed|string |
86
|
|
|
* @throws \InvalidArgumentException |
87
|
|
|
*/ |
88
|
|
|
public function route($name, array $args = []): string |
89
|
|
|
{ |
90
|
|
|
if (!array_key_exists($name, static::$routes)) { |
91
|
|
|
$message = sprintf('%s route not found.', $name); |
92
|
|
|
throw new \InvalidArgumentException($message); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$args['access_token'] = $this->token; |
96
|
|
|
|
97
|
|
|
return $this->url(static::$routes[$name], $args); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $url |
103
|
|
|
* @param array $args |
104
|
|
|
* @return mixed|string |
105
|
|
|
*/ |
106
|
|
|
public function url($url, array $args = []): string |
107
|
|
|
{ |
108
|
|
|
$url .= '?'; |
109
|
|
|
|
110
|
|
|
foreach ($args as $key => $value) { |
111
|
|
|
$value = urlencode($value); |
112
|
|
|
$search = sprintf('{%s}', $key); |
113
|
|
|
|
114
|
|
|
if (!str_contains($url, $search)) { |
115
|
|
|
$url .= sprintf('&%s=%s', $key, $value); |
116
|
|
|
} |
117
|
|
|
$url = str_replace($search, $value, $url); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $url; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.