From f245437fc559915c370cf3afc280dd8cae7e1f65 Mon Sep 17 00:00:00 2001 From: Sander van Hooft Date: Mon, 31 Jul 2023 14:37:24 +0200 Subject: [PATCH 1/2] Drop obsolete clearIdempotencyGenerator parameter --- src/MollieApiClient.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/MollieApiClient.php b/src/MollieApiClient.php index 545d4006..bd150263 100644 --- a/src/MollieApiClient.php +++ b/src/MollieApiClient.php @@ -596,10 +596,9 @@ public function setIdempotencyKeyGenerator($generator) } /** - * @param \Mollie\Api\Idempotency\IdempotencyKeyGeneratorContract $generator * @return \Mollie\Api\MollieApiClient */ - public function clearIdempotencyKeyGenerator($generator) + public function clearIdempotencyKeyGenerator() { $this->idempotencyKeyGenerator = null; From 1f240b12a5aa8998f1dcd43adcb7b66721ac5a1e Mon Sep 17 00:00:00 2001 From: Sander van Hooft Date: Mon, 31 Jul 2023 14:55:55 +0200 Subject: [PATCH 2/2] Refactor and explicitly drop key on non-mutating requests --- src/MollieApiClient.php | 44 ++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/MollieApiClient.php b/src/MollieApiClient.php index bd150263..b8b7904c 100644 --- a/src/MollieApiClient.php +++ b/src/MollieApiClient.php @@ -666,17 +666,7 @@ public function performHttpCallToFullUrl($httpMethod, $url, $httpBody = null) $headers['X-Mollie-Client-Info'] = php_uname(); } - if (in_array($httpMethod, [self::HTTP_POST, self::HTTP_PATCH, self::HTTP_DELETE])) { - if (! $this->idempotencyKey && $this->idempotencyKeyGenerator) { - $headers['Idempotency-Key'] = $this->idempotencyKeyGenerator->generate(); - } - - if ($this->idempotencyKey) { - $headers['Idempotency-Key'] = $this->idempotencyKey; - } elseif ($this->idempotencyKeyGenerator) { - $headers['Idempotency-Key'] = $this->idempotencyKeyGenerator->generate(); - } - } + $headers = $this->applyIdempotencyKey($headers, $httpMethod); $response = $this->httpClient->send($httpMethod, $url, $headers, $httpBody); @@ -685,6 +675,38 @@ public function performHttpCallToFullUrl($httpMethod, $url, $httpBody = null) return $response; } + /** + * Conditionally apply the idempotency key to the request headers + * + * @param array $headers + * @param string $httpMethod + * @return array + */ + private function applyIdempotencyKey(array $headers, string $httpMethod) + { + if (! in_array($httpMethod, [self::HTTP_POST, self::HTTP_PATCH, self::HTTP_DELETE])) { + unset($headers['Idempotency-Key']); + + return $headers; + } + + if ($this->idempotencyKey) { + $headers['Idempotency-Key'] = $this->idempotencyKey; + + return $headers; + } + + if ($this->idempotencyKeyGenerator) { + $headers['Idempotency-Key'] = $this->idempotencyKeyGenerator->generate(); + + return $headers; + } + + unset($headers['Idempotency-Key']); + + return $headers; + } + /** * Serialization can be used for caching. Of course doing so can be dangerous but some like to live dangerously. *