forked from php-twinfield/twinfield
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from iranl/extend-all-testing
Extend all testing
- Loading branch information
Showing
10 changed files
with
480 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace PhpTwinfield\ApiConnectors; | ||
|
||
final class ApiOptions | ||
{ | ||
private $retriableExceptionMessages = [ | ||
"SSL: Connection reset by peer", | ||
"Your logon credentials are not valid anymore. Try to log on again." | ||
]; | ||
|
||
private $maxRetries = 3; | ||
|
||
/** | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function __construct(?array $messages = null, ?int $maxRetries = null) | ||
{ | ||
if ($messages !== null) { | ||
$this->validateMessages($messages); | ||
$this->retriableExceptionMessages = $messages; | ||
} | ||
if ($maxRetries !== null) { | ||
$this->validateMaxRetries($maxRetries); | ||
$this->maxRetries = $maxRetries; | ||
} | ||
} | ||
|
||
/** | ||
* @throws \InvalidArgumentException | ||
*/ | ||
private function validateMaxRetries(int $maxRetries): void | ||
{ | ||
if ($maxRetries < 0) { | ||
throw new \InvalidArgumentException('The max retries should be a positive integer.'); | ||
} | ||
} | ||
|
||
/** | ||
* @throws \InvalidArgumentException | ||
*/ | ||
private function validateMessages(array $messages): void | ||
{ | ||
foreach ($messages as $key => $message) { | ||
if (trim($message) === '') { | ||
throw new \InvalidArgumentException( | ||
sprintf('The exception message should not be empty. Key: [%s]', $key) | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getRetriableExceptionMessages(): array | ||
{ | ||
return $this->retriableExceptionMessages; | ||
} | ||
|
||
/** | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function setRetriableExceptionMessages(array $retriableExceptionMessages): ApiOptions | ||
{ | ||
return new self( | ||
$retriableExceptionMessages, | ||
$this->maxRetries | ||
); | ||
} | ||
|
||
/** | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function addMessages(array $messages): ApiOptions | ||
{ | ||
return new self( | ||
array_merge($messages, $this->retriableExceptionMessages), | ||
$this->maxRetries | ||
); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getMaxRetries(): int | ||
{ | ||
return $this->maxRetries; | ||
} | ||
|
||
/** | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function setMaxRetries(int $maxRetries): ApiOptions | ||
{ | ||
return new self( | ||
$this->retriableExceptionMessages, | ||
$maxRetries | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.