AbstractService   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 27.78%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 52
ccs 5
cts 18
cp 0.2778
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
B makeHTTPRequest() 0 17 7
A getRepoPageText() 0 3 1
A getInstance() 0 7 3
1
<?php
2
3
namespace dokuwiki\plugin\issuelinks\services;
4
5
use dokuwiki\plugin\issuelinks\classes\ExternalServerException;
6
use dokuwiki\plugin\issuelinks\classes\HTTPRequestException;
7
8
abstract class AbstractService implements ServiceInterface
9
{
10
    protected static $instance = [];
11
12 2
    public static function getInstance($forcereload = false)
13
    {
14 2
        $class = static::class;
15 2
        if (empty(self::$instance[$class]) || $forcereload) {
16 2
            self::$instance[$class] = new $class();
17
        }
18 2
        return self::$instance[$class];
19
    }
20
21
    /**
22
     * Make an http request
23
     *
24
     * Throws an exception on errer or if the request was not successful
25
     *
26
     * You can query the $dokuHTTPClient for more result headers later
27
     *
28
     * @param \DokuHTTPClient $dokuHTTPClient an Instance of \DokuHTTPClient which will make the request
29
     * @param string          $url            the complete URL to which to make the request
30
     * @param array           $headers        This headers will be merged with the headers in the DokuHTTPClient
31
     * @param array           $data           an array of the data which is to be send
32
     * @param string          $method         a HTTP verb, like GET, POST or DELETE
33
     *
34
     * @return array the result that was returned from the server, json-decoded
35
     *
36
     * @throws HTTPRequestException
37
     */
38
    protected function makeHTTPRequest(\DokuHTTPClient $dokuHTTPClient, $url, $headers, array $data, $method)
39
    {
40
        $dokuHTTPClient->headers = array_merge($dokuHTTPClient->headers ?: [], $headers);
41
        $dataToBeSend = json_encode($data);
42
        try {
43
            $success = $dokuHTTPClient->sendRequest($url, $dataToBeSend, $method);
44
        } catch (\HTTPClientException $e) {
45
            throw new HTTPRequestException('request error', $dokuHTTPClient, $url, $method);
46
        }
47
48
        if (!$success || $dokuHTTPClient->status < 200 || $dokuHTTPClient->status > 206) {
49
            if ($dokuHTTPClient->status >= 500) {
50
                throw new ExternalServerException('request error', $dokuHTTPClient, $url, $method);
51
            }
52
            throw new HTTPRequestException('request error', $dokuHTTPClient, $url, $method);
53
        }
54
        return json_decode($dokuHTTPClient->resp_body, true);
55
    }
56
57
    public function getRepoPageText()
58
    {
59
        return '';
60
    }
61
}
62