Completed
Pull Request — master (#75)
by
unknown
03:47
created

ApiClientContext::getResponse()   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 0
1
<?php
2
3
/*
4
 * This file is part of the Behat WebApiExtension.
5
 *
6
 * (c) Konstantin Kudryashov <[email protected]>
7
 * (c) Keyclic team <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Behat\WebApiExtension\Context;
14
15
use GuzzleHttp\ClientInterface;
16
use GuzzleHttp\Exception\GuzzleException;
17
use GuzzleHttp\Exception\RequestException;
18
use GuzzleHttp\Psr7\Request;
19
use Psr\Http\Message\RequestInterface;
20
use Psr\Http\Message\ResponseInterface;
21
22
/**
23
 * Provides methods without Features methods.
24
 */
25
abstract class ApiClientContext implements ApiClientContextInterface
26
{
27
    /**
28
     * @var ClientInterface
29
     */
30
    private $client;
31
32
    /**
33
     * @var array
34
     */
35
    private $headers = [];
36
37
    /**
38
     * @var array
39
     */
40
    private $placeHolders = [];
41
42
    /**
43
     * @var RequestInterface
44
     */
45
    private $request;
46
47
    /**
48
     * @var ResponseInterface
49
     */
50
    private $response;
51
52
    /**
53
     * @return ClientInterface
54
     */
55
    private function getClient()
56
    {
57
        if (null === $this->client) {
58
            throw new \RuntimeException('Client has not been set in WebApiContext.');
59
        }
60
61
        return $this->client;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function setClient(ClientInterface $client)
68
    {
69
        $this->client = $client;
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    public function getHeaders()
76
    {
77
        return $this->headers;
78
    }
79
80
    /**
81
     * Adds header.
82
     *
83
     * @param string $name
84
     * @param string $value
85
     *
86
     * @return ApiClientContext
87
     */
88
    public function addHeader($name, $value)
89
    {
90
        if (isset($this->headers[$name])
91
        && true === is_array($this->headers[$name])) {
92
            array_push($this->headers[$name], $value);
93
94
            return $this;
95
        }
96
97
        if (isset($this->headers[$name])
98
        && false === is_array($this->headers[$name])) {
99
            $this->headers[$name] = [
100
                $this->headers[$name],
101
                $value,
102
            ];
103
104
            return $this;
105
        }
106
107
        $this->headers[$name] = $value;
108
109
        return $this;
110
    }
111
112
    /**
113
     * Removes a header identified by $name.
114
     *
115
     * @param string $name
116
     *
117
     * @return ApiClientContext
118
     */
119
    public function removeHeader($name)
120
    {
121
        if (array_key_exists($name, $this->headers)) {
122
            unset($this->headers[$name]);
123
        }
124
125
        return $this;
126
    }
127
128
    /**
129
     * Sets place holder for replacement.
130
     *
131
     * you can specify placeholders, which will
132
     * be replaced in URL, request or response body.
133
     *
134
     * @param string $key
135
     * @param string $value replace value
136
     *
137
     * @return ApiClientContext
138
     *
139
     * @deprecated
140
     */
141
    public function setPlaceHolder($key, $value)
142
    {
143
        return $this->addPlaceholder($key, $value);
144
    }
145
146
    /**
147
     * Add place holder for replacement.
148
     *
149
     * you can specify placeholders, which will
150
     * be replaced in URL, request or response body.
151
     *
152
     * @param string $key
153
     * @param string $value replace value
154
     *
155
     * @return ApiClientContext
156
     */
157
    public function addPlaceholder($key, $value)
158
    {
159
        $this->placeHolders[$key] = $value;
160
161
        return $this;
162
    }
163
164
    /**
165
     * Removes a placeholder identified by $key.
166
     *
167
     * @param string $key token name
168
     *
169
     * @return ApiClientContext
170
     */
171
    public function removePlaceHolder($key)
172
    {
173
        if (array_key_exists($key, $this->placeHolders)) {
174
            unset($this->placeHolders[$key]);
175
        }
176
177
        return $this;
178
    }
179
180
    /**
181
     * @return RequestInterface
182
     */
183
    public function getRequest()
184
    {
185
        return $this->request;
186
    }
187
188
    /**
189
     * @return ResponseInterface
190
     */
191
    public function getResponse()
192
    {
193
        return $this->response;
194
    }
195
196
    /**
197
     * @param string      $method
198
     * @param string      $url
199
     * @param array       $headers
200
     * @param string|null $body
201
     *
202
     * @throws GuzzleException
203
     */
204
    protected function sendRequest($method, $url, array $headers = [], $body = null)
205
    {
206
        $this->request = new Request($method, $url, $headers, $body);
207
208
        try {
209
            $this->response = $this->getClient()->send($this->request);
210
        } catch (RequestException $e) {
211
            $this->response = $e->getResponse();
212
213
            if (null === $this->response) {
214
                throw $e;
215
            }
216
        }
217
    }
218
219
    /**
220
     * Replaces placeholders in provided text.
221
     *
222
     * @param string $string
223
     *
224
     * @return string
225
     */
226
    protected function replacePlaceHolder($string)
227
    {
228
        foreach ($this->placeHolders as $key => $val) {
229
            $string = str_replace($key, $val, $string);
230
        }
231
232
        return $string;
233
    }
234
235
    /**
236
     * Prepare URL by replacing placeholders and trimming slashes.
237
     *
238
     * @param string $url
239
     *
240
     * @return string
241
     */
242
    protected function prepareUrl($url)
243
    {
244
        return ltrim($this->replacePlaceHolder($url), '/');
245
    }
246
}
247