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

Request::getParams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 0
crap 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
    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