Completed
Push — bootstrap4 ( dc7ca1...d74498 )
by Simon
06:19
created

JsonApiPageBase::runApiPage()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
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\Tasks;
10
11
use Waca\API\ApiException;
12
use Waca\API\IJsonApiAction;
13
use Waca\WebRequest;
14
15
abstract class JsonApiPageBase extends ApiPageBase implements IJsonApiAction
16
{
17
    /**
18
     * Main function for this page, when no specific actions are called.
19
     *
20
     * @return void
21
     * @throws ApiException
22
     */
23 View Code Duplication
    final protected function main()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25
        if (headers_sent()) {
26
            throw new ApiException('Headers have already been sent - this indicates a bug in the application!');
27
        }
28
29
        header("Content-Type: application/json");
30
31
        // javascript access control
32
        $httpOrigin = WebRequest::origin();
33
34
        if ($httpOrigin !== null) {
35
            $CORSallowed = $this->getSiteConfiguration()->getCrossOriginResourceSharingHosts();
36
37
            if (in_array($httpOrigin, $CORSallowed)) {
38
                header("Access-Control-Allow-Origin: " . $httpOrigin);
39
            }
40
        }
41
42
        $responseData = $this->runApiPage();
43
44
        ob_end_clean();
45
        print($responseData);
46
        ob_start();
47
    }
48
49
    /**
50
     * Method that runs API action
51
     *
52
     * @return object|array The modified API document
53
     */
54
    public abstract function executeApiAction();
55
56
    /**
57
     * @return string
58
     */
59
    final public function runApiPage()
60
    {
61
62
        try {
63
            $apiDocument = $this->executeApiAction();
64
        }
65
        catch (ApiException $ex) {
66
            $apiDocument = [
67
                'error' => $ex->getMessage(),
68
            ];
69
        }
70
71
        $data = json_encode($apiDocument, JSON_UNESCAPED_UNICODE);
72
73
        $targetVar = WebRequest::getString('targetVariable');
74
        if ($targetVar !== null && preg_match('/^[a-z]+$/', $targetVar)) {
75
            $data = $targetVar . ' = ' . $data . ';';
76
        }
77
78
        return $data;
79
    }
80
}
81