ClientFactory::getCredentials()   B
last analyzed

Complexity

Conditions 9
Paths 6

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 20.2062

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 41
ccs 14
cts 29
cp 0.4828
rs 8.0555
c 0
b 0
f 0
cc 9
nc 6
nop 1
crap 20.2062
1
<?php
2
/**
3
 *  This file is part of the Simple S3 package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 */
11
12
namespace Matecat\SimpleS3;
13
14
use Aws\Credentials\Credentials;
15
use Aws\S3\S3Client;
16
use Aws\Sts\StsClient;
17
use InvalidArgumentException;
18
19
/**
20
 * Class ClientFactory
21
 *
22
 * This class is a simple factory for S3/Client
23
 *
24
 * List of options:
25
 * - api_provider
26
 * - credentials
27
 * - debug
28
 * - stats
29
 * - endpoint
30
 * - endpoint_provider
31
 * - endpoint_discovery
32
 * - handler
33
 * - http
34
 * - http_handler
35
 * - profile
36
 * - region
37
 * - retries
38
 * - scheme
39
 * - service
40
 * - signature_provider
41
 * - signature_version
42
 * - ua_append
43
 * - validate
44
 * - version
45
 *
46
 * Please see the complete config documentation here:
47
 *
48
 * https://docs.aws.amazon.com/en_us/sdk-for-php/v3/developer-guide/guide_configuration.html
49
 *
50
 * @package SimpleS3
51
 */
52
final class ClientFactory
53
{
54
    /**
55
     * @param array $config
56
     *
57
     * @return S3Client
58
     */
59 43
    public static function create(array $config = []): S3Client
60
    {
61 43
        self::validateConfig($config);
62
63 42
        return new S3Client(self::createConfigArray($config));
64
    }
65
66
    /**
67
     * @param array $config
68
     *
69
     * @return array
70
     */
71 42
    private static function createConfigArray(array $config): array
72
    {
73 42
        $credentials = self::getCredentials($config);
74 42
        if (!empty($credentials)) {
75 42
            $config[ 'credentials' ] = new Credentials(
76 42
                    $credentials[ 'key' ],
77 42
                    $credentials[ 'secret' ],
78 42
                    $credentials[ 'token' ]
79 42
            );
80
        }
81
82
        // Temp fix: suppressing PHP < 8.1 warnings
83 42
        $config[ 'suppress_php_deprecation_warning' ] = true;
84
85 42
        return $config;
86
    }
87
88
    /**
89
     * @param array $config
90
     */
91 43
    private static function validateConfig(array $config): void
92
    {
93 43
        $allowedKeys = [
94 43
                'api_provider',
95 43
                'credentials',
96 43
                'debug',
97 43
                'endpoint',
98 43
                'endpoint_provider',
99 43
                'endpoint_discovery',
100 43
                'handler',
101 43
                'http',
102 43
                'http_handler',
103 43
                'iam',
104 43
                'profile',
105 43
                'region',
106 43
                'retries',
107 43
                'scheme',
108 43
                'service',
109 43
                'signature_provider',
110 43
                'signature_version',
111 43
                'stats',
112 43
                'ua_append',
113 43
                'validate',
114 43
                'version',
115 43
        ];
116
117 43
        foreach (array_keys($config) as $key) {
118 43
            if (!in_array($key, $allowedKeys)) {
119 1
                throw new InvalidArgumentException(sprintf('%s is not an allowed key', $key));
120
            }
121
        }
122
    }
123
124
    /**
125
     * @param array $config
126
     *
127
     * @return array
128
     */
129 42
    private static function getCredentials(array $config): array
130
    {
131
        // 1. credentials
132 42
        if (isset($config[ 'credentials' ][ 'key' ]) and isset($config[ 'credentials' ][ 'secret' ])) {
133 1
            return [
134 1
                    'key'    => $config[ 'credentials' ][ 'key' ],
135 1
                    'secret' => $config[ 'credentials' ][ 'secret' ],
136 1
                    'token'  => $config[ 'credentials' ][ 'token' ] ?? null
137 1
            ];
138
        }
139
140
        // 2. IAM
141 41
        if (isset($config[ 'iam' ])) {
142
            $stsClient = new StsClient([
143
                    'profile' => (isset($config[ 'profile' ])) ? $config[ 'profile' ] : 'default',
144
                    'region'  => $config[ 'region' ],
145
                    'version' => $config[ 'version' ]
146
            ]);
147
148
            $result = $stsClient->assumeRole([
149
                    'RoleArn'         => $config[ 'iam' ][ 'arn' ],
150
                    'RoleSessionName' => $config[ 'iam' ][ 'session' ],
151
            ]);
152
153
            return [
154
                    'key'    => $result[ 'Credentials' ][ 'AccessKeyId' ],
155
                    'secret' => $result[ 'Credentials' ][ 'SecretAccessKey' ],
156
                    'token'  => isset($result[ 'Credentials' ][ 'SessionToken' ]) ? $result[ 'Credentials' ][ 'SessionToken' ] : null
157
            ];
158
        }
159
160
        // 3. env
161 41
        if (false !== getenv('AWS_ACCESS_KEY_ID') and false !== getenv('AWS_SECRET_ACCESS_KEY')) {
162 41
            return [
163 41
                    'key'    => getenv('AWS_ACCESS_KEY_ID'),
164 41
                    'secret' => getenv('AWS_SECRET_ACCESS_KEY'),
165 41
                    'token'  => (false !== getenv('AWS_SESSION_TOKEN')) ? getenv('AWS_SESSION_TOKEN') : null
166 41
            ];
167
        }
168
169
        return [];
170
    }
171
}
172