Test Setup Failed
Push — master ( 1f25c9...2084e1 )
by Simon
03:27
created

Logger::banReplaced()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
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\DataObject;
13
use Waca\DataObjects\Ban;
14
use Waca\DataObjects\Comment;
15
use Waca\DataObjects\Domain;
16
use Waca\DataObjects\EmailTemplate;
17
use Waca\DataObjects\JobQueue;
18
use Waca\DataObjects\Log;
19
use Waca\DataObjects\Request;
20
use Waca\DataObjects\RequestForm;
21
use Waca\DataObjects\RequestQueue;
22
use Waca\DataObjects\SiteNotice;
23
use Waca\DataObjects\User;
24
use Waca\DataObjects\WelcomeTemplate;
25
use Waca\PdoDatabase;
26
27
/**
28
 * Helper class for creating log entries
29
 *
30
 * Logger description.
31
 *
32
 * @version 1.0
33
 * @author  stwalkerster
34
 */
35
class Logger
36
{
37
    /**
38
     * @param PdoDatabase $database
39
     * @param Request     $object
40
     */
41
    public static function emailConfirmed(PdoDatabase $database, Request $object)
42
    {
43
        self::createLogEntry($database, $object, "Email Confirmed", null, User::getCommunity());
44
    }
45
46
    /**
47
     * @param PdoDatabase $database
48
     * @param DataObject  $object
49
     * @param string      $logAction
50
     * @param null|string $comment
51
     * @param User|null   $user
52
     * @param int|null    $domain
53
     *
54
     * @throws Exception
55
     */
56
    private static function createLogEntry(
57
        PdoDatabase $database,
58
        DataObject $object,
59
        $logAction,
60
        $comment = null,
61
        $user = null,
62
        ?int $domain = null
63
    ) {
64
        if ($user == null) {
65
            $user = User::getCurrent($database);
66
        }
67
68
        $objectType = get_class($object);
69
        if (strpos($objectType, 'Waca\\DataObjects\\') !== false) {
70
            $objectType = str_replace('Waca\\DataObjects\\', '', $objectType);
71
        }
72
73
        $log = new Log();
74
75
        if ($domain !== null) {
76
            $log->setDomain($domain);
77
        }
78
        elseif (method_exists($object, 'getDomain')) {
79
            $log->setDomain($object->getDomain());
80
        }
81
82
        $log->setDatabase($database);
83
        $log->setAction($logAction);
84
        $log->setObjectId($object->getId());
85
        $log->setObjectType($objectType);
86
        $log->setUser($user);
87
        $log->setComment($comment);
88
        $log->save();
89
    }
90
91
    #region Users
92
93
    /**
94
     * @param PdoDatabase $database
95
     * @param User        $user
96
     */
97
    public static function newUser(PdoDatabase $database, User $user)
98
    {
99
        self::createLogEntry($database, $user, 'Registered', null, User::getCommunity());
100
    }
101
102
    /**
103
     * @param PdoDatabase $database
104
     * @param User        $object
105
     */
106
    public static function approvedUser(PdoDatabase $database, User $object)
107
    {
108
        self::createLogEntry($database, $object, "Approved");
109
    }
110
111
    /**
112
     * @param PdoDatabase $database
113
     * @param User        $object
114
     * @param string      $comment
115
     */
116
    public static function declinedUser(PdoDatabase $database, User $object, $comment)
117
    {
118
        self::createLogEntry($database, $object, "Declined", $comment);
119
    }
120
121
    /**
122
     * @param PdoDatabase $database
123
     * @param User        $object
124
     * @param string      $comment
125
     */
126
    public static function suspendedUser(PdoDatabase $database, User $object, $comment)
127
    {
128
        self::createLogEntry($database, $object, "Suspended", $comment);
129
    }
130
131
    /**
132
     * @param PdoDatabase $database
133
     * @param User        $object
134
     * @param string      $comment
135
     */
136
    public static function demotedUser(PdoDatabase $database, User $object, $comment)
137
    {
138
        self::createLogEntry($database, $object, "Demoted", $comment);
139
    }
140
141
    /**
142
     * @param PdoDatabase $database
143
     * @param User        $object
144
     */
145
    public static function promotedUser(PdoDatabase $database, User $object)
146
    {
147
        self::createLogEntry($database, $object, "Promoted");
148
    }
149
150
    /**
151
     * @param PdoDatabase $database
152
     * @param User        $object
153
     * @param string      $comment
154
     */
155
    public static function renamedUser(PdoDatabase $database, User $object, $comment)
156
    {
157
        self::createLogEntry($database, $object, "Renamed", $comment);
158
    }
159
160
    /**
161
     * @param PdoDatabase $database
162
     * @param User        $object
163
     */
164
    public static function userPreferencesChange(PdoDatabase $database, User $object)
165
    {
166
        self::createLogEntry($database, $object, "Prefchange");
167
    }
168
169
    /**
170
     * @param PdoDatabase $database
171
     * @param User        $object
172
     * @param string      $reason
173
     * @param array       $added
174
     * @param array       $removed
175
     * @param int         $domain
176
     *
177
     * @throws Exception
178
     */
179
    public static function userRolesEdited(PdoDatabase $database, User $object, $reason, $added, $removed, int $domain)
180
    {
181
        $logData = serialize(array(
182
            'added'   => $added,
183
            'removed' => $removed,
184
            'reason'  => $reason,
185
        ));
186
187
        self::createLogEntry($database, $object, "RoleChange", $logData, null, $domain);
188
    }
189
190
    #endregion
191
192
    /**
193
     * @param PdoDatabase $database
194
     * @param SiteNotice  $object
195
     */
196
    public static function siteNoticeEdited(PdoDatabase $database, SiteNotice $object)
197
    {
198
        self::createLogEntry($database, $object, "Edited");
199
    }
200
201
    #region Welcome Templates
202
203
    /**
204
     * @param PdoDatabase     $database
205
     * @param WelcomeTemplate $object
206
     */
207
    public static function welcomeTemplateCreated(PdoDatabase $database, WelcomeTemplate $object)
208
    {
209
        self::createLogEntry($database, $object, "CreatedTemplate");
210
    }
211
212
    /**
213
     * @param PdoDatabase     $database
214
     * @param WelcomeTemplate $object
215
     */
216
    public static function welcomeTemplateEdited(PdoDatabase $database, WelcomeTemplate $object)
217
    {
218
        self::createLogEntry($database, $object, "EditedTemplate");
219
    }
220
221
    /**
222
     * @param PdoDatabase     $database
223
     * @param WelcomeTemplate $object
224
     */
225
    public static function welcomeTemplateDeleted(PdoDatabase $database, WelcomeTemplate $object)
226
    {
227
        self::createLogEntry($database, $object, "DeletedTemplate");
228
    }
229
230
    #endregion
231
232
    #region Bans
233
234
    /**
235
     * @param PdoDatabase $database
236
     * @param Ban         $object
237
     * @param string      $reason
238
     */
239
    public static function banned(PdoDatabase $database, Ban $object, $reason)
240
    {
241
        self::createLogEntry($database, $object, "Banned", $reason);
242
    }
243
244
    /**
245
     * @param PdoDatabase $database
246
     * @param Ban         $object
247
     * @param string      $reason
248
     */
249
    public static function unbanned(PdoDatabase $database, Ban $object, $reason)
250
    {
251
        self::createLogEntry($database, $object, "Unbanned", $reason);
252
    }
253
254
    public static function banReplaced(PdoDatabase $database, Ban $object)
255
    {
256
        self::createLogEntry($database, $object, "BanReplaced");
257
    }
258
259
    #endregion
260
261
    #region Requests
262
263
    /**
264
     * @param PdoDatabase $database
265
     * @param Request     $object
266
     * @param string      $target
267
     */
268
    public static function deferRequest(PdoDatabase $database, Request $object, $target)
269
    {
270
        self::createLogEntry($database, $object, "Deferred to $target");
271
    }
272
273
    /**
274
     * @param PdoDatabase $database
275
     * @param Request     $object
276
     * @param integer     $target
277
     * @param string      $comment
278
     * @param User|null   $logUser
279
     */
280
    public static function closeRequest(PdoDatabase $database, Request $object, $target, $comment, User $logUser = null)
281
    {
282
        self::createLogEntry($database, $object, "Closed $target", $comment, $logUser);
283
    }
284
285
    public static function manuallyConfirmRequest(PdoDatabase $database, Request $object)
286
    {
287
        self::createLogEntry($database, $object, "Manually Confirmed");
288
    }
289
290
    /**
291
     * @param PdoDatabase $database
292
     * @param Request     $object
293
     */
294
    public static function reserve(PdoDatabase $database, Request $object)
295
    {
296
        self::createLogEntry($database, $object, "Reserved");
297
    }
298
299
    /**
300
     * @param PdoDatabase $database
301
     * @param Request     $object
302
     */
303
    public static function breakReserve(PdoDatabase $database, Request $object)
304
    {
305
        self::createLogEntry($database, $object, "BreakReserve");
306
    }
307
308
    /**
309
     * @param PdoDatabase $database
310
     * @param Request     $object
311
     */
312
    public static function unreserve(PdoDatabase $database, Request $object)
313
    {
314
        self::createLogEntry($database, $object, "Unreserved");
315
    }
316
317
    /**
318
     * @param PdoDatabase $database
319
     * @param Comment     $object
320
     * @param Request     $request
321
     */
322
    public static function editComment(PdoDatabase $database, Comment $object, Request $request)
323
    {
324
        self::createLogEntry($database, $request, "EditComment-r");
325
        self::createLogEntry($database, $object, "EditComment-c", null, null, $request->getDomain());
326
    }
327
328
    /**
329
     * @param PdoDatabase $database
330
     * @param Comment     $object
331
     */
332
    public static function flaggedComment(PdoDatabase $database, Comment $object, int $domain)
333
    {
334
        self::createLogEntry($database, $object, "FlaggedComment", null, null, $domain);
335
    }
336
337
    /**
338
     * @param PdoDatabase $database
339
     * @param Comment     $object
340
     */
341
    public static function unflaggedComment(PdoDatabase $database, Comment $object, int $domain)
342
    {
343
        self::createLogEntry($database, $object, "UnflaggedComment", null, null, $domain);
344
    }
345
346
    /**
347
     * @param PdoDatabase $database
348
     * @param Request     $object
349
     * @param User        $target
350
     */
351
    public static function sendReservation(PdoDatabase $database, Request $object, User $target)
352
    {
353
        self::createLogEntry($database, $object, "SendReserved");
354
        self::createLogEntry($database, $object, "ReceiveReserved", null, $target);
355
    }
356
357
    /**
358
     * @param PdoDatabase $database
359
     * @param Request     $object
360
     * @param string      $comment
361
     */
362
    public static function sentMail(PdoDatabase $database, Request $object, $comment)
363
    {
364
        self::createLogEntry($database, $object, "SentMail", $comment);
365
    }
366
367
    /**
368
     * @param PdoDatabase $database
369
     * @param Request     $object
370
     */
371
    public static function enqueuedJobQueue(PdoDatabase $database, Request $object)
372
    {
373
        self::createLogEntry($database, $object, 'EnqueuedJobQueue');
374
    }
375
376
    public static function hospitalised(PdoDatabase $database, Request $object)
377
    {
378
        self::createLogEntry($database, $object, 'Hospitalised');
379
    }
380
    #endregion
381
382
    #region Email templates
383
384
    /**
385
     * @param PdoDatabase   $database
386
     * @param EmailTemplate $object
387
     */
388
    public static function createEmail(PdoDatabase $database, EmailTemplate $object)
389
    {
390
        self::createLogEntry($database, $object, "CreatedEmail");
391
    }
392
393
    /**
394
     * @param PdoDatabase   $database
395
     * @param EmailTemplate $object
396
     */
397
    public static function editedEmail(PdoDatabase $database, EmailTemplate $object)
398
    {
399
        self::createLogEntry($database, $object, "EditedEmail");
400
    }
401
402
    #endregion
403
404
    #region Display
405
406
    #endregion
407
408
    #region Automation
409
410
    public static function backgroundJobComplete(PdoDatabase $database, JobQueue $job)
411
    {
412
        self::createLogEntry($database, $job, 'JobCompleted', null, User::getCommunity());
413
    }
414
415
    public static function backgroundJobIssue(PdoDatabase $database, JobQueue $job)
416
    {
417
        $data = array('status' => $job->getStatus(), 'error' => $job->getError());
418
        self::createLogEntry($database, $job, 'JobIssue', serialize($data), User::getCommunity());
419
    }
420
421
    public static function backgroundJobCancelled(PdoDatabase $database, JobQueue $job)
422
    {
423
        self::createLogEntry($database, $job, 'JobCancelled', $job->getError());
424
    }
425
426
    public static function backgroundJobRequeued(PdoDatabase $database, JobQueue $job)
427
    {
428
        self::createLogEntry($database, $job, 'JobRequeued');
429
    }
430
431
    public static function backgroundJobAcknowledged(PdoDatabase $database, JobQueue $job, $comment = null)
432
    {
433
        self::createLogEntry($database, $job, 'JobAcknowledged', $comment);
434
    }
435
    #endregion
436
437
    #region Request Queues
438
    public static function requestQueueCreated(PdoDatabase $database, RequestQueue $queue)
439
    {
440
        self::createLogEntry($database, $queue, 'QueueCreated');
441
    }
442
443
    public static function requestQueueEdited(PdoDatabase $database, RequestQueue $queue)
444
    {
445
        self::createLogEntry($database, $queue, 'QueueEdited');
446
    }
447
    #endregion
448
    #region Domains
449
    public static function domainCreated(PdoDatabase $database, Domain $domain)
450
    {
451
        self::createLogEntry($database, $domain, 'DomainCreated');
452
    }
453
454
    public static function domainEdited(PdoDatabase $database, Domain $domain)
455
    {
456
        self::createLogEntry($database, $domain, 'DomainEdited');
457
    }
458
    #endregion
459
    #region Request Forms
460
    public static function requestFormCreated(PdoDatabase $database, RequestForm $requestForm)
461
    {
462
        self::createLogEntry($database, $requestForm, 'RequestFormCreated');
463
    }
464
465
    public static function requestFormEdited(PdoDatabase $database, RequestForm $requestForm)
466
    {
467
        self::createLogEntry($database, $requestForm, 'RequestFormEdited');
468
    }
469
    #endregion
470
}
471