Configuration.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Aws\Retry;
  3. use Aws\Retry\Exception\ConfigurationException;
  4. class Configuration implements ConfigurationInterface
  5. {
  6. private $mode;
  7. private $maxAttempts;
  8. private $validModes = [
  9. 'legacy',
  10. 'standard',
  11. 'adaptive'
  12. ];
  13. public function __construct($mode = 'legacy', $maxAttempts = 3)
  14. {
  15. $mode = strtolower($mode);
  16. if (!in_array($mode, $this->validModes)) {
  17. throw new ConfigurationException("'{$mode}' is not a valid mode."
  18. . " The mode has to be 'legacy', 'standard', or 'adaptive'.");
  19. }
  20. if (!is_numeric($maxAttempts)
  21. || intval($maxAttempts) != $maxAttempts
  22. || $maxAttempts < 1
  23. ) {
  24. throw new ConfigurationException("The 'maxAttempts' parameter has"
  25. . " to be an integer >= 1.");
  26. }
  27. $this->mode = $mode;
  28. $this->maxAttempts = intval($maxAttempts);
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getMode()
  34. {
  35. return $this->mode;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getMaxAttempts()
  41. {
  42. return $this->maxAttempts;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function toArray()
  48. {
  49. return [
  50. 'mode' => $this->getMode(),
  51. 'max_attempts' => $this->getMaxAttempts(),
  52. ];
  53. }
  54. }