Completed
Pull Request — master (#1056)
by Jonathan
03:30
created

AwsAuthV4::getCredentialProvider()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 10
nc 2
nop 0
1
<?php
2
namespace Elastica\Transport;
3
4
use Aws\Credentials\CredentialProvider;
5
use Aws\Credentials\Credentials;
6
use Aws\Signature\SignatureV4;
7
use Elastica\Connection;
8
use GuzzleHttp;
9
use GuzzleHttp\Client;
10
use GuzzleHttp\HandlerStack;
11
use GuzzleHttp\Middleware;
12
use Psr\Http\Message\RequestInterface;
13
14
class AwsAuthV4 extends Guzzle
15
{
16
    protected function _getGuzzleClient($baseUrl, $persistent = true)
17
    {
18
        if (!$persistent || !self::$_guzzleClientConnection) {
19
            $stack = HandlerStack::create(GuzzleHttp\choose_handler());
20
            $stack->push($this->getSigningMiddleware(), 'sign');
21
22
            self::$_guzzleClientConnection = new Client([
23
                'base_uri' => $baseUrl,
24
                'handler' => $stack,
25
            ]);
26
        }
27
28
        return self::$_guzzleClientConnection;
29
    }
30
31
    protected function _getBaseUrl(Connection $connection)
32
    {
33
        $this->initializePortAndScheme();
34
35
        return parent::_getBaseUrl($connection);
36
    }
37
38
    private function getSigningMiddleware()
39
    {
40
        $region = $this->getConnection()->hasParam('aws_region')
41
            ? $this->getConnection()->getParam('aws_region')
42
            : getenv('AWS_REGION');
43
        $signer = new SignatureV4('es', $region);
44
        $credProvider = $this->getCredentialProvider();
45
46
        return Middleware::mapRequest(function (RequestInterface $req) use (
47
            $signer,
48
            $credProvider
49
        ) {
50
            return $signer->signRequest($req, $credProvider()->wait());
51
        });
52
    }
53
54
    private function getCredentialProvider()
55
    {
56
        $connection = $this->getConnection();
57
        if ($connection->hasParam('aws_secret_access_key')) {
58
            return CredentialProvider::fromCredentials(new Credentials(
59
                $connection->getParam('aws_access_key_id'),
60
                $connection->getParam('aws_secret_access_key'),
61
                $connection->hasParam('aws_session_token')
62
                    ? $connection->getParam('aws_session_token')
63
                    : null
64
            ));
65
        }
66
67
        return CredentialProvider::defaultProvider();
68
    }
69
70
    private function initializePortAndScheme()
71
    {
72
        $connection = $this->getConnection();
73
        if (true === $this->getConfig($connection, 'ssl')) {
74
            $this->_scheme = 'https';
75
            $connection->setPort(443);
76
        } else {
77
            $this->_scheme = 'http';
78
            $connection->setPort(80);
79
        }
80
    }
81
82
    private function getConfig(Connection $conn, $key, $default = null)
83
    {
84
        return $conn->hasConfig($key)
85
            ? $conn->getConfig($key)
86
            : $default;
87
    }
88
}
89