| Conditions | 12 |
| Paths | 48 |
| Total Lines | 81 |
| 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; |
||
| 50 | protected function buildClient(array $config): Client |
||
| 51 | { |
||
| 52 | |||
| 53 | $clientBuilder = ClientBuilder::create(); |
||
| 54 | |||
| 55 | // Configure hosts |
||
| 56 | |||
| 57 | $clientBuilder->setHosts($config['hosts']); |
||
| 58 | |||
| 59 | // Configure logging |
||
| 60 | |||
| 61 | if (array_get($config, 'logging')) { |
||
| 62 | $logObject = array_get($config, 'logObject'); |
||
| 63 | $logPath = array_get($config, 'logPath'); |
||
| 64 | $logLevel = array_get($config, 'logLevel'); |
||
| 65 | if ($logObject && $logObject instanceof LoggerInterface) { |
||
| 66 | $clientBuilder->setLogger($logObject); |
||
| 67 | } else if ($logPath && $logLevel) { |
||
| 68 | $logObject = ClientBuilder::defaultLogger($logPath, $logLevel); |
||
| 69 | $clientBuilder->setLogger($logObject); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | // Set additional client configuration |
||
| 74 | |||
| 75 | foreach ($this->configMappings as $key => $method) { |
||
| 76 | $value = array_get($config, $key); |
||
| 77 | if ($value !== null) { |
||
| 78 | if (is_array($value)) { |
||
| 79 | foreach ($value as $vItem) { |
||
| 80 | call_user_func([$clientBuilder, $method], $vItem); |
||
| 81 | } |
||
| 82 | } else { |
||
| 83 | call_user_func([$clientBuilder, $method], $value); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | foreach($config['hosts'] as $c) { |
||
| 89 | if( $c['aws'] ) { |
||
| 90 | $clientBuilder->setHandler( function(array $request) use($c) { |
||
| 91 | $psr7Handler = \Aws\default_http_handler(); |
||
| 92 | $signer = new \Aws\Signature\SignatureV4('es', $c['aws_region']); |
||
| 93 | $request['headers']['Host'][0] = parse_url($request['headers']['Host'][0])['host']; |
||
| 94 | // Create a PSR-7 request from the array passed to the handler |
||
| 95 | $psr7Request = new \GuzzleHttp\Psr7\Request( |
||
| 96 | $request['http_method'], |
||
| 97 | (new \GuzzleHttp\Psr7\Uri($request['uri'])) |
||
| 98 | ->withScheme($request['scheme']) |
||
| 99 | ->withHost($request['headers']['Host'][0]), |
||
| 100 | $request['headers'], |
||
| 101 | $request['body'] |
||
| 102 | ); |
||
| 103 | // Sign the PSR-7 request with credentials from the environment |
||
| 104 | $signedRequest = $signer->signRequest( |
||
| 105 | $psr7Request, |
||
| 106 | new \Aws\Credentials\Credentials($c['aws_key'], $c['aws_secret']) |
||
| 107 | ); |
||
| 108 | // Send the signed request to Amazon ES |
||
| 109 | /** @var \Psr\Http\Message\ResponseInterface $response */ |
||
| 110 | $response = $psr7Handler($signedRequest)->then(function (\Psr\Http\Message\ResponseInterface $response) { |
||
| 111 | return $response; |
||
| 112 | }, function ($error) { |
||
| 113 | return $error['response']; |
||
| 114 | })->wait(); |
||
| 115 | // Convert the PSR-7 response to a RingPHP response |
||
| 116 | return new \GuzzleHttp\Ring\Future\CompletedFutureArray([ |
||
| 117 | 'status' => $response->getStatusCode(), |
||
| 118 | 'headers' => $response->getHeaders(), |
||
| 119 | 'body' => $response->getBody()->detach(), |
||
| 120 | 'transfer_stats' => ['total_time' => 0], |
||
| 121 | 'effective_url' => (string)$psr7Request->getUri(), |
||
| 122 | ]); |
||
| 123 | }); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | // Build and return the client |
||
| 128 | |||
| 129 | return $clientBuilder->build(); |
||
| 130 | } |
||
| 131 | } |
||
| 132 |