summaryrefslogtreecommitdiff
path: root/vendor/aws/aws-sdk-php/src/History.php
blob: 9f8a3bf044e75dfbfd7f5d7ecca61b3742756cdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace Aws;

use Psr\Http\Message\RequestInterface;
use Aws\Exception\AwsException;

/**
 * Represents a history container that is required when using the history
 * middleware.
 */
class History implements \Countable, \IteratorAggregate
{
    private $maxEntries;
    private $entries = array();

    /**
     * @param int $maxEntries Maximum number of entries to store.
     */
    public function __construct($maxEntries = 10)
    {
        $this->maxEntries = $maxEntries;
    }

    /**
     * @return int
     */
    #[\ReturnTypeWillChange]
    public function count()
    {
        return count($this->entries);
    }

    #[\ReturnTypeWillChange]
    public function getIterator()
    {
        return new \ArrayIterator(array_values($this->entries));
    }

    /**
     * Get the last finished command seen by the history container.
     *
     * @return CommandInterface
     * @throws \LogicException if no commands have been seen.
     */
    public function getLastCommand()
    {
        if (!$this->entries) {
            throw new \LogicException('No commands received');
        }

        return end($this->entries)['command'];
    }

    /**
     * Get the last finished request seen by the history container.
     *
     * @return RequestInterface
     * @throws \LogicException if no requests have been seen.
     */
    public function getLastRequest()
    {
        if (!$this->entries) {
            throw new \LogicException('No requests received');
        }

        return end($this->entries)['request'];
    }

    /**
     * Get the last received result or exception.
     *
     * @return ResultInterface|AwsException
     * @throws \LogicException if no return values have been received.
     */
    public function getLastReturn()
    {
        if (!$this->entries) {
            throw new \LogicException('No entries');
        }

        $last = end($this->entries);

        if (isset($last['result'])) {
            return $last['result'];
        }

        if (isset($last['exception'])) {
            return $last['exception'];
        }

        throw new \LogicException('No return value for last entry.');
    }

    /**
     * Initiate an entry being added to the history.
     *
     * @param CommandInterface $cmd Command be executed.
     * @param RequestInterface $req Request being sent.
     *
     * @return string Returns the ticket used to finish the entry.
     */
    public function start(CommandInterface $cmd, RequestInterface $req)
    {
        $ticket = uniqid();
        $this->entries[$ticket] = [
            'command'   => $cmd,
            'request'   => $req,
            'result'    => null,
            'exception' => null,
        ];

        return $ticket;
    }

    /**
     * Finish adding an entry to the history container.
     *
     * @param string $ticket Ticket returned from the start call.
     * @param mixed  $result The result (an exception or AwsResult).
     */
    public function finish($ticket, $result)
    {
        if (!isset($this->entries[$ticket])) {
            throw new \InvalidArgumentException('Invalid history ticket');
        }

        if (isset($this->entries[$ticket]['result'])
            || isset($this->entries[$ticket]['exception'])
        ) {
            throw new \LogicException('History entry is already finished');
        }

        if ($result instanceof \Exception) {
            $this->entries[$ticket]['exception'] = $result;
        } else {
            $this->entries[$ticket]['result'] = $result;
        }

        if (count($this->entries) >= $this->maxEntries) {
            $this->entries = array_slice($this->entries, -$this->maxEntries, null, true);
        }
    }

    /**
     * Flush the history
     */
    public function clear()
    {
        $this->entries = [];
    }

    /**
     * Converts the history to an array.
     *
     * @return array
     */
    public function toArray()
    {
        return array_values($this->entries);
    }
}