Skip to content

Commit 7a7ecf8

Browse files
committed
Add ElasticSearchClientFactory
1 parent 7709f6b commit 7a7ecf8

File tree

3 files changed

+107
-76
lines changed

3 files changed

+107
-76
lines changed

src/ElasticSearchClientFactory.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace Elasticquent;
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+
final class ElasticSearchClientFactory
13+
{
14+
use ElasticquentConfigTrait;
15+
16+
/**
17+
* @var array
18+
*/
19+
private $config;
20+
21+
/**
22+
* ElasticSearchClientFactory constructor.
23+
*/
24+
public function __construct()
25+
{
26+
/* @var array $config */
27+
$this->config = $this->getElasticConfig();
28+
}
29+
30+
/**
31+
* @return \Elasticsearch\Client
32+
*/
33+
public function getClient()
34+
{
35+
// elasticsearch v2.0 using builder
36+
if (class_exists('\Elasticsearch\ClientBuilder')) {
37+
// elasticsearch v2.0 using builder
38+
$awsConfig = $this->getElasticConfig('aws');
39+
if (!empty($awsConfig) && array_get($this->getElasticConfig('aws'), 'iam', false)) {
40+
if ($handler = $this->getAwsESHandler()) {
41+
array_set($this->config, 'handler', $handler);
42+
}
43+
}
44+
45+
return \Elasticsearch\ClientBuilder::fromConfig($this->config);
46+
}
47+
48+
// elasticsearch v1
49+
return new \Elasticsearch\Client($this->config);
50+
}
51+
52+
/**
53+
* @return bool|\Closure
54+
*/
55+
private function getAwsESHandler()
56+
{
57+
$awsConfig = $this->getElasticConfig('aws');
58+
if (empty($awsConfig)) {
59+
return false;
60+
}
61+
62+
$key = array_get($awsConfig, 'key');
63+
$secret = array_get($awsConfig, 'secret');
64+
$region = array_get($awsConfig, 'region', 'us-west-2');
65+
66+
$psr7Handler = \Aws\default_http_handler();
67+
$signer = new SignatureV4('es', $region);
68+
69+
$handler = function (array $request) use (
70+
$psr7Handler,
71+
$signer,
72+
$key,
73+
$secret
74+
) {
75+
// Amazon ES listens on standard ports (443 for HTTPS, 80 for HTTP).
76+
$request['headers']['host'][0] = parse_url($request['headers']['host'][0], PHP_URL_HOST);
77+
78+
$credentials = new Credentials($key, $secret);
79+
80+
// 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']);
84+
85+
// Sign the PSR-7 request with credentials from the environment
86+
$signedRequest = $signer->signRequest($psr7Request, $credentials);
87+
88+
// Send the signed request to Amazon ES
89+
/** @var ResponseInterface $response */
90+
$response = $psr7Handler($signedRequest)->wait();
91+
92+
// Convert the PSR-7 response to a RingPHP response
93+
return new CompletedFutureArray([
94+
'status' => $response->getStatusCode(),
95+
'headers' => $response->getHeaders(),
96+
'body' => $response->getBody()->detach(),
97+
'transfer_stats' => ['total_time' => 0],
98+
'effective_url' => (string) $psr7Request->getUri(),
99+
]);
100+
};
101+
102+
return $handler;
103+
}
104+
}

src/ElasticquentClientTrait.php

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22

33
namespace Elasticquent;
44

5-
use GuzzleHttp\Psr7\Request;
6-
use Aws\Signature\SignatureV4;
7-
use Aws\Credentials\Credentials;
8-
use GuzzleHttp\Ring\Future\CompletedFutureArray;
9-
use GuzzleHttp\Psr7\Uri;
10-
use Psr\Http\Message\ResponseInterface;
11-
125
trait ElasticquentClientTrait
136
{
147
use ElasticquentConfigTrait;
@@ -20,74 +13,8 @@ trait ElasticquentClientTrait
2013
*/
2114
public function getElasticSearchClient()
2215
{
23-
$config = $this->getElasticConfig();
24-
25-
// elasticsearch v2.0 using builder
26-
if (class_exists('\Elasticsearch\ClientBuilder')) {
27-
$awsConfig = $this->getElasticConfig('aws');
28-
if (!empty($awsConfig) && array_get($this->getElasticConfig('aws'), 'iam', false)) {
29-
if ($handler = $this->getAwsESHandler()) {
30-
array_set($config, 'handler', $handler);
31-
}
32-
}
33-
34-
return \Elasticsearch\ClientBuilder::fromConfig($config);
35-
}
36-
37-
// elasticsearch v1
38-
return new \Elasticsearch\Client($config);
39-
}
40-
41-
/**
42-
* @return bool|\Closure
43-
*/
44-
private function getAwsESHandler()
45-
{
46-
$awsConfig = $this->getElasticConfig('aws');
47-
if (empty($awsConfig)) {
48-
return false;
49-
}
50-
51-
$key = array_get($awsConfig, 'key');
52-
$secret = array_get($awsConfig, 'secret');
53-
$region = array_get($awsConfig, 'region', 'us-west-2');
54-
55-
$psr7Handler = \Aws\default_http_handler();
56-
$signer = new SignatureV4('es', $region);
57-
58-
$handler = function (array $request) use (
59-
$psr7Handler,
60-
$signer,
61-
$key,
62-
$secret
63-
) {
64-
// Amazon ES listens on standard ports (443 for HTTPS, 80 for HTTP).
65-
$request['headers']['host'][0] = parse_url($request['headers']['host'][0], PHP_URL_HOST);
66-
67-
$credentials = new Credentials($key, $secret);
68-
69-
// Create a PSR-7 request from the array passed to the handler
70-
$psr7Request = new Request($request['http_method'],
71-
(new Uri($request['uri']))->withScheme($request['scheme'])->withHost($request['headers']['host'][0]),
72-
$request['headers'], $request['body']);
73-
74-
// Sign the PSR-7 request with credentials from the environment
75-
$signedRequest = $signer->signRequest($psr7Request, $credentials);
76-
77-
// Send the signed request to Amazon ES
78-
/** @var ResponseInterface $response */
79-
$response = $psr7Handler($signedRequest)->wait();
80-
81-
// Convert the PSR-7 response to a RingPHP response
82-
return new CompletedFutureArray([
83-
'status' => $response->getStatusCode(),
84-
'headers' => $response->getHeaders(),
85-
'body' => $response->getBody()->detach(),
86-
'transfer_stats' => ['total_time' => 0],
87-
'effective_url' => (string) $psr7Request->getUri(),
88-
]);
89-
};
16+
$factory = new ElasticSearchClientFactory();
9017

91-
return $handler;
18+
return $factory->getClient();
9219
}
9320
}

src/ElasticquentTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Elasticquent;
44

55
use Exception;
6-
use ReflectionMethod;
76
use Illuminate\Database\Eloquent\Model;
87
use Illuminate\Database\Eloquent\Relations\Relation;
8+
use ReflectionMethod;
99

1010
/**
1111
* Elasticquent Trait.

0 commit comments

Comments
 (0)