1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nopolabs\Yabot\Plugins\Reservations; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Nopolabs\Yabot\Message\Message; |
7
|
|
|
use Nopolabs\Yabot\Plugin\PluginInterface; |
8
|
|
|
use Nopolabs\Yabot\Plugin\PluginTrait; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
|
11
|
|
|
class ReservationsPlugin implements PluginInterface |
12
|
|
|
{ |
13
|
|
|
use PluginTrait; |
14
|
|
|
|
15
|
|
|
/** @var ResourcesInterface */ |
16
|
|
|
protected $resources; |
17
|
|
|
|
18
|
|
|
public function __construct( |
19
|
|
|
LoggerInterface $logger, |
20
|
|
|
ResourcesInterface $resources, |
21
|
|
|
array $config = []) |
22
|
|
|
{ |
23
|
|
|
$this->setLog($logger); |
24
|
|
|
$this->resources = $resources; |
25
|
|
|
|
26
|
|
|
$help = <<<EOS |
27
|
|
|
<prefix> reserve [env] |
28
|
|
|
<prefix> reserve [env] until [time] |
29
|
|
|
<prefix> reserve [env] forever |
30
|
|
|
<prefix> release [env] |
31
|
|
|
<prefix> release mine |
32
|
|
|
<prefix> release all |
33
|
|
|
<prefix> (what|which) envs are reserved |
34
|
|
|
<prefix> (what|which) envs are mine |
35
|
|
|
<prefix> (what|which) envs are free |
36
|
|
|
<prefix> is [env] free |
37
|
|
|
EOS; |
38
|
|
|
|
39
|
|
|
$this->setConfig(array_merge( |
40
|
|
|
[ |
41
|
|
|
'help' => $help, |
42
|
|
|
'resourceNamePlural' => 'envs', |
43
|
|
|
'matchers' => [ |
44
|
|
|
'reserveForever' => "/^reserve (?'resource'\\w+) forever\\b/", |
45
|
|
|
'reserveUntil' => "/^reserve (?'resource'\\w+) until (?'until'.+)/", |
46
|
|
|
'reserve' => "/^reserve (?'resource'\\w+)/", |
47
|
|
|
|
48
|
|
|
'releaseMine' => "/^release mine\\b/", |
49
|
|
|
'releaseAll' => "/^release all\\b/", |
50
|
|
|
'release' => "/^release (?'resource'\\w+)/", |
51
|
|
|
'releasePlease' => "/^release (?'resource'\\w+)\\s+please\\b/", |
52
|
|
|
'pleaseRelease' => "/^please\\s+release (?'resource'\\w+)/", |
53
|
|
|
|
54
|
|
|
'list' => '/^wh(?:at|ich) #resourceNamePlural# are reserved\\b/', |
55
|
|
|
'listMine' => "/^wh(?:at|ich) #resourceNamePlural# are mine\\b/", |
56
|
|
|
'listFree' => "/^wh(?:at|ich) #resourceNamePlural# are free\\b/", |
57
|
|
|
|
58
|
|
|
'isFree' => "/^is (?'resource'\\w+) free\\b/", |
59
|
|
|
], |
60
|
|
|
], |
61
|
|
|
$config |
62
|
|
|
)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function reserve(Message $msg, array $matches) |
66
|
|
|
{ |
67
|
|
|
$key = $matches['resource']; |
68
|
|
|
$results = $this->placeReservation($msg, $key); |
69
|
|
|
$msg->reply(implode("\n", $results)); |
70
|
|
|
$msg->setHandled(true); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
public function reserveForever(Message $msg, array $matches) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$key = $matches['resource']; |
76
|
|
|
$results = $this->placeReservation($msg, $key, $this->resources->forever()); |
77
|
|
|
$msg->reply(implode("\n", $results)); |
78
|
|
|
$msg->setHandled(true); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function reserveUntil(Message $msg, array $matches) |
82
|
|
|
{ |
83
|
|
|
$key = $matches['resource']; |
84
|
|
|
$until = $matches['until']; |
85
|
|
|
$until = $until === 'forever' ? $this->resources->forever() : new DateTime($until); |
86
|
|
|
$results = $this->placeReservation($msg, $key, $until); |
87
|
|
|
$msg->reply(implode("\n", $results)); |
88
|
|
|
$msg->setHandled(true); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function releasePlease(Message $msg, array $matches) |
92
|
|
|
{ |
93
|
|
|
$this->release($msg, $matches, true); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function pleaseRelease(Message $msg, array $matches) |
97
|
|
|
{ |
98
|
|
|
$this->release($msg, $matches, true); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
View Code Duplication |
public function release(Message $msg, array $matches, $please = false) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$key = $matches['resource']; |
104
|
|
|
$results = $this->releaseReservation($msg, $key, $please); |
105
|
|
|
$msg->reply(implode("\n", $results)); |
106
|
|
|
$msg->setHandled(true); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
public function releaseMine(Message $msg, array $matches) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$me = $msg->getUsername(); |
112
|
|
|
$results = []; |
113
|
|
|
foreach ($this->resources->getAll() as $key => $resource) { |
114
|
|
|
if (isset($resource['user']) && ($resource['user'] === $me)) { |
115
|
|
|
$results = array_merge($results, $this->releaseReservation($msg, $key)); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
$msg->reply(implode("\n", $results)); |
119
|
|
|
$msg->setHandled(true); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
View Code Duplication |
public function releaseAll(Message $msg, array $matches) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$results = []; |
125
|
|
|
foreach ($this->resources->getKeys() as $key) { |
126
|
|
|
$results = array_merge($results, $this->releaseReservation($msg, $key)); |
127
|
|
|
} |
128
|
|
|
$msg->reply(implode("\n", $results)); |
129
|
|
|
$msg->setHandled(true); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function list(Message $msg, array $matches) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$results = $this->resources->getAllStatuses(); |
135
|
|
|
$msg->reply(implode("\n", $results)); |
136
|
|
|
$msg->setHandled(true); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
View Code Duplication |
public function listMine(Message $msg, array $matches) |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$me = $msg->getUsername(); |
142
|
|
|
$results = []; |
143
|
|
|
foreach ($this->resources->getAll() as $key => $resource) { |
144
|
|
|
if (isset($resource['user']) && ($resource['user'] === $me)) { |
145
|
|
|
$results[] = $key; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
$msg->reply(implode(',', $results)); |
149
|
|
|
$msg->setHandled(true); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
View Code Duplication |
public function listFree(Message $msg, array $matches) |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
$results = []; |
155
|
|
|
foreach ($this->resources->getAll() as $key => $resource) { |
156
|
|
|
if (empty($resource)) { |
157
|
|
|
$results[] = $key; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
$msg->reply(implode(',', $results)); |
161
|
|
|
$msg->setHandled(true); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function isFree(Message $msg, array $matches) |
165
|
|
|
{ |
166
|
|
|
$results = []; |
167
|
|
|
$key = $matches['resource']; |
168
|
|
|
$resource = $this->resources->getResource($key); |
169
|
|
|
if ($resource === null) { |
170
|
|
|
$results[] = "$key not found."; |
171
|
|
View Code Duplication |
} else { |
|
|
|
|
172
|
|
|
if (empty($resource)) { |
173
|
|
|
$results[] = "$key is free."; |
174
|
|
|
} else { |
175
|
|
|
$results[] = "$key is reserved by {$resource['user']}."; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
$msg->reply(implode(',', $results)); |
179
|
|
|
$msg->setHandled(true); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
protected function overrideConfig(array $params) |
183
|
|
|
{ |
184
|
|
|
$config = $this->canonicalConfig(array_merge($this->getConfig(), $params)); |
185
|
|
|
|
186
|
|
|
$matchers = $config['matchers']; |
187
|
|
|
$resourceNamePlural = $config['resourceNamePlural']; |
188
|
|
|
|
189
|
|
|
$matchers = $this->replaceInPatterns('#resourceNamePlural#', $resourceNamePlural, $matchers); |
190
|
|
|
$matchers = $this->replaceInPatterns(' ', "\\s+", $matchers); |
191
|
|
|
|
192
|
|
|
$config['matchers'] = $matchers; |
193
|
|
|
|
194
|
|
|
$this->setConfig($config); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
protected function placeReservation(Message $msg, $key, DateTime $until = null) : array |
198
|
|
|
{ |
199
|
|
|
$results = []; |
200
|
|
|
$resource = $this->resources->getResource($key); |
201
|
|
|
|
202
|
|
|
if ($resource === null) { |
203
|
|
|
$results[] = "$key not found."; |
204
|
|
|
} else { |
205
|
|
|
$user = $msg->getUser(); |
206
|
|
|
$username = $user->getUsername(); |
207
|
|
|
if (empty($resource)) { |
208
|
|
|
$this->resources->reserve($key, $user, $until); |
209
|
|
|
$results[] = "Reserved $key for $username."; |
210
|
|
|
} elseif ($resource['user'] === $username) { |
211
|
|
|
$this->resources->reserve($key, $user, $until); |
212
|
|
|
$results[] = "Updated $key for $username."; |
213
|
|
|
} else { |
214
|
|
|
$results[] = "$key is reserved by {$resource['user']}."; |
215
|
|
|
} |
216
|
|
|
$results[] = $this->resources->getStatus($key); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return $results; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
protected function releaseReservation(Message $msg, $key, bool $please = false) : array |
223
|
|
|
{ |
224
|
|
|
$results = []; |
225
|
|
|
$resource = $this->resources->getResource($key); |
226
|
|
|
|
227
|
|
|
if ($resource === null) { |
228
|
|
|
$results[] = "$key not found."; |
229
|
|
|
} else { |
230
|
|
|
if (empty($resource)) { |
231
|
|
|
$results[] = "$key is not reserved."; |
232
|
|
|
} else { |
233
|
|
|
$user = $msg->getUser(); |
234
|
|
|
$username = $user->getUsername(); |
235
|
|
|
|
236
|
|
|
if ($please || $resource['user'] === $username) { |
237
|
|
|
$this->resources->release($key); |
238
|
|
|
$results[] = "Released $key."; |
239
|
|
View Code Duplication |
} else { |
|
|
|
|
240
|
|
|
$results[] = "$key is reserved by {$resource['user']}"; |
241
|
|
|
$results[] = "you may use 'release $key please' or 'please release $key'"; |
242
|
|
|
$results[] = "to release an env reserved by someone else."; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return $results; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
protected function validMatch(Message $message, array $params, array $matches) : bool |
|
|
|
|
251
|
|
|
{ |
252
|
|
|
if (isset($matches['resource'])) { |
253
|
|
|
$key = $matches['resource']; |
254
|
|
|
if (!$this->resources->isResource($key)) { |
255
|
|
|
$message->reply("'$key' is not a reservable resource"); |
256
|
|
|
return false; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
return true; |
260
|
|
|
} |
261
|
|
|
} |
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.