Completed
Push — irc-link-update ( 5c98e1 )
by Simon
17s queued 13s
created

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