Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
RunSingleJobHandler
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 validate
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 execute
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 executeJob
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 makeJob
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 */
20
21namespace MediaWiki\Extension\EventBus\Rest;
22
23use Job;
24use JobRunner;
25use LogicException;
26use MediaWiki\Config\Config;
27use MediaWiki\Logger\LoggerFactory;
28use MediaWiki\Rest\Handler;
29use MediaWiki\Rest\HttpException;
30use MediaWiki\Rest\Validator\Validator;
31use Psr\Log\LoggerInterface;
32use Wikimedia\Rdbms\ReadOnlyMode;
33
34/**
35 * Class RunSingleJobHandler
36 * @package MediaWiki\Extension\EventBus
37 */
38class RunSingleJobHandler extends Handler {
39
40    /**
41     * @var LoggerInterface
42     */
43    private $logger;
44
45    /**
46     * @var Config
47     */
48    private $config;
49
50    /**
51     * @var JobRunner
52     */
53    private $jobRunner;
54
55    /**
56     * @var ReadOnlyMode
57     */
58    private $readOnly;
59
60    public function __construct(
61        ReadOnlyMode $readOnlyMode,
62        Config $config,
63        JobRunner $jobRunner
64    ) {
65        $this->readOnly = $readOnlyMode;
66        $this->config = $config;
67        $this->jobRunner = $jobRunner;
68        $this->logger = LoggerFactory::getInstance( 'RunSingleJobHandler' );
69    }
70
71    public function validate( Validator $restValidator ) {
72        if ( !$this->config->get( 'EventBusEnableRunJobAPI' ) ) {
73            throw new HttpException(
74                'Set $wgEventBusEnableRunJobAPI to true to enable the internal EventBus API',
75                501 );
76        }
77
78        if ( $this->readOnly->isReadOnly() ) {
79            throw new HttpException( "Wiki is in read-only mode.", 423 );
80        }
81        parent::validate( $restValidator );
82    }
83
84    /**
85     * @return array
86     * @throws HttpException
87     */
88    public function execute() {
89        $event = $this->getRequest()->getParsedBody();
90
91        if ( $event === null ) {
92            throw new LogicException( 'No parsed body found, should have failed in parseBodyData()' );
93        }
94
95        $job = $this->makeJob( $event );
96
97        // execute the job
98        $response = $this->executeJob( $job );
99        if ( $response['status'] === true ) {
100            return $response;
101        } else {
102            throw new HttpException( 'Internal Server Error', 500, [ 'error' => $response['error'] ] );
103        }
104    }
105
106    /**
107     * @param Job $job
108     * @return array containing the Job, status and potentially error message
109     */
110    private function executeJob( Job $job ) {
111        $result = $this->jobRunner->executeJob( $job );
112
113        if ( !$job->allowRetries() ) {
114            // Report success if the job doesn't allow retries
115            // even if actually the job has failed.
116            $result['status'] = true;
117        }
118
119        return $result;
120    }
121
122    private function makeJob( array $event ): Job {
123        $validator = new EventBodyValidator(
124            $this->config->get( 'SecretKey' ),
125            $this->logger
126        );
127        return $validator->validateEvent( $event );
128    }
129
130}