Passed
Pull Request — master (#12)
by Louis
16:52 queued 08:04
created

Request   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 57.69%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 77
ccs 15
cts 26
cp 0.5769
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A send() 0 11 1
A getUrlPath() 0 10 1
A getParams() 0 17 3
1
<?php
2
3
namespace EtherpadLite;
4
5
use GuzzleHttp\Client as HttpClient;
6
use Psr\Http\Message\ResponseInterface;
7
8
class Request
9
{
10
    private $apiKey = null;
11
    private $url = null;
12
    private $method;
13
    private $args;
14
15
    /**
16
     * @param $url
17
     * @param $apiKey
18
     * @param $method
19
     * @param array $args
20
     */
21 35
    public function __construct($url, $apiKey, $method, $args = array())
22
    {
23 35
        $this->url = $url;
24 35
        $this->apiKey = $apiKey;
25 35
        $this->method = $method;
26 35
        $this->args = $args;
27 35
    }
28
29
    /**
30
     * Send the built request url against the etherpad lite instance
31
     *
32
     * @return ResponseInterface
33
     */
34
    public function send()
35
    {
36
        $client = new HttpClient(['base_uri' => $this->url]);
37
38
        return $client->get(
39
            $this->getUrlPath(),
40
            array(
41
                'query' => $this->getParams()
42
            )
43
        );
44
    }
45
46
    /**
47
     * Returns the path of the request url
48
     *
49
     * @return string
50
     */
51
    protected function getUrlPath()
52
    {
53
        $existingPath = parse_url($this->url, PHP_URL_PATH);
54
55
        return $existingPath . sprintf(
56
                '/api/%s/%s',
57
                Client::API_VERSION,
58
                $this->method
59
            );
60
    }
61
62
    /**
63
     * Maps the given arguments from Client::__call to the parameter of the api method
64
     *
65
     * @return array
66
     */
67 35
    public function getParams()
68
    {
69 35
        $params = array();
70 35
        $args = $this->args;
71
72 35
        $params['apikey'] = $this->apiKey;
73
74 35
        $methods = Client::getMethods();
75
76 35
        foreach ($methods[$this->method] as $key => $paramName) {
77 31
            if (isset($args[$key])) {
78 31
                $params[$paramName] = $args[$key];
79
            }
80
        }
81
82 35
        return $params;
83
    }
84
}
85