Passed
Push — newinternal-releasecandidate ( 1c5b59...e5ef47 )
by Simon
06:45
created

IrcNotificationHelper   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 430
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 33
eloc 99
c 3
b 0
f 0
dl 0
loc 430
rs 9.76

28 Methods

Rating   Name   Duplication   Size   Complexity  
A userApproved() 0 3 1
A userNew() 0 3 1
A send() 0 27 3
A __construct() 0 21 2
A requestDeferred() 0 8 1
A userPrefChange() 0 3 1
A welcomeTemplateCreated() 0 3 1
A requestUnreserved() 0 3 1
A requestReservationSent() 0 7 1
A requestDeferredWithMail() 0 10 1
A welcomeTemplateEdited() 0 3 1
A userSuspended() 0 3 1
A unbanned() 0 4 1
A requestReserveBroken() 0 5 1
A requestClosed() 0 5 1
A userRolesEdited() 0 4 1
A userRenamed() 0 3 1
A emailEdited() 0 4 1
A requestReceived() 0 13 1
A userDeclined() 0 3 1
A welcomeTemplateDeleted() 0 3 1
A emailCreated() 0 4 1
A commentEdited() 0 6 1
A siteNoticeEdited() 0 3 1
A commentCreated() 0 6 2
A banned() 0 12 2
A requestReserved() 0 5 1
A sentMail() 0 4 1
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() == -1) {
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 sentMail
337
     *
338
     * @param Request $request
339
     */
340
    public function sentMail(Request $request)
341
    {
342
        $this->send($this->currentUser->getUsername()
343
            . " sent an email related to Request {$request->getId()} ({$request->getName()})");
344
    }
345
346
    #endregion
347
348
    #region reservations
349
350
    /**
351
     * Summary of requestReserved
352
     *
353
     * @param Request $request
354
     */
355
    public function requestReserved(Request $request)
356
    {
357
        $username = $this->currentUser->getUsername();
358
359
        $this->send("Request {$request->getId()} ({$request->getName()}) reserved by {$username}");
360
    }
361
362
    /**
363
     * Summary of requestReserveBroken
364
     *
365
     * @param Request $request
366
     */
367
    public function requestReserveBroken(Request $request)
368
    {
369
        $username = $this->currentUser->getUsername();
370
371
        $this->send("Reservation on request {$request->getId()} ({$request->getName()}) broken by {$username}");
372
    }
373
374
    /**
375
     * Summary of requestUnreserved
376
     *
377
     * @param Request $request
378
     */
379
    public function requestUnreserved(Request $request)
380
    {
381
        $this->send("Request {$request->getId()} ({$request->getName()}) is no longer being handled.");
382
    }
383
384
    /**
385
     * Summary of requestReservationSent
386
     *
387
     * @param Request $request
388
     * @param User    $target
389
     */
390
    public function requestReservationSent(Request $request, User $target)
391
    {
392
        $username = $this->currentUser->getUsername();
393
394
        $this->send(
395
            "Reservation of request {$request->getId()} ({$request->getName()}) sent to {$target->getUsername()} by "
396
            . $username);
397
    }
398
399
    #endregion
400
401
    #region comments
402
403
    /**
404
     * Summary of commentCreated
405
     *
406
     * @param Comment $comment
407
     * @param Request $request
408
     */
409
    public function commentCreated(Comment $comment, Request $request)
410
    {
411
        $username = $this->currentUser->getUsername();
412
        $visibility = ($comment->getVisibility() == "admin" ? "private " : "");
413
414
        $this->send("{$username} posted a {$visibility}comment on request {$request->getId()} ({$request->getName()})");
415
    }
416
417
    /**
418
     * Summary of commentEdited
419
     *
420
     * @param Comment $comment
421
     * @param Request $request
422
     */
423
    public function commentEdited(Comment $comment, Request $request)
424
    {
425
        $username = $this->currentUser->getUsername();
426
427
        $this->send(<<<TAG
428
Comment {$comment->getId()} on request {$request->getId()} ({$request->getName()}) edited by {$username}
429
TAG
430
        );
431
    }
432
433
    #endregion
434
435
    #region email management (close reasons)
436
437
    /**
438
     * Summary of emailCreated
439
     *
440
     * @param EmailTemplate $template
441
     */
442
    public function emailCreated(EmailTemplate $template)
443
    {
444
        $username = $this->currentUser->getUsername();
445
        $this->send("Email {$template->getId()} ({$template->getName()}) created by " . $username);
446
    }
447
448
    /**
449
     * Summary of emailEdited
450
     *
451
     * @param EmailTemplate $template
452
     */
453
    public function emailEdited(EmailTemplate $template)
454
    {
455
        $username = $this->currentUser->getUsername();
456
        $this->send("Email {$template->getId()} ({$template->getName()}) edited by " . $username);
457
    }
458
    #endregion
459
}
460