History.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. }
  69. if (isset($last['exception'])) {
  70. return $last['exception'];
  71. }
  72. throw new \LogicException('No return value for last entry.');
  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. }
  104. if (isset($this->entries[$ticket]['result'])
  105. || isset($this->entries[$ticket]['exception'])
  106. ) {
  107. throw new \LogicException('History entry is already finished');
  108. }
  109. if ($result instanceof \Exception) {
  110. $this->entries[$ticket]['exception'] = $result;
  111. } else {
  112. $this->entries[$ticket]['result'] = $result;
  113. }
  114. if (count($this->entries) >= $this->maxEntries) {
  115. $this->entries = array_slice($this->entries, -$this->maxEntries, null, true);
  116. }
  117. }
  118. /**
  119. * Flush the history
  120. */
  121. public function clear()
  122. {
  123. $this->entries = [];
  124. }
  125. /**
  126. * Converts the history to an array.
  127. *
  128. * @return array
  129. */
  130. public function toArray()
  131. {
  132. return array_values($this->entries);
  133. }
  134. }