History.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Aws;
  3. use Psr\Http\Message\RequestInterface;
  4. use Aws\Exception\AwsException;
  5. /**
  6. * Represents a history container that is required when using the history
  7. * middleware.
  8. */
  9. class History implements \Countable, \IteratorAggregate
  10. {
  11. private $maxEntries;
  12. private $entries = array();
  13. /**
  14. * @param int $maxEntries Maximum number of entries to store.
  15. */
  16. public function __construct($maxEntries = 10)
  17. {
  18. $this->maxEntries = $maxEntries;
  19. }
  20. public function count()
  21. {
  22. return count($this->entries);
  23. }
  24. public function getIterator()
  25. {
  26. return new \ArrayIterator(array_values($this->entries));
  27. }
  28. /**
  29. * Get the last finished command seen by the history container.
  30. *
  31. * @return CommandInterface
  32. * @throws \LogicException if no commands have been seen.
  33. */
  34. public function getLastCommand()
  35. {
  36. if (!$this->entries) {
  37. throw new \LogicException('No commands received');
  38. }
  39. return end($this->entries)['command'];
  40. }
  41. /**
  42. * Get the last finished request seen by the history container.
  43. *
  44. * @return RequestInterface
  45. * @throws \LogicException if no requests have been seen.
  46. */
  47. public function getLastRequest()
  48. {
  49. if (!$this->entries) {
  50. throw new \LogicException('No requests received');
  51. }
  52. return end($this->entries)['request'];
  53. }
  54. /**
  55. * Get the last received result or exception.
  56. *
  57. * @return ResultInterface|AwsException
  58. * @throws \LogicException if no return values have been received.
  59. */
  60. public function getLastReturn()
  61. {
  62. if (!$this->entries) {
  63. throw new \LogicException('No entries');
  64. }
  65. $last = end($this->entries);
  66. if (isset($last['result'])) {
  67. return $last['result'];
  68. } elseif (isset($last['exception'])) {
  69. return $last['exception'];
  70. } else {
  71. throw new \LogicException('No return value for last entry.');
  72. }
  73. }
  74. /**
  75. * Initiate an entry being added to the history.
  76. *
  77. * @param CommandInterface $cmd Command be executed.
  78. * @param RequestInterface $req Request being sent.
  79. *
  80. * @return string Returns the ticket used to finish the entry.
  81. */
  82. public function start(CommandInterface $cmd, RequestInterface $req)
  83. {
  84. $ticket = uniqid();
  85. $this->entries[$ticket] = [
  86. 'command' => $cmd,
  87. 'request' => $req,
  88. 'result' => null,
  89. 'exception' => null,
  90. ];
  91. return $ticket;
  92. }
  93. /**
  94. * Finish adding an entry to the history container.
  95. *
  96. * @param string $ticket Ticket returned from the start call.
  97. * @param mixed $result The result (an exception or AwsResult).
  98. */
  99. public function finish($ticket, $result)
  100. {
  101. if (!isset($this->entries[$ticket])) {
  102. throw new \InvalidArgumentException('Invalid history ticket');
  103. } elseif (isset($this->entries[$ticket]['result'])
  104. || isset($this->entries[$ticket]['exception'])
  105. ) {
  106. throw new \LogicException('History entry is already finished');
  107. }
  108. if ($result instanceof \Exception) {
  109. $this->entries[$ticket]['exception'] = $result;
  110. } else {
  111. $this->entries[$ticket]['result'] = $result;
  112. }
  113. if (count($this->entries) >= $this->maxEntries) {
  114. $this->entries = array_slice($this->entries, -$this->maxEntries, null, true);
  115. }
  116. }
  117. /**
  118. * Flush the history
  119. */
  120. public function clear()
  121. {
  122. $this->entries = [];
  123. }
  124. /**
  125. * Converts the history to an array.
  126. *
  127. * @return array
  128. */
  129. public function toArray()
  130. {
  131. return array_values($this->entries);
  132. }
  133. }