Passed
Branch master (4f0f9e)
by Louis
17:08
created

Request::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
crap 1
1
<?php
2
3
namespace EtherpadLite;
4
5
class Request
6
{
7
    const API_VERSION = '1.2.7';
8
9
    private $apikey = null;
10
    private $url = null;
11
    private $method;
12
    private $args;
13
14
    /**
15
     * @param $url
16
     * @param $apikey
17
     * @param $method
18
     * @param array $args
19
     */
20 35
    public function __construct($url, $apikey, $method, $args = array())
21
    {
22 35
        $this->url = $url;
23 35
        $this->apikey = $apikey;
24 35
        $this->method = $method;
25 35
        $this->args = $args;
26 35
    }
27
28
    /**
29
     * Send the built request url against the etherpad lite instance
30
     *
31
     * @return \Guzzle\Http\Message\Response
32
     */
33
    public function send()
34
    {
35
        $client = new \Guzzle\Http\Client($this->url);
36
37
        $request = $client->get(
38
            $this->getUrlPath(),
39
            array(),
40
            array(
41
                'query' => $this->getParams()
42
            )
43
        );
44
45
        return $request->send();
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
}