Skip to content

Commit 9235384

Browse files
committed
TASK: Adjust requests and responses to Flow 6 PSR-7 interfaces
1 parent b8e34f9 commit 9235384

File tree

4 files changed

+87
-69
lines changed

4 files changed

+87
-69
lines changed

Classes/Domain/Model/Client/ClientConfiguration.php

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace Flowpack\ElasticSearch\Domain\Model\Client;
35

46
/*
@@ -11,7 +13,10 @@
1113
* source code.
1214
*/
1315

14-
use Neos\Flow\Http\Uri;
16+
use GuzzleHttp\Psr7\Uri;
17+
use Neos\Flow\Annotations as Flow;
18+
use Neos\Http\Factories\UriFactory;
19+
use Psr\Http\Message\UriInterface;
1520

1621
/**
1722
* Client configuration
@@ -43,10 +48,16 @@ class ClientConfiguration
4348
*/
4449
protected $password = '';
4550

51+
/**
52+
* @Flow\Inject
53+
* @var UriFactory
54+
*/
55+
protected $uriFactory;
56+
4657
/**
4758
* @return string
4859
*/
49-
public function getHost()
60+
public function getHost(): string
5061
{
5162
return $this->host;
5263
}
@@ -55,15 +66,15 @@ public function getHost()
5566
* @param string $host
5667
* @return void
5768
*/
58-
public function setHost($host)
69+
public function setHost(string $host): void
5970
{
6071
$this->host = $host;
6172
}
6273

6374
/**
6475
* @return int
6576
*/
66-
public function getPort()
77+
public function getPort(): int
6778
{
6879
return $this->port;
6980
}
@@ -72,15 +83,15 @@ public function getPort()
7283
* @param int $port
7384
* @return void
7485
*/
75-
public function setPort($port)
86+
public function setPort(int $port): void
7687
{
7788
$this->port = $port;
7889
}
7990

8091
/**
8192
* @return string
8293
*/
83-
public function getScheme()
94+
public function getScheme(): string
8495
{
8596
return $this->scheme;
8697
}
@@ -89,7 +100,7 @@ public function getScheme()
89100
* @param string $scheme
90101
* @return void
91102
*/
92-
public function setScheme($scheme)
103+
public function setScheme(string $scheme): void
93104
{
94105
$this->scheme = $scheme;
95106
}
@@ -99,7 +110,7 @@ public function setScheme($scheme)
99110
*
100111
* @return string
101112
*/
102-
public function getUsername()
113+
public function getUsername(): string
103114
{
104115
return $this->username;
105116
}
@@ -110,7 +121,7 @@ public function getUsername()
110121
* @param string $username
111122
* @return void
112123
*/
113-
public function setUsername($username)
124+
public function setUsername(string $username): void
114125
{
115126
$this->username = $username;
116127
}
@@ -120,7 +131,7 @@ public function setUsername($username)
120131
*
121132
* @return string
122133
*/
123-
public function getPassword()
134+
public function getPassword(): string
124135
{
125136
return $this->password;
126137
}
@@ -131,23 +142,20 @@ public function getPassword()
131142
* @param string $password
132143
* @return void
133144
*/
134-
public function setPassword($password)
145+
public function setPassword(string $password): void
135146
{
136147
$this->password = $password;
137148
}
138149

139150
/**
140-
* @return Uri
151+
* @return UriInterface
141152
*/
142-
public function getUri()
153+
public function getUri(): UriInterface
143154
{
144-
$uri = new Uri('');
145-
$uri->setScheme($this->scheme);
146-
$uri->setHost($this->host);
147-
$uri->setPort($this->port);
148-
$uri->setUsername($this->username);
149-
$uri->setPassword($this->password);
150-
151-
return $uri;
155+
return $this->uriFactory->createUri()
156+
->withScheme($this->scheme)
157+
->withHost($this->host)
158+
->withPort($this->port)
159+
->withUserInfo($this->username, $this->password);
152160
}
153161
}

Classes/Transfer/Exception.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace Flowpack\ElasticSearch\Transfer;
35

