| 1 |
<?php |
| 2 |
namespace Aws; |
| 3 |
|
| 4 |
use Psr\Http\Message\RequestInterface; |
| 5 |
use Aws\Exception\AwsException; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class History implements \Countable, \IteratorAggregate |
| 12 |
{ |
| 13 |
private $maxEntries; |
| 14 |
private $entries = array(); |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public function __construct($maxEntries = 10) |
| 20 |
{ |
| 21 |
$this->maxEntries = $maxEntries; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#[\ReturnTypeWillChange] |
| 28 |
public function count() |
| 29 |
{ |
| 30 |
return count($this->entries); |
| 31 |
} |
| 32 |
|
| 33 |
#[\ReturnTypeWillChange] |
| 34 |
public function getIterator() |
| 35 |
{ |
| 36 |
return new \ArrayIterator(array_values($this->entries)); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function getLastCommand() |
| 46 |
{ |
| 47 |
if (!$this->entries) { |
| 48 |
throw new \LogicException('No commands received'); |
| 49 |
} |
| 50 |
|
| 51 |
return end($this->entries)['command']; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function getLastRequest() |
| 61 |
{ |
| 62 |
if (!$this->entries) { |
| 63 |
throw new \LogicException('No requests received'); |
| 64 |
} |
| 65 |
|
| 66 |
return end($this->entries)['request']; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function getLastReturn() |
| 76 |
{ |
| 77 |
if (!$this->entries) { |
| 78 |
throw new \LogicException('No entries'); |
| 79 |
} |
| 80 |
|
| 81 |
$last = end($this->entries); |
| 82 |
|
| 83 |
if (isset($last['result'])) { |
| 84 |
return $last['result']; |
| 85 |
} |
| 86 |
|
| 87 |
if (isset($last['exception'])) { |
| 88 |
return $last['exception']; |
| 89 |
} |
| 90 |
|
| 91 |
throw new \LogicException('No return value for last entry.'); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function start(CommandInterface $cmd, RequestInterface $req) |
| 103 |
{ |
| 104 |
$ticket = uniqid(); |
| 105 |
$this->entries[$ticket] = [ |
| 106 |
'command' => $cmd, |
| 107 |
'request' => $req, |
| 108 |
'result' => null, |
| 109 |
'exception' => null, |
| 110 |
]; |
| 111 |
|
| 112 |
return $ticket; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public function finish($ticket, $result) |
| 122 |
{ |
| 123 |
if (!isset($this->entries[$ticket])) { |
| 124 |
throw new \InvalidArgumentException('Invalid history ticket'); |
| 125 |
} |
| 126 |
|
| 127 |
if (isset($this->entries[$ticket]['result']) |
| 128 |
|| isset($this->entries[$ticket]['exception']) |
| 129 |
) { |
| 130 |
throw new \LogicException('History entry is already finished'); |
| 131 |
} |
| 132 |
|
| 133 |
if ($result instanceof \Exception) { |
| 134 |
$this->entries[$ticket]['exception'] = $result; |
| 135 |
} else { |
| 136 |
$this->entries[$ticket]['result'] = $result; |
| 137 |
} |
| 138 |
|
| 139 |
if (count($this->entries) >= $this->maxEntries) { |
| 140 |
$this->entries = array_slice($this->entries, -$this->maxEntries, null, true); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function clear() |
| 148 |
{ |
| 149 |
$this->entries = []; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public function toArray() |
| 158 |
{ |
| 159 |
return array_values($this->entries); |
| 160 |
} |
| 161 |
} |