Failed Conditions
Pull Request — newinternal (#552)
by Simon
15:44 queued 05:45
created

IrcNotificationHelper::requestCloseQueued()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Helpers;
10
11
use Exception;
12
use Waca\DataObjects\Ban;
13
use Waca\DataObjects\Comment;
14
use Waca\DataObjects\EmailTemplate;
15
use Waca\DataObjects\Notification;
16
use Waca\DataObjects\Request;
17
use Waca\DataObjects\User;
18
use Waca\DataObjects\WelcomeTemplate;
19
use Waca\IrcColourCode;
20
use Waca\PdoDatabase;
21
use Waca\SiteConfiguration;
0 ignored issues
show
Bug introduced by
The type Waca\SiteConfiguration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
/**
24
 * Class IrcNotificationHelper
25
 * @package Waca\Helpers
26
 */
27
class IrcNotificationHelper
28
{
29
    /** @var PdoDatabase $notificationsDatabase */
30
    private $notificationsDatabase;
31
    /** @var PdoDatabase $primaryDatabase */
32
    private $primaryDatabase;
33
    /** @var bool $notificationsEnabled */
34
    private $notificationsEnabled;
35
    /** @var int $notificationType */
36
    private $notificationType;
37
    /** @var User $currentUser */
38
    private $currentUser;
39
    /** @var string $instanceName */
40
    private $instanceName;
41
    /** @var string */
42
    private $baseUrl;
43
    /** @var array */
44
    private $requestStates;
45
46
    /**
47
     * IrcNotificationHelper constructor.
48
     *
49
     * @param SiteConfiguration $siteConfiguration
50
     * @param PdoDatabase       $primaryDatabase
51
     * @param PdoDatabase       $notificationsDatabase
52
     */
53
    public function __construct(
54
        SiteConfiguration $siteConfiguration,
55
        PdoDatabase $primaryDatabase,
56
        PdoDatabase $notificationsDatabase = null
57
    ) {
58
        $this->primaryDatabase = $primaryDatabase;
59
60
        if ($notificationsDatabase !== null) {
61
            $this->notificationsDatabase = $notificationsDatabase;
62
            $this->notificationsEnabled = $siteConfiguration->getIrcNotificationsEnabled();
63
        }
64
        else {
65
            $this->notificationsEnabled = false;
66
        }
67
68
        $this->notificationType = $siteConfiguration->getIrcNotificationType();
69
        $this->instanceName = $siteConfiguration->getIrcNotificationsInstance();
70
        $this->baseUrl = $siteConfiguration->getBaseUrl();
71
        $this->requestStates = $siteConfiguration->getRequestStates();
72
73
        $this->currentUser = User::getCurrent($primaryDatabase);
74
    }
75
76
    /**
77
     * Send a notification
78
     *
79
     * @param string $message The text to send
80
     */
81
    protected function send($message)
82
    {
83
        $instanceName = $this->instanceName;
84
85
        if (!$this->notificationsEnabled) {
86
            return;
87
        }
88
89
        $blacklist = array("DCC", "CCTP", "PRIVMSG");
90
        $message = str_replace($blacklist, "(IRC Blacklist)", $message); // Lets stop DCC etc
91
92
        $msg = IrcColourCode::RESET . IrcColourCode::BOLD . "[$instanceName]" . IrcColourCode::RESET . ": $message";
93
94
        try {
95
            $notification = new Notification();
96
            $notification->setDatabase($this->notificationsDatabase);
97
            $notification->setType($this->notificationType);
98
            $notification->setText($msg);
99
100
            $notification->save();
101
        }
102
        catch (Exception $ex) {
103
            // OK, so we failed to send the notification - that db might be down?
104
            // This is non-critical, so silently fail.
105
106
            // Disable notifications for remainder of request.
107
            $this->notificationsEnabled = false;
108
        }
109
    }
110
111
    #region user management
112
113
    /**
114
     * send a new user notification
115
     *
116
     * @param User $user
117
     */
118
    public function userNew(User $user)
119
    {
120
        $this->send("New user: {$user->getUsername()}");
121
    }
122
123
    /**
124
     * send an approved notification
125
     *
126
     * @param User $user
127
     */
128
    public function userApproved(User $user)
129
    {
130
        $this->send("{$user->getUsername()} approved by " . $this->currentUser->getUsername());
131
    }
132
133
    /**
134
     * send a declined notification
135
     *
136
     * @param User   $user
137
     * @param string $reason the reason the user was declined
138
     */
139
    public function userDeclined(User $user, $reason)
140
    {
141
        $this->send("{$user->getUsername()} declined by " . $this->currentUser->getUsername() . " ($reason)");
142
    }
143
144
    /**
145
     * send a suspended notification
146
     *
147
     * @param User   $user
148
     * @param string $reason The reason the user has been suspended
149
     */
150
    public function userSuspended(User $user, $reason)
151
    {
152
        $this->send("{$user->getUsername()} suspended by " . $this->currentUser->getUsername() . " ($reason)");
153
    }
154
155
    /**
156
     * Send a preference change notification
157
     *
158
     * @param User $user
159
     */
160
    public function userPrefChange(User $user)
161
    {
162
        $this->send("{$user->getUsername()}'s preferences were changed by " . $this->currentUser->getUsername());
163
    }
164
165
    /**
166
     * Send a user renamed notification
167
     *
168
     * @param User   $user
169
     * @param string $old
170
     */
171
    public function userRenamed(User $user, $old)
172
    {
173
        $this->send($this->currentUser->getUsername() . " renamed $old to {$user->getUsername()}");
174
    }
175
176
    /**
177
     * @param User   $user
178
     * @param string $reason
179
     */
180
    public function userRolesEdited(User $user, $reason)
181
    {
182
        $currentUser = $this->currentUser->getUsername();
183
        $this->send("Active roles for {$user->getUsername()} changed by " . $currentUser . " ($reason)");
184
    }
185
186
    #endregion
187
188
    #region Site Notice
189
190
    /**
191
     * Summary of siteNoticeEdited
192
     */
193
    public function siteNoticeEdited()
194
    {
195
        $this->send("Site notice edited by " . $this->currentUser->getUsername());
196
    }
197
    #endregion
198
199
    #region Welcome Templates
200
    /**
201
     * Summary of welcomeTemplateCreated
202
     *
203
     * @param WelcomeTemplate $template
204
     */
205
    public function welcomeTemplateCreated(WelcomeTemplate $template)
206
    {
207
        $this->send("Welcome template {$template->getId()} created by " . $this->currentUser->getUsername());
208
    }
209
210
    /**
211
     * Summary of welcomeTemplateDeleted
212
     *
213
     * @param int $templateid
214
     */
215
    public function welcomeTemplateDeleted($templateid)
216
    {
217
        $this->send("Welcome template {$templateid} deleted by " . $this->currentUser->getUsername());
218
    }
219
220
    /**
221
     * Summary of welcomeTemplateEdited
222
     *
223
     * @param WelcomeTemplate $template
224
     */
225
    public function welcomeTemplateEdited(WelcomeTemplate $template)
226
    {
227
        $this->send("Welcome template {$template->getId()} edited by " . $this->currentUser->getUsername());
228
    }
229
230
    #endregion
231
232
    #region bans
233
    /**
234
     * Summary of banned
235
     *
236
     * @param Ban $ban
237
     */
238
    public function banned(Ban $ban)
239
    {
240
        if ($ban->getDuration() === null) {
241
            $duration = "indefinitely";
242
        }
243
        else {
244
            $duration = "until " . date("F j, Y, g:i a", $ban->getDuration());
245
        }
246
247
        $username = $this->currentUser->getUsername();
248
249
        $this->send("{$ban->getTarget()} banned by {$username} for '{$ban->getReason()}' {$duration}");
250
    }
251
252
    /**
253
     * Summary of unbanned
254
     *
255
     * @param Ban    $ban
256
     * @param string $unbanreason
257
     */
258
    public function unbanned(Ban $ban, $unbanreason)
259
    {
260
        $this->send($ban->getTarget() . " unbanned by " . $this->currentUser
261
                ->getUsername() . " (" . $unbanreason . ")");
262
    }
263
264
    #endregion
265
266
    #region request management
267
268
    /**
269
     * Summary of requestReceived
270
     *
271
     * @param Request $request
272
     */
273
    public function requestReceived(Request $request)
274
    {
275
        $this->send(
276
            IrcColourCode::DARK_GREY . "[["
277
            . IrcColourCode::DARK_GREEN . "acc:"
278
            . IrcColourCode::ORANGE . $request->getId()
279
            . IrcColourCode::DARK_GREY . "]]"
280
            . IrcColourCode::RED . " N "
281
            . IrcColourCode::DARK_BLUE . $this->baseUrl . "/internal.php/viewRequest?id={$request->getId()} "
282
            . IrcColourCode::DARK_RED . "* "
283
            . IrcColourCode::DARK_GREEN . $request->getName()
284
            . IrcColourCode::DARK_RED . " * "
285
            . IrcColourCode::RESET
286
        );
287
    }
288
289
    /**
290
     * Summary of requestDeferred
291
     *
292
     * @param Request $request
293
     */
294
    public function requestDeferred(Request $request)
295
    {
296
        $availableRequestStates = $this->requestStates;
297
298
        $deferTo = $availableRequestStates[$request->getStatus()]['deferto'];
299
        $username = $this->currentUser->getUsername();
300
301
        $this->send("Request {$request->getId()} ({$request->getName()}) deferred to {$deferTo} by {$username}");
302
    }
303
304
    /**
305
     *
306
     * Summary of requestDeferredWithMail
307
     *
308
     * @param Request $request
309
     */
310
    public function requestDeferredWithMail(Request $request)
311
    {
312
        $availableRequestStates = $this->requestStates;
313
314
        $deferTo = $availableRequestStates[$request->getStatus()]['deferto'];
315
        $username = $this->currentUser->getUsername();
316
        $id = $request->getId();
317
        $name = $request->getName();
318
319
        $this->send("Request {$id} ({$name}) deferred to {$deferTo} with an email by {$username}");
320
    }
321
322
    /**
323
     * Summary of requestClosed
324
     *
325
     * @param Request $request
326
     * @param string  $closetype
327
     */
328
    public function requestClosed(Request $request, $closetype)
329
    {
330
        $username = $this->currentUser->getUsername();
331
332
        $this->send("Request {$request->getId()} ({$request->getName()}) closed ($closetype) by {$username}");
333
    }
334
335
    /**
336
     * Summary of requestClosed
337
     *
338
     * @param Request $request
339
     * @param string  $closetype
340
     */
341
    public function requestCloseQueued(Request $request, $closetype)
342
    {
343
        $username = $this->currentUser->getUsername();
344
345
        $this->send("Request {$request->getId()} ({$request->getName()}) queued for creation ($closetype) by {$username}");
346
    }
347
348
    /**
349
     * Summary of requestClosed
350
     *
351
     * @param Request $request
352
     */
353
    public function requestCreationFailed(Request $request)
354
    {
355
        $this->send("Request {$request->getId()} ({$request->getName()}) failed auto-creation, and was sent to the hospital queue.");
356
    }
357
358
    /**
359
     * Summary of sentMail
360
     *
361
     * @param Request $request
362
     */
363
    public function sentMail(Request $request)
364
    {
365
        $this->send($this->currentUser->getUsername()
366
            . " sent an email related to Request {$request->getId()} ({$request->getName()})");
367
    }
368
369
    #endregion
370
371
    #region reservations
372
373
    /**
374
     * Summary of requestReserved
375
     *
376
     * @param Request $request
377
     */
378
    public function requestReserved(Request $request)
379
    {
380
        $username = $this->currentUser->getUsername();
381
382
        $this->send("Request {$request->getId()} ({$request->getName()}) reserved by {$username}");
383
    }
384
385
    /**
386
     * Summary of requestReserveBroken
387
     *
388
     * @param Request $request
389
     */
390
    public function requestReserveBroken(Request $request)
391
    {
392
        $username = $this->currentUser->getUsername();
393
394
        $this->send("Reservation on request {$request->getId()} ({$request->getName()}) broken by {$username}");
395
    }
396
397
    /**
398
     * Summary of requestUnreserved
399
     *
400
     * @param Request $request
401
     */
402
    public function requestUnreserved(Request $request)
403
    {
404
        $this->send("Request {$request->getId()} ({$request->getName()}) is no longer being handled.");
405
    }
406
407
    /**
408
     * Summary of requestReservationSent
409
     *
410
     * @param Request $request
411
     * @param User    $target
412
     */
413
    public function requestReservationSent(Request $request, User $target)
414
    {
415
        $username = $this->currentUser->getUsername();
416
417
        $this->send(
418
            "Reservation of request {$request->getId()} ({$request->getName()}) sent to {$target->getUsername()} by "
419
            . $username);
420
    }
421
422
    #endregion
423
424
    #region comments
425
426
    /**
427
     * Summary of commentCreated
428
     *
429
     * @param Comment $comment
430
     * @param Request $request
431
     */
432
    public function commentCreated(Comment $comment, Request $request)
433
    {
434
        $username = $this->currentUser->getUsername();
435
        $visibility = ($comment->getVisibility() == "admin" ? "private " : "");
436
437
        $this->send("{$username} posted a {$visibility}comment on request {$request->getId()} ({$request->getName()})");
438
    }
439
440
    /**
441
     * Summary of commentEdited
442
     *
443
     * @param Comment $comment
444
     * @param Request $request
445
     */
446
    public function commentEdited(Comment $comment, Request $request)
447
    {
448
        $username = $this->currentUser->getUsername();
449
450
        $this->send(<<<TAG
451
Comment {$comment->getId()} on request {$request->getId()} ({$request->getName()}) edited by {$username}
452
TAG
453
        );
454
    }
455
456
    #endregion
457
458
    #region email management (close reasons)
459
460
    /**
461
     * Summary of emailCreated
462
     *
463
     * @param EmailTemplate $template
464
     */
465
    public function emailCreated(EmailTemplate $template)
466
    {
467
        $username = $this->currentUser->getUsername();
468
        $this->send("Email {$template->getId()} ({$template->getName()}) created by " . $username);
469
    }
470
471
    /**
472
     * Summary of emailEdited
473
     *
474
     * @param EmailTemplate $template
475
     */
476
    public function emailEdited(EmailTemplate $template)
477
    {
478
        $username = $this->currentUser->getUsername();
479
        $this->send("Email {$template->getId()} ({$template->getName()}) edited by " . $username);
480
    }
481
    #endregion
482
}
483