Client::post()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 3
b 0
f 0
nc 1
nop 3
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
1
<?php
2
namespace ApigilityClient\Http;
3
4
use ApigilityClient\Exception\RuntimeException,
5
    ApigilityClient\Http\Response,
6
    ApigilityClient\Http\Client\ClientInterface;
7
8
use Zend\Http\Client as ZendHttpClient,
9
    Zend\Http\Client\Adapter\Curl,
10
    Zend\Http\Exception\RuntimeException as ZendHttpRuntimeException;
11
12
final class Client implements ClientInterface
13
{
14
15
    /**
16
     * @const Int Timeout for each request
17
     */
18
    const TIMEOUT = 60;
19
20
    /**
21
     * @var Zend\Http\Client Instance
22
     */
23
    private $zendClient;
24
25 2
    public function __construct(ZendHttpClient $client = null)
26
    {
27 2
        $client = ($client instanceof ZendHttpClient) ? $client : new ZendHttpClient();
28
29 2
        $this->setZendClient($client);
30 2
    }
31
32 2
    public function setZendClient(ZendHttpClient $client)
33
    {
34 2
        $host = $client->getUri()->getHost();
35
36 2
        if (empty($host)) {
37 1
            throw new ZendHttpRuntimeException(
38
                'Host not defined.'
39 1
            );
40
        }
41
42 2
        $this->zendClient = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client of type object<Zend\Http\Client> is incompatible with the declared type object<ApigilityClient\Http\Zend\Http\Client> of property $zendClient.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
44 2
        return $this;
45
    }
46
47
    /**
48
     * Get the Zend\Http\Client instance
49
     *
50
     * @return Zend\Http\Client
51
     */
52 1
    public function getZendClient()
53
    {
54 1
        return $this->zendClient;
55
    }
56
57
    /**
58
     * Perform the request to api server
59
     *
60
     * @param String $path Example: "/v1/endpoint"
61
     * @param Array $headers
62
     */
63
    private function doRequest($path, $headers = array())
64
    {
65
        $this->zendClient->getUri()->setPath($path);
66
67
        $this->zendClient->getRequest()->getHeaders()->addHeaders($headers);
68
69
        $zendHttpResponse = $this->zendClient->send();
70
71
        try {
72
            $response = new Response($this->zendClient, $zendHttpResponse);
73
            $content = $response->getContent();
74
        } catch (ZendHttpRuntimeException $e) {
75
	    throw $e;
76
        }
77
78
        return $content;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function get($path, array $data = array(), array $headers = array())
85
    {
86
        $this->zendClient->setMethod('GET')
87
                         ->setParameterGet($data);
88
89
        return $this->doRequest($path, $headers);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function post($path, array $data, array $headers = array())
96
    {
97
        $this->zendClient->setMethod('POST')
98
                         ->setRawBody(json_encode($data));
99
100
        return $this->doRequest($path, $headers);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function put($path, array $data, array $headers = array())
107
    {
108
        $this->zendClient->setMethod('PUT')
109
                         ->setRawBody(json_encode($data));
110
111
        return $this->doRequest($path, $headers);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function patch($path, array $data, array $headers = array())
118
    {
119
        $this->zendClient->setMethod('PATCH')
120
                         ->setRawBody(json_encode($data));
121
122
        return $this->doRequest($path, $headers);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function delete($path, array $headers = array())
129
    {
130
        $this->zendClient->setMethod('DELETE');
131
132
        return $this->doRequest($path, $headers);
133
    }
134
135
}
136