Passed
Push — v9 ( 345353...555fde )
by Georges
04:49
created

Config::getRegion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 *
4
 * This file is part of Phpfastcache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
9
 *
10
 * @author Georges.L (Geolim4)  <[email protected]>
11
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
12
 */
13
14
declare(strict_types=1);
15
16
namespace Phpfastcache\Drivers\Dynamodb;
17
18
use Phpfastcache\Config\ConfigurationOption;
19
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
20
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
21
22
/**
23
 * @see https://github.com/arangodb/arangodb-php/blob/devel/examples/init.php
24
 * @SuppressWarnings(PHPMD.TooManyFields)
25
 */
26
class Config extends ConfigurationOption
27
{
28
    protected ?string $awsAccessKeyId = null;
29
30
    protected ?string $awsSecretAccessKey = null;
31
32
    protected bool $allowEnvCredentialOverride = false;
33
34
    protected ? string $endpoint = null; // List of endpoints here: https://docs.aws.amazon.com/general/latest/gr/ddb.html
35
36
    protected string $region;
37
38
    protected string $table;
39
40
    protected bool $debugEnabled = false;
41
42
    protected string $version = 'latest';
43
44
    protected string $partitionKey = ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX;
45
46
    public function __construct(array $parameters = [])
47
    {
48
        parent::__construct($parameters);
49
        $this->awsAccessKeyId = $this->getDefaultSuperGlobalAccessor()('SERVER', 'AWS_ACCESS_KEY_ID');
50
        $this->awsSecretAccessKey = $this->getDefaultSuperGlobalAccessor()('SERVER', 'AWS_SECRET_ACCESS_KEY');
51
    }
52
53
    /**
54
     * @return string|null
55
     */
56
    public function getAwsAccessKeyId(): ?string
57
    {
58
        return $this->awsAccessKeyId;
59
    }
60
61
    /**
62
     * @param string|null $awsAccessKeyId
63
     * @return Config
64
     * @throws PhpfastcacheLogicException
65
     */
66
    public function setAwsAccessKeyId(?string $awsAccessKeyId): Config
67
    {
68
        $this->enforceLockedProperty(__FUNCTION__);
69
        if ($awsAccessKeyId !== null) {
70
            if (!$this->isAllowEnvCredentialOverride()) {
71
                throw new PhpfastcacheLogicException('You are not allowed to override AWS environment variables.');
72
            }
73
            \putenv("AWS_ACCESS_KEY_ID=$awsAccessKeyId");
74
            $this->awsAccessKeyId = $awsAccessKeyId;
75
        }
76
        return $this;
77
    }
78
79
    /**
80
     * @return string|null
81
     */
82
    public function getAwsSecretAccessKey(): ?string
83
    {
84
        return $this->awsSecretAccessKey;
85
    }
86
87
    /**
88
     * @param string|null $awsSecretAccessKey
89
     * @return Config
90
     * @throws PhpfastcacheLogicException
91
     */
92
    public function setAwsSecretAccessKey(?string $awsSecretAccessKey): Config
93
    {
94
        $this->enforceLockedProperty(__FUNCTION__);
95
        if ($awsSecretAccessKey !== null) {
96
            if (!$this->isAllowEnvCredentialOverride()) {
97
                throw new PhpfastcacheLogicException('You are not allowed to override AWS environment variables.');
98
            }
99
            \putenv("AWS_SECRET_ACCESS_KEY=$awsSecretAccessKey");
100
            $this->awsSecretAccessKey = $awsSecretAccessKey;
101
        }
102
        return $this;
103
    }
104
105
    /**
106
     * @return bool
107
     */
108
    public function isAllowEnvCredentialOverride(): bool
109
    {
110
        return $this->allowEnvCredentialOverride;
111
    }
112
113
    /**
114
     * @param bool $allowEnvCredentialOverride
115
     * @return Config
116
     * @throws PhpfastcacheLogicException
117
     */
118
    public function setAllowEnvCredentialOverride(bool $allowEnvCredentialOverride): Config
119
    {
120
        $this->enforceLockedProperty(__FUNCTION__);
121
        $this->allowEnvCredentialOverride = $allowEnvCredentialOverride;
122
        return $this;
123
    }
124
125
    /**
126
     * @return ?string
127
     */
128
    public function getEndpoint(): ?string
129
    {
130
        return $this->endpoint;
131
    }
132
133
    /**
134
     * @param ?string $endpoint
135
     * @return Config
136
     * @throws PhpfastcacheLogicException
137
     */
138
    public function setEndpoint(?string $endpoint): Config
139
    {
140
        $this->enforceLockedProperty(__FUNCTION__);
141
        if (!\str_starts_with($endpoint, 'https://') && \str_ends_with($endpoint, 'amazonaws.com')) {
0 ignored issues
show
Bug introduced by
It seems like $endpoint can also be of type null; however, parameter $haystack of str_starts_with() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
        if (!\str_starts_with(/** @scrutinizer ignore-type */ $endpoint, 'https://') && \str_ends_with($endpoint, 'amazonaws.com')) {
Loading history...
Bug introduced by
It seems like $endpoint can also be of type null; however, parameter $haystack of str_ends_with() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
        if (!\str_starts_with($endpoint, 'https://') && \str_ends_with(/** @scrutinizer ignore-type */ $endpoint, 'amazonaws.com')) {
Loading history...
142
            $endpoint = 'https://' . $endpoint;
143
        }
144
        $this->endpoint = $endpoint;
145
        return $this;
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function getRegion(): string
152
    {
153
        return $this->region;
154
    }
155
156
    /**
157
     * @param string $region
158
     * @return Config
159
     * @throws PhpfastcacheLogicException
160
     */
161
    public function setRegion(string $region): Config
162
    {
163
        $this->enforceLockedProperty(__FUNCTION__);
164
        $this->region = $region;
165
        return $this;
166
    }
167
168
    /**
169
     * @return string
170
     */
171
    public function getTable(): string
172
    {
173
        return $this->table;
174
    }
175
176
    /**
177
     * @param string $table
178
     * @return Config
179
     * @throws PhpfastcacheLogicException
180
     */
181
    public function setTable(string $table): Config
182
    {
183
        $this->enforceLockedProperty(__FUNCTION__);
184
        $this->table = $table;
185
        return $this;
186
    }
187
188
    /**
189
     * @return bool
190
     */
191
    public function isDebugEnabled(): bool
192
    {
193
        return $this->debugEnabled;
194
    }
195
196
    /**
197
     * @param bool $debugEnabled
198
     * @return Config
199
     * @throws PhpfastcacheLogicException
200
     */
201
    public function setDebugEnabled(bool $debugEnabled): Config
202
    {
203
        $this->enforceLockedProperty(__FUNCTION__);
204
        $this->debugEnabled = $debugEnabled;
205
        return $this;
206
    }
207
208
    /**
209
     * @return string
210
     */
211
    public function getVersion(): string
212
    {
213
        return $this->version;
214
    }
215
216
    /**
217
     * @param string $version
218
     * @return Config
219
     * @throws PhpfastcacheLogicException
220
     */
221
    public function setVersion(string $version): Config
222
    {
223
        $this->enforceLockedProperty(__FUNCTION__);
224
        $this->version = $version;
225
        return $this;
226
    }
227
228
    /**
229
     * @return string
230
     */
231
    public function getPartitionKey(): string
232
    {
233
        return $this->partitionKey;
234
    }
235
236
    /**
237
     * @param string $partitionKey
238
     * @return Config
239
     * @throws PhpfastcacheLogicException
240
     */
241
    public function setPartitionKey(string $partitionKey): Config
242
    {
243
        $this->enforceLockedProperty(__FUNCTION__);
244
        $this->partitionKey = $partitionKey;
245
        return $this;
246
    }
247
}
248