2
2
3
3
namespace Elasticquent ;
4
4
5
- use Aws \Credentials \Credentials ;
6
- use Aws \Signature \SignatureV4 ;
7
- use GuzzleHttp \Psr7 \Request ;
8
- use GuzzleHttp \Psr7 \Uri ;
9
- use GuzzleHttp \Ring \Future \CompletedFutureArray ;
10
- use Psr \Http \Message \ResponseInterface ;
11
-
12
5
final class ElasticSearchClientFactory
13
6
{
14
7
use ElasticquentConfigTrait;
@@ -54,6 +47,21 @@ public function getClient()
54
47
*/
55
48
private function getAwsESHandler ()
56
49
{
50
+ $ classExistsChecks = [
51
+ '\Aws\Credentials\Credentials ' ,
52
+ '\Aws\Signature\SignatureV4 ' ,
53
+ '\GuzzleHttp\Psr7\Request ' ,
54
+ '\GuzzleHttp\Psr7\Uri ' ,
55
+ '\GuzzleHttp\Ring\Future\CompletedFutureArray ' ,
56
+ '\Psr\Http\Message\ResponseInterface ' ,
57
+ ];
58
+
59
+ foreach ($ classExistsChecks as $ classExistsCheck ) {
60
+ if (!class_exists ($ classExistsCheck )) {
61
+ return false ;
62
+ }
63
+ }
64
+
57
65
$ awsConfig = $ this ->getElasticConfig ('aws ' );
58
66
if (empty ($ awsConfig )) {
59
67
return false ;
@@ -64,7 +72,7 @@ private function getAwsESHandler()
64
72
$ region = array_get ($ awsConfig , 'region ' , 'us-west-2 ' );
65
73
66
74
$ psr7Handler = \Aws \default_http_handler ();
67
- $ signer = new SignatureV4 ('es ' , $ region );
75
+ $ signer = new \ Aws \ Signature \ SignatureV4 ('es ' , $ region );
68
76
69
77
$ handler = function (array $ request ) use (
70
78
$ psr7Handler ,
@@ -75,22 +83,22 @@ private function getAwsESHandler()
75
83
// Amazon ES listens on standard ports (443 for HTTPS, 80 for HTTP).
76
84
$ request ['headers ' ]['host ' ][0 ] = parse_url ($ request ['headers ' ]['host ' ][0 ], PHP_URL_HOST );
77
85
78
- $ credentials = new Credentials ($ key , $ secret );
86
+ $ credentials = new \ Aws \ Credentials \ Credentials ($ key , $ secret );
79
87
80
88
// Create a PSR-7 request from the array passed to the handler
81
- $ psr7Request = new Request ($ request ['http_method ' ],
82
- (new Uri ($ request ['uri ' ]))->withScheme ($ request ['scheme ' ])->withHost ($ request ['headers ' ]['host ' ][0 ]), $ request [ ' headers ' ] ,
83
- $ request ['body ' ]);
89
+ $ psr7Request = new \ GuzzleHttp \ Psr7 \ Request ($ request ['http_method ' ],
90
+ (new \ GuzzleHttp \ Psr7 \ Uri ($ request ['uri ' ]))->withScheme ($ request ['scheme ' ])->withHost ($ request ['headers ' ]['host ' ][0 ]),
91
+ $ request ['headers ' ], $ request [ ' body ' ]);
84
92
85
93
// Sign the PSR-7 request with credentials from the environment
86
94
$ signedRequest = $ signer ->signRequest ($ psr7Request , $ credentials );
87
95
88
96
// Send the signed request to Amazon ES
89
- /** @var ResponseInterface $response */
97
+ /** @var \Psr\Http\Message\ ResponseInterface $response */
90
98
$ response = $ psr7Handler ($ signedRequest )->wait ();
91
99
92
100
// Convert the PSR-7 response to a RingPHP response
93
- return new CompletedFutureArray ([
101
+ return new \ GuzzleHttp \ Ring \ Future \ CompletedFutureArray ([
94
102
'status ' => $ response ->getStatusCode (),
95
103
'headers ' => $ response ->getHeaders (),
96
104
'body ' => $ response ->getBody ()->detach (),
0 commit comments