Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeferredChecksJob
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 2
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2
3namespace LoginNotify;
4
5use Job;
6use LogicException;
7use MediaWiki\Title\Title;
8use MediaWiki\User\UserFactory;
9
10/**
11 * Class DeferredChecksJob
12 * @package LoginNotify
13 */
14class DeferredChecksJob extends Job {
15    public const TYPE_LOGIN_FAILED = 'failed';
16    public const TYPE_LOGIN_SUCCESS = 'success';
17
18    private $userFactory;
19
20    /**
21     * @param Title $title
22     * @param array $params
23     */
24    public function __construct( Title $title, array $params, UserFactory $userFactory ) {
25        $this->userFactory = $userFactory;
26        parent::__construct( 'LoginNotifyChecks', $title, $params );
27    }
28
29    /**
30     * Run the job
31     * @return bool Success
32     */
33    public function run() {
34        $checkType = $this->params['checkType'];
35        $userId = $this->params['userId'];
36        $user = $this->userFactory->newFromId( $userId );
37        // The ID is marked as a loaded field by the factory. To check if the
38        // user_id exists in the database, we need to explicitly load.
39        $user->load();
40        if ( !$user->getId() ) {
41            throw new LogicException( "Can't find user for user id=" . print_r( $userId, true ) );
42        }
43        if ( !isset( $this->params['subnet'] ) || !is_string( $this->params['subnet'] ) ) {
44            throw new LogicException( __CLASS__
45                . " expected to receive a string parameter 'subnet', got "
46                . print_r( $this->params['subnet'], true )
47            );
48        }
49        $subnet = $this->params['subnet'];
50        if ( !isset( $this->params['resultSoFar'] ) || !is_string( $this->params['resultSoFar'] ) ) {
51            throw new LogicException( __CLASS__
52                . " expected to receive a string parameter 'resultSoFar', got "
53                . print_r( $this->params['resultSoFar'], true )
54            );
55        }
56        $resultSoFar = $this->params['resultSoFar'];
57
58        $loginNotify = LoginNotify::getInstance();
59
60        switch ( $checkType ) {
61            case self::TYPE_LOGIN_FAILED:
62                $loginNotify->recordFailureDeferred( $user, $subnet, $resultSoFar );
63                break;
64            case self::TYPE_LOGIN_SUCCESS:
65                $loginNotify->sendSuccessNoticeDeferred( $user, $subnet, $resultSoFar );
66                break;
67            default:
68                throw new LogicException( 'Unknown check type ' . print_r( $checkType, true ) );
69        }
70
71        return true;
72    }
73}