Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
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 / 25
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 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 executeJob
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getBodyValidator
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
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 MediaWiki\Config\Config;
26use MediaWiki\Logger\LoggerFactory;
27use MediaWiki\Rest\Handler;
28use MediaWiki\Rest\HttpException;
29use MediaWiki\Rest\Validator\BodyValidator;
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|mixed
86     * @throws HttpException
87     */
88    public function execute() {
89        // execute the job
90        $response = $this->executeJob( $this->getValidatedBody() );
91        if ( $response['status'] === true ) {
92            return $response;
93        } else {
94            throw new HttpException( 'Internal Server Error', 500, [ 'error' => $response['error'] ] );
95        }
96    }
97
98    /**
99     * @param Job $job
100     * @return array containing the Job, status and potentially error message
101     */
102    private function executeJob( Job $job ) {
103        $result = $this->jobRunner->executeJob( $job );
104
105        if ( !$job->allowRetries() ) {
106            // Report success if the job doesn't allow retries
107            // even if actually the job has failed.
108            $result['status'] = true;
109        }
110
111        return $result;
112    }
113
114    /**
115     * Fetch the BodyValidator
116     * @param string $contentType Content type of the request.
117     * @return BodyValidator
118     * @throws HttpException
119     */
120    public function getBodyValidator( $contentType ) {
121        if ( $contentType !== 'application/json' ) {
122            throw new HttpException( "Unsupported Content-Type",
123                415,
124                [ 'content_type' => $contentType ]
125            );
126        }
127        return new EventBodyValidator( $this->config->get( 'SecretKey' ), $this->logger );
128    }
129}