Completed
Push — master ( 98fc75...1c7a02 )
by Rémy
03:43
created

Client::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace XoteliaClient;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Message\Response;
8
9
class Client
10
{
11
    /**
12
     * @var ClientInterface
13
     */
14
    protected $handler;
15
16
    /**
17
     * @param ClientInterface $handler
18
     */
19
    public function __construct(ClientInterface $handler)
20
    {
21
        $this->handler = $handler;
22
    }
23
24
    /**
25
     * @param string $path
26
     * @param array $options
27
     *
28
     * @return Response
29
     */
30
    public function get($path, array $options = [])
31
    {
32
        return $this->sendRequest('GET', $path, $options);
33
    }
34
35
    /**
36
     * @param string $path
37
     * @param array $options
38
     *
39
     * @return Response
40
     */
41
    public function put($path, array $options = [])
42
    {
43
        return $this->sendRequest('PUT', $path, $options);
44
    }
45
46
    /**
47
     * @param string $path
48
     * @param array $options
49
     *
50
     * @return Response
51
     */
52
    public function delete($path, array $options = [])
53
    {
54
        return $this->sendRequest('DELETE', $path, $options);
55
    }
56
57
    /**
58
     * @param string $path
59
     * @param array $options
60
     *
61
     * @return Response
62
     */
63
    public function post($path, array $options = [])
64
    {
65
        return $this->sendRequest('POST', $path, $options);
66
    }
67
68
    /**
69
     * @param string $method
70
     * @param string $path
71
     * @param array $options
72
     *
73
     * @return array
74
     */
75
    public function sendRequest($method, $path, array $options = [])
76
    {
77
        try {
78
            $response = $this->handler->send($this->handler->createRequest($method, $path, $options));
79
        } catch (ClientException $ex) {
80
            $response = $ex->getResponse();
81
        }
82
83
        return $response->json();
84
    }
85
}
86