Conditions | 10 |
Paths | 36 |
Total Lines | 75 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php namespace Cviebrock\LaravelElasticsearch; |
||
49 | protected function buildClient(array $config): Client |
||
50 | { |
||
51 | |||
52 | $clientBuilder = ClientBuilder::create(); |
||
53 | |||
54 | // Configure hosts |
||
55 | |||
56 | $clientBuilder->setHosts($config['hosts']); |
||
57 | |||
58 | // Configure logging |
||
59 | |||
60 | if (array_get($config, 'logging')) { |
||
61 | $logObject = array_get($config, 'logObject'); |
||
62 | $logPath = array_get($config, 'logPath'); |
||
63 | $logLevel = array_get($config, 'logLevel'); |
||
64 | if ($logObject && $logObject instanceof LoggerInterface) { |
||
65 | $clientBuilder->setLogger($logObject); |
||
66 | } else if ($logPath && $logLevel) { |
||
67 | $logObject = ClientBuilder::defaultLogger($logPath, $logLevel); |
||
68 | $clientBuilder->setLogger($logObject); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | // Set additional client configuration |
||
73 | |||
74 | foreach ($this->configMappings as $key => $method) { |
||
75 | $value = array_get($config, $key); |
||
76 | if ($value !== null) { |
||
77 | call_user_func([$clientBuilder, $method], $value); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | foreach($config['hosts'] as $c) { |
||
82 | if( $c['aws'] ) { |
||
83 | $clientBuilder->setHandler( function(array $request) use($c) { |
||
84 | $psr7Handler = \Aws\default_http_handler(); |
||
85 | $signer = new \Aws\Signature\SignatureV4('es', $c['aws_region']); |
||
86 | $request['headers']['Host'][0] = parse_url($request['headers']['Host'][0])['host']; |
||
87 | // Create a PSR-7 request from the array passed to the handler |
||
88 | $psr7Request = new \GuzzleHttp\Psr7\Request( |
||
89 | $request['http_method'], |
||
90 | (new \GuzzleHttp\Psr7\Uri($request['uri'])) |
||
91 | ->withScheme($request['scheme']) |
||
92 | ->withHost($request['headers']['Host'][0]), |
||
93 | $request['headers'], |
||
94 | $request['body'] |
||
95 | ); |
||
96 | // Sign the PSR-7 request with credentials from the environment |
||
97 | $signedRequest = $signer->signRequest( |
||
98 | $psr7Request, |
||
99 | new \Aws\Credentials\Credentials($c['aws_key'], $c['aws_secret']) |
||
100 | ); |
||
101 | // Send the signed request to Amazon ES |
||
102 | /** @var \Psr\Http\Message\ResponseInterface $response */ |
||
103 | $response = $psr7Handler($signedRequest)->then(function (\Psr\Http\Message\ResponseInterface $response) { |
||
104 | return $response; |
||
105 | }, function ($error) { |
||
106 | return $error['response']; |
||
107 | })->wait(); |
||
108 | // Convert the PSR-7 response to a RingPHP response |
||
109 | return new \GuzzleHttp\Ring\Future\CompletedFutureArray([ |
||
110 | 'status' => $response->getStatusCode(), |
||
111 | 'headers' => $response->getHeaders(), |
||
112 | 'body' => $response->getBody()->detach(), |
||
113 | 'transfer_stats' => ['total_time' => 0], |
||
114 | 'effective_url' => (string)$psr7Request->getUri(), |
||
115 | ]); |
||
116 | }); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | // Build and return the client |
||
121 | |||
122 | return $clientBuilder->build(); |
||
123 | } |
||
124 | } |
||
125 |