46
/*
@@ -10,35 +12,27 @@
1012
* information, please view the LICENSE file which was distributed with this
1113
* source code.
1214
*/
15+
1316
use Flowpack\ElasticSearch\Exception as ElasticSearchException;
14-
use Neos\Flow\Http\Request;
15-
use Neos\Flow\Http\Response as FlowResponse;
17+
use Psr\Http\Message\RequestInterface;
18+
use Psr\Http\Message\ResponseInterface;
1619

1720
/**
1821
* Exception that occurs related to ElasticSearch transfers
1922
*/
2023
class Exception extends ElasticSearchException
2124
{
2225
/**
23-
* @var FlowResponse
26+
* @var ResponseInterface
2427
*/
2528
protected $response;
2629

2730
/**
28-
* @var Request
31+
* @var RequestInterface
2932
*/
3033
protected $request;
3134

32-
/**
33-
* Exception constructor.
34-
*
35-
* @param string $message
36-
* @param int $code
37-
* @param FlowResponse $response
38-
* @param Request $request
39-
* @param \Exception $previous
40-
*/
41-
public function __construct($message, $code, FlowResponse $response, Request $request = null, \Exception $previous = null)
35+
public function __construct($message, $code, ResponseInterface $response, RequestInterface $request = null, \Exception $previous = null)
4236
{
4337
$this->response = $response;
4438
$this->request = $request;
@@ -47,26 +41,26 @@ public function __construct($message, $code, FlowResponse $response, Request $re
4741
"Elasticsearch request failed.\n[%s %s]: %s\n\nRequest data: %s",
4842
$request->getMethod(),
4943
$request->getUri(),
50-
$message . '; Response body: ' . $response->getContent(),
51-
$request->getContent()
44+
$message . '; Response body: ' . $response->getBody()->getContents(),
45+
$request->getBody()->getContents()
5246
);
5347
}
5448

5549
parent::__construct($message, $code, $previous);
5650
}
5751

5852
/**
59-
* @return Request
53+
* @return RequestInterface
6054
*/
61-
public function getRequest()
55+
public function getRequest(): RequestInterface
6256
{
6357
return $this->request;
6458
}
6559

6660
/**
67-
* @return FlowResponse
61+
* @return ResponseInterface
6862
*/
69-
public function getResponse()
63+
public function getResponse(): ResponseInterface
7064
{
7165
return $this->response;
7266
}

Classes/Transfer/RequestService.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
namespace Flowpack\ElasticSearch\Transfer;
34

45
/*
@@ -16,7 +17,8 @@
1617
use Neos\Flow\Annotations as Flow;
1718
use Neos\Flow\Http\Client\Browser;
1819
use Neos\Flow\Http\Client\CurlEngine;
19-
use Neos\Flow\Http\Request;
20+
use Psr\Http\Message\ServerRequestFactoryInterface;
21+
use Psr\Http\Message\StreamFactoryInterface;
2022

2123
/**
2224
* Handles the requests
@@ -31,6 +33,18 @@ class RequestService
3133
*/
3234
protected $browser;
3335

