Request::getParams()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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