Passed
Push — multiproject/domainui ( d92423 )
by Simon
04:27
created

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