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