36+
/**
37+
* @Flow\Inject
38+
* @var ServerRequestFactoryInterface
39+
*/
40+
protected $requestFactory;
41+
42+
/**
43+
* @Flow\Inject
44+
* @var StreamFactoryInterface
45+
*/
46+
protected $contentStreamFactory;
47+
3448
/**
3549
* @var array
3650
*/
@@ -62,36 +76,34 @@ public function initializeObject()
6276
* @param array $arguments
6377
* @param string|array $content
6478
* @return Response
79+
* @throws Exception
80+
* @throws Exception\ApiException
81+
* @throws \Neos\Flow\Http\Exception
6582
*/
66-
public function request($method, ElasticSearchClient $client, $path = null, $arguments = [], $content = null)
83+
public function request($method, ElasticSearchClient $client, ?string $path = null, array $arguments = [], $content = null): Response
6784
{
6885
$clientConfigurations = $client->getClientConfigurations();
6986
$clientConfiguration = $clientConfigurations[0];
7087
/** @var ClientConfiguration $clientConfiguration */
7188

7289
$uri = clone $clientConfiguration->getUri();
90+
7391
if ($path !== null) {
7492
if (strpos($path, '?') !== false) {
7593
list($path, $query) = explode('?', $path);
76-
$uri->setQuery($query);
94+
$uri->withQuery($query);
7795
}
78-
$uri->setPath($uri->getPath() . $path);
96+
$uri->withPath($uri->getPath() . $path);
7997
}
8098

81-
$request = Request::create($uri, $method, $arguments, [], []);
99+
$request = $this->requestFactory->createServerRequest($method, $uri);
100+
82101
// In some cases, $content will contain "null" as a string. Better be safe and handle this weird case:
83-
if ($content === 'null') {
84-
$request->setContent(null);
85-
} else {
86-
$request->setContent((is_array($content) ? json_encode($content) : $content));
87-
}
88-
if ($uri->getUsername()) {
89-
$requestUri = $request->getUri();
90-
$requestUri->setUsername($uri->getUsername());
91-
$requestUri->setPassword($uri->getPassword());
102+
if ($content !== 'null') {
103+
$request->withBody($this->contentStreamFactory->createStream((is_array($content) ? json_encode($content) : $content)));
92104
}
93-
94-
$request->setHeader('Content-Type', 'application/json');
105+
106+
$request->withHeader('Content-Type', 'application/json');
95107
$response = $this->browser->sendRequest($request);
96108

97109
return new Response($response, $this->browser->getLastRequest());

Classes/Transfer/Response.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace Flowpack\ElasticSearch\Transfer;
35

46
/*
@@ -11,13 +13,13 @@
1113
* source code.
1214
*/
1315

14-
use Neos\Flow\Http\Request;
15-
use Neos\Flow\Http\Response as HttpResponse;
16+
use Psr\Http\Message\RequestInterface;
17+
use Psr\Http\Message\ResponseInterface;
1618

1719
class Response
1820
{
1921
/**
20-
* @var HttpResponse
22+
* @var ResponseInterface
2123
*/
2224
protected $originalResponse;
2325

@@ -29,20 +31,22 @@ class Response
2931
protected $treatedContent;
3032

3133
/**
32-
* @param HttpResponse $response
33-
* @param Request $request
34+
* Response constructor.
35+
* @param ResponseInterface $response
36+
* @param RequestInterface|null $request
3437
* @throws Exception
3538
* @throws Exception\ApiException
3639
*/
37-
public function __construct(HttpResponse $response, Request $request = null)
40+
public function __construct(ResponseInterface $response, RequestInterface $request = null)
3841
{
3942
$this->originalResponse = $response;
4043

41-
$treatedContent = json_decode($response->getContent(), true);
44+
$content = $response->getBody()->getContents();
45+
$treatedContent = json_decode($content, true);
4246

43-
if (strlen($response->getContent()) > 0) {
47+
if (strlen($content) > 0) {
4448
if ($treatedContent === null) {
45-
throw new Exception('The request returned an invalid JSON string which was "' . $response->getContent() . '".', 1338976439, $response, $request);
49+
throw new Exception('The request returned an invalid JSON string which was "' . $content . '".', 1338976439, $response, $request);
4650
}
4751

4852
if (array_key_exists('error', $treatedContent)) {
@@ -59,7 +63,7 @@ public function __construct(HttpResponse $response, Request $request = null)
5963
*
6064
* @return int
6165
*/
62-
public function getStatusCode()
66+
public function getStatusCode(): int
6367
{
6468
return $this->originalResponse->getStatusCode();
6569
}
@@ -73,9 +77,9 @@ public function getTreatedContent()
7377
}
7478

7579
/**
76-
* @return HttpResponse
80+
* @return ResponseInterface
7781
*/
78-
public function getOriginalResponse()
82+
public function getOriginalResponse(): ResponseInterface
7983
{
8084
return $this->originalResponse;
8185
}

0 commit comments

Comments
 (0)