PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / Aws / History.php

History.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/Aws/History.php

135 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Dudlewebs\WPMCS\s3\Aws;
4
5 use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface;
6 use Dudlewebs\WPMCS\s3\Aws\Exception\AwsException;
7 /**
8 * Represents a history container that is required when using the history
9 * middleware.
10 */
11 class History implements \Countable, \IteratorAggregate
12 {
13 private $maxEntries;
14 private $entries = array();
15 /**
16 * @param int $maxEntries Maximum number of entries to store.
17 */
18 public function __construct($maxEntries = 10)
19 {
20 $this->maxEntries = $maxEntries;
21 }
22 /**
23 * @return int
24 */
25 #[\ReturnTypeWillChange]
26 public function count()
27 {
28 return \count($this->entries);
29 }
30 #[\ReturnTypeWillChange]
31 public function getIterator()
32 {
33 return new \ArrayIterator(\array_values($this->entries));
34 }
35 /**
36 * Get the last finished command seen by the history container.
37 *
38 * @return CommandInterface
39 * @throws \LogicException if no commands have been seen.
40 */
41 public function getLastCommand()
42 {
43 if (!$this->entries) {
44 throw new \LogicException('No commands received');
45 }
46 return \end($this->entries)['command'];
47 }
48 /**
49 * Get the last finished request seen by the history container.
50 *
51 * @return RequestInterface
52 * @throws \LogicException if no requests have been seen.
53 */
54 public function getLastRequest()
55 {
56 if (!$this->entries) {
57 throw new \LogicException('No requests received');
58 }
59 return \end($this->entries)['request'];
60 }
61 /**
62 * Get the last received result or exception.
63 *
64 * @return ResultInterface|AwsException
65 * @throws \LogicException if no return values have been received.
66 */
67 public function getLastReturn()
68 {
69 if (!$this->entries) {
70 throw new \LogicException('No entries');
71 }
72 $last = \end($this->entries);
73 if (isset($last['result'])) {
74 return $last['result'];
75 }
76 if (isset($last['exception'])) {
77 return $last['exception'];
78 }
79 throw new \LogicException('No return value for last entry.');
80 }
81 /**
82 * Initiate an entry being added to the history.
83 *
84 * @param CommandInterface $cmd Command be executed.
85 * @param RequestInterface $req Request being sent.
86 *
87 * @return string Returns the ticket used to finish the entry.
88 */
89 public function start(CommandInterface $cmd, RequestInterface $req)
90 {
91 $ticket = \uniqid();
92 $this->entries[$ticket] = ['command' => $cmd, 'request' => $req, 'result' => null, 'exception' => null];
93 return $ticket;
94 }
95 /**
96 * Finish adding an entry to the history container.
97 *
98 * @param string $ticket Ticket returned from the start call.
99 * @param mixed $result The result (an exception or AwsResult).
100 */
101 public function finish($ticket, $result)
102 {
103 if (!isset($this->entries[$ticket])) {
104 throw new \InvalidArgumentException('Invalid history ticket');
105 }
106 if (isset($this->entries[$ticket]['result']) || isset($this->entries[$ticket]['exception'])) {
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 }
135