Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

GuzzleDriver.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient;
8
9 use Guzzle\Http\Client;
10 use Guzzle\Http\Message\Header;
11 use Guzzle\Http\Exception\BadResponseException;
12
13 class GuzzleDriver implements DriverInterface
14 {
15 use DriverHelper;
16
18 protected $sent = false;
19
21 protected $client;
22
24 protected $request;
25
27 protected $response;
28
30 protected $host;
31
33 protected $resource;
34
36 protected $method;
37
39 protected $headers;
40
42 protected $body;
43
47 public function __construct()
48 {
49 $this->client = new Client();
50 }
51
59 protected function getResponse()
60 {
61 if ($this->sent) {
62 return $this->response;
63 }
64
65 throw new \RuntimeException("Attempt to get response data when request hasn't been sent yet");
66 }
67
71 public function send()
72 {
73 // make a request object
74 $this->request = $this->client->createRequest(
75 $this->method,
76 $this->host . $this->resource
77 );
78
79 // set headers
80 foreach ($this->headers as $header => $value) {
81 $this->request->setHeader($header, $value);
82 }
83
84 // set body
85 if (!empty($this->body)) {
86 $this->request->setBody($this->body);
87 }
88
89 try {
90 // finally send the request
91 $this->response = $this->client->send($this->request);
92 } catch (BadResponseException $e) {
93 // if the response is an 40x or a 50x then it will throw an exception
94 // we catch and get the response stored on the request object
95 $this->response = $this->request->getResponse();
96 }
97
98 $this->sent = true;
99 }
100
106 public function setHost($host)
107 {
108 if (substr($host, -1) === '/') {
109 $host = substr($host, 0, strlen($host) - 1);
110 }
111
112 $this->host = $host;
113 }
114
120 public function setResource($resource)
121 {
122 $this->resource = $resource;
123 }
124
130 public function setMethod($method)
131 {
132 $this->method = $method;
133 }
134
142 public function getStatusCode()
143 {
144 return $this->getResponse()->getStatusCode();
145 }
146
154 public function getStatusMessage()
155 {
156 return $this->getResponse()->getReasonPhrase();
157 }
158
164 public function setHeader($header, $value)
165 {
166 if (is_array($value)) {
167 $value = implode(';', $value);
168 }
169
170 $this->headers[$header] = $value;
171 }
172
180 public function getHeaders()
181 {
182 $headers = [];
183 foreach ($this->response->getHeaders()->getAll() as $header => $headerObject) {
184 $allHeaderValues = $headerObject->toArray();
185 $headers[strtolower($header)] = implode(';', $allHeaderValues);
186 }
187
188 return $headers;
189 }
190
198 public function getBody()
199 {
200 $bodyObject = $this->response->getBody();
201
202 $bodyObject->rewind();
203
204 $length = $bodyObject->getContentLength();
205 if ($length === false || $length <= 0) {
206 return '';
207 }
208
209 return $bodyObject->read($length);
210 }
211
217 public function setBody($body)
218 {
219 $this->body = $body;
220 }
221 }
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver
Definition: GuzzleDriver.php:14
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\$host
$host
Definition: GuzzleDriver.php:30
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\getStatusMessage
getStatusMessage()
Get response status message.
Definition: GuzzleDriver.php:154
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\setMethod
setMethod($method)
Set request method.
Definition: GuzzleDriver.php:130
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\setBody
setBody($body)
Set request body.
Definition: GuzzleDriver.php:217
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\__construct
__construct()
Instanciate a client.
Definition: GuzzleDriver.php:47
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\$request
$request
Definition: GuzzleDriver.php:24
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\$resource
$resource
Definition: GuzzleDriver.php:33
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\getHeaders
getHeaders()
Get all response headers.
Definition: GuzzleDriver.php:180
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\$sent
$sent
Definition: GuzzleDriver.php:18
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\setResource
setResource($resource)
Set request resource url.
Definition: GuzzleDriver.php:120
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\send
send()
Send the request.
Definition: GuzzleDriver.php:71
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\$client
$client
Definition: GuzzleDriver.php:21
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\$headers
$headers
Definition: GuzzleDriver.php:39
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\$method
$method
Definition: GuzzleDriver.php:36
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\$response
$response
Definition: GuzzleDriver.php:27
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\setHost
setHost($host)
Set request host.
Definition: GuzzleDriver.php:106
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\$body
$body
Definition: GuzzleDriver.php:42
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\getBody
getBody()
Get response body.
Definition: GuzzleDriver.php:198
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\getResponse
getResponse()
Get reponse.
Definition: GuzzleDriver.php:59
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\setHeader
setHeader($header, $value)
Set request header.
Definition: GuzzleDriver.php:164
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\GuzzleDriver\getStatusCode
getStatusCode()
Get response status code.
Definition: GuzzleDriver.php:142
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient\DriverInterface
DriverInterface has the definition of the methods needed for our REST tests so Drivers need to implem...
Definition: DriverInterface.php:17
eZ\Bundle\EzPublishRestBundle\Features\Context\RestClient
Definition: BuzzDriver.php:7
host-p
Returns the Data Source Name as a structure containing the various parts of the DSN.
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.