Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws;
3
 
4
use Psr\Http\Message\RequestInterface;
5
use Aws\Exception\AwsException;
6
 
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
    /**
17
     * @param int $maxEntries Maximum number of entries to store.
18
     */
19
    public function __construct($maxEntries = 10)
20
    {
21
        $this->maxEntries = $maxEntries;
22
    }
23
 
24
    /**
25
     * @return int
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
     * Get the last finished command seen by the history container.
41
     *
42
     * @return CommandInterface
43
     * @throws \LogicException if no commands have been seen.
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
     * Get the last finished request seen by the history container.
56
     *
57
     * @return RequestInterface
58
     * @throws \LogicException if no requests have been seen.
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
     * Get the last received result or exception.
71
     *
72
     * @return ResultInterface|AwsException
73
     * @throws \LogicException if no return values have been received.
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
     * Initiate an entry being added to the history.
96
     *
97
     * @param CommandInterface $cmd Command be executed.
98
     * @param RequestInterface $req Request being sent.
99
     *
100
     * @return string Returns the ticket used to finish the entry.
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
     * Finish adding an entry to the history container.
117
     *
118
     * @param string $ticket Ticket returned from the start call.
119
     * @param mixed  $result The result (an exception or AwsResult).
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
     * Flush the history
146
     */
147
    public function clear()
148
    {
149
        $this->entries = [];
150
    }
151
 
152
    /**
153
     * Converts the history to an array.
154
     *
155
     * @return array
156
     */
157
    public function toArray()
158
    {
159
        return array_values($this->entries);
160
    }
161
}