Skip to content

Commit 0ad732d

Browse files
authored
Use DateTimeImmutable instead of DateTimeInterface (#365)
* Use DateTimeImmutable instead of DateTimeInterface * Short syntax
1 parent 888ecac commit 0ad732d

File tree

5 files changed

+22
-28
lines changed

5 files changed

+22
-28
lines changed

src/AbstractApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ final public function getConfiguration(): Configuration
8080
return $this->configuration;
8181
}
8282

83-
final public function presign(Input $input, ?\DateTimeInterface $expires = null): string
83+
final public function presign(Input $input, ?\DateTimeImmutable $expires = null): string
8484
{
8585
$request = $input->request();
8686
$request->setEndpoint($this->getEndpoint($request->getUri(), $request->getQuery()));

src/Credentials/Credentials.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(
2525
string $accessKeyId,
2626
string $secretKey,
2727
?string $sessionToken = null,
28-
?\DateTimeInterface $expireDate = null
28+
?\DateTimeImmutable $expireDate = null
2929
) {
3030
$this->accessKeyId = $accessKeyId;
3131
$this->secretKey = $secretKey;
@@ -48,14 +48,14 @@ public function getSessionToken(): ?string
4848
return $this->sessionToken;
4949
}
5050

51-
public function getExpireDate(): ?\DateTimeInterface
51+
public function getExpireDate(): ?\DateTimeImmutable
5252
{
5353
return $this->expireDate;
5454
}
5555

5656
public function isExpired(): bool
5757
{
58-
return null !== $this->expireDate && new \DateTime() >= $this->expireDate;
58+
return null !== $this->expireDate && new \DateTimeImmutable() >= $this->expireDate;
5959
}
6060

6161
public function getCredentials(Configuration $configuration): ?Credentials

src/RequestContext.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ class RequestContext
2525
private $operation;
2626

2727
/**
28-
* @var \DateTimeInterface|null
28+
* @var \DateTimeImmutable|null
2929
*/
3030
private $expirationDate;
3131

3232
/**
33-
* @var \DateTimeInterface|null
33+
* @var \DateTimeImmutable|null
3434
*/
3535
private $currentDate;
3636

3737
/**
3838
* @param array{
3939
* operation?: null|string
40-
* expirationDate?: null|\\DateTimeInterface
41-
* currentDate?: null|\\DateTimeInterface
40+
* expirationDate?: null|\DateTimeImmutable
41+
* currentDate?: null|\DateTimeImmutable
4242
* }
4343
*/
4444
public function __construct(array $options = [])
@@ -57,12 +57,12 @@ public function getOperation(): ?string
5757
return $this->operation;
5858
}
5959

60-
public function getExpirationDate(): ?\DateTimeInterface
60+
public function getExpirationDate(): ?\DateTimeImmutable
6161
{
6262
return $this->expirationDate;
6363
}
6464

65-
public function getCurrentDate(): ?\DateTimeInterface
65+
public function getCurrentDate(): ?\DateTimeImmutable
6666
{
6767
return $this->currentDate;
6868
}

src/Signer/SignerV4.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,8 @@ public function __construct(string $scopeName, string $region)
6161

6262
public function presign(Request $request, Credentials $credentials, RequestContext $context): void
6363
{
64-
if (null === $now = $context->getCurrentDate()) {
65-
$now = new \DateTimeImmutable();
66-
} else {
67-
$now = new \DateTimeImmutable($now->format(\DateTimeInterface::ATOM));
68-
}
64+
$now = $context->getCurrentDate() ?? new \DateTimeImmutable();
65+
6966
// Signer date have to be UTC https://docs.aws.amazon.com/general/latest/gr/sigv4-date-handling.html
7067
$now = $now->setTimezone(new \DateTimeZone('UTC'));
7168
$expires = $context->getExpirationDate() ?? $now->add(new \DateInterval('PT1H'));
@@ -75,11 +72,8 @@ public function presign(Request $request, Credentials $credentials, RequestConte
7572

7673
public function sign(Request $request, Credentials $credentials, RequestContext $context): void
7774
{
78-
if (null === $now = $context->getCurrentDate()) {
79-
$now = new \DateTimeImmutable();
80-
} else {
81-
$now = new \DateTimeImmutable($now->format(\DateTimeInterface::ATOM));
82-
}
75+
$now = $context->getCurrentDate() ?? new \DateTimeImmutable();
76+
8377
// Signer date have to be UTC https://docs.aws.amazon.com/general/latest/gr/sigv4-date-handling.html
8478
$now = $now->setTimezone(new \DateTimeZone('UTC'));
8579

@@ -106,7 +100,7 @@ protected function buildBodyDigest(Request $request, bool $isPresign): string
106100
return $hash;
107101
}
108102

109-
private function handleSignature(Request $request, Credentials $credentials, \DateTimeInterface $now, \DateTimeInterface $expires, bool $isPresign): void
103+
private function handleSignature(Request $request, Credentials $credentials, \DateTimeImmutable $now, \DateTimeImmutable $expires, bool $isPresign): void
110104
{
111105
$this->removePresign($request);
112106
$this->sanitizeHostForHeader($request);
@@ -197,7 +191,7 @@ private function assignAmzQueryValues(Request $request, Credentials $credentials
197191
}
198192
}
199193

200-
private function buildTime(Request $request, \DateTimeInterface $now, \DateTimeInterface $expires, bool $isPresign): void
194+
private function buildTime(Request $request, \DateTimeImmutable $now, \DateTimeImmutable $expires, bool $isPresign): void
201195
{
202196
if ($isPresign) {
203197
$duration = $expires->getTimestamp() - $now->getTimestamp();
@@ -215,7 +209,7 @@ private function buildTime(Request $request, \DateTimeInterface $now, \DateTimeI
215209
}
216210
}
217211

218-
private function buildCredentialString(Request $request, Credentials $credentials, \DateTimeInterface $now, bool $isPresign): array
212+
private function buildCredentialString(Request $request, Credentials $credentials, \DateTimeImmutable $now, bool $isPresign): array
219213
{
220214
$credentialScope = [$now->format('Ymd'), $this->region, $this->scopeName, 'aws4_request'];
221215

@@ -259,7 +253,7 @@ private function convertBodyToQuery(Request $request): void
259253
$request->setBody(StringStream::create(''));
260254
}
261255

262-
private function convertBodyToStream(Request $request, \DateTimeInterface $now, string $credentialString, string $signingKey, string &$signature): void
256+
private function convertBodyToStream(Request $request, \DateTimeImmutable $now, string $credentialString, string $signingKey, string &$signature): void
263257
{
264258
$body = $request->getBody();
265259
if ($request->hasHeader('content-length')) {
@@ -388,7 +382,7 @@ private function buildCanonicalPath(Request $request): string
388382
return '/' . str_replace('%2F', '/', $doubleEncoded);
389383
}
390384

391-
private function buildStringToSign(\DateTimeInterface $now, string $credentialString, string $canonicalRequest): string
385+
private function buildStringToSign(\DateTimeImmutable $now, string $credentialString, string $canonicalRequest): string
392386
{
393387
return implode("\n", [
394388
self::ALGORITHM_REQUEST,
@@ -398,7 +392,7 @@ private function buildStringToSign(\DateTimeInterface $now, string $credentialSt
398392
]);
399393
}
400394

401-
private function buildChunkStringToSign(\DateTimeInterface $now, string $credentialString, string $signature, string $chunk): string
395+
private function buildChunkStringToSign(\DateTimeImmutable $now, string $credentialString, string $signature, string $chunk): string
402396
{
403397
static $emptyHash;
404398
$emptyHash = $emptyHash ?? hash('sha256', '');

src/Sts/ValueObject/Credentials.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Credentials
2929
* AccessKeyId: string,
3030
* SecretAccessKey: string,
3131
* SessionToken: string,
32-
* Expiration: \DateTimeInterface,
32+
* Expiration: \DateTimeImmutable,
3333
* } $input
3434
*/
3535
public function __construct(array $input)
@@ -50,7 +50,7 @@ public function getAccessKeyId(): string
5050
return $this->AccessKeyId;
5151
}
5252

53-
public function getExpiration(): \DateTimeInterface
53+
public function getExpiration(): \DateTimeImmutable
5454
{
5555
return $this->Expiration;
5656
}

0 commit comments

Comments
 (0)