Completed
Push — master ( ee1b9f...ffef5f )
by William Johnson S.
02:04
created

GuzzleAdapter::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Katapoka\Ahgora\Adapters;
4
5
use Doctrine\Instantiator\Exception\InvalidArgumentException;
6
use GuzzleHttp\Client;
7
use Katapoka\Ahgora\HttpResponse;
8
use Katapoka\Ahgora\IHttpClient;
9
10
/**
11
 * Guzzle IHttpClient version
12
 */
13
class GuzzleAdapter implements IHttpClient
14
{
15
    /** @var \GuzzleHttp\Client The guzzle client. */
16
    private $client;
17
18
    /** @var int the timeout of the client or the request. */
19
    private $timeout;
20
21
    /** @var array The client headers. */
22
    private $headers = [];
23
24
    /** @var bool Set if the request sends an json */
25
    private $isJson = false;
26
27
    /**
28
     * GuzzleAdapter constructor.
29
     *
30
     * @param Client $client
31
     */
32 11
    public function __construct(Client $client)
33
    {
34 11
        $this->client = $client;
35 11
    }
36
37
    /**
38
     * Make an http request to some URL with the given http method
39
     *
40
     * @param string $method
41
     * @param string $url
42
     * @param array  $data
43
     * @param array  $config
44
     *
45
     * @return \Katapoka\Ahgora\HttpResponse
46
     */
47 3
    public function request($method, $url, $data = [], array $config = [])
48
    {
49
        // @TODO: Still, please fix me!
50 3
        return new HttpResponse([
51 3
            'httpStatus' => 200,
52 3
            'body' => json_encode(['message' => 'hello world']),
53 3
        ]);
54
    }
55
56
    /**
57
     * Make a get request to an URL.
58
     *
59
     * @param string $url
60
     * @param array  $data
61
     * @param array  $config
62
     *
63
     * @return \Katapoka\Ahgora\HttpResponse
64
     */
65 1
    public function get($url, $data = [], array $config = [])
66
    {
67 1
        return $this->request(IHttpClient::HTTP_GET, $url, $data, $config);
68
    }
69
70
    /**
71
     * Make a post request to an URL.
72
     *
73
     * @param string $url
74
     * @param array  $data
75
     * @param array  $config
76
     *
77
     * @return \Katapoka\Ahgora\HttpResponse
78
     */
79 1
    public function post($url, $data = [], array $config = [])
80
    {
81 1
        return $this->request(IHttpClient::HTTP_POST, $url, $data, $config);
82
    }
83
84
    /**
85
     * Set a header to the request.
86
     *
87
     * @param string $header
88
     * @param string $value
89
     *
90
     * @return \Katapoka\Ahgora\IHttpClient the instance of the class for method chaining
91
     */
92 5
    public function setHeader($header, $value)
93
    {
94 5
        if (!is_string($header)) {
95 1
            throw new InvalidArgumentException('Header should be a string');
96
        }
97
98 5
        if (!is_string($value)) {
99 1
            throw new InvalidArgumentException('Value should be a string');
100
        }
101
102 5
        $this->headers[$header] = $value;
103
104 5
        return $this;
105
    }
106
107
    /**
108
     * Unset a header to the request.
109
     *
110
     * @param string $header
111
     *
112
     * @return \Katapoka\Ahgora\IHttpClient the instance of the class for method chaining
113
     */
114 2
    public function unsetHeader($header)
115
    {
116 2
        if ($this->headerExists($header)) {
117 2
            unset($this->headers[$header]);
118 2
        }
119
120 2
        return $this;
121
    }
122
123
    /**
124
     * Retrieves the value of a given header name.
125
     *
126
     * @param string $header
127
     *
128
     * @return string|null
129
     */
130 2
    public function getHeader($header)
131
    {
132 2
        if ($this->headerExists($header)) {
133 2
            return $this->headers[$header];
134
        }
135
136 2
        return null;
137
    }
138
139
    /**
140
     * Get all headers from the http client.
141
     *
142
     * @return array
143
     */
144 3
    public function getHeaders()
145
    {
146 3
        return $this->headers;
147
    }
148
149
    /**
150
     * Set a timeout to the connection.
151
     *
152
     * @param int $timeout
153
     *
154
     * @return \Katapoka\Ahgora\IHttpClient
155
     */
156 1
    public function setTimeout($timeout)
157
    {
158 1
        $this->timeout = $timeout;
159
160 1
        return $this;
161
    }
162
163
    /**
164
     * Set if the request will response a json instead of form data.
165
     *
166
     * @param bool $isJson
167
     *
168
     * @return \Katapoka\Ahgora\IHttpClient
169
     */
170 1
    public function setIsJson($isJson = true)
171
    {
172 1
        if (!is_bool($isJson)) {
173 1
            throw new InvalidArgumentException('IsJson should be a boolean');
174
        }
175
176 1
        $this->isJson = $isJson;
177 1
        if ($isJson) {
178 1
            $this->setHeader('content-type', 'application/json');
179 1
        } else {
180 1
            $this->unsetHeader('content-type');
181
        }
182
183 1
        return $this;
184
    }
185
186 3
    private function headerExists($header)
187
    {
188 3
        return array_key_exists($header, $this->headers);
189
    }
190
}
191