Completed
Pull Request — master (#7)
by Louis
54:05 queued 33:43
created

Request   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 54.84%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 79
ccs 17
cts 31
cp 0.5484
rs 10
c 1
b 1
f 0

4 Methods

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