Passed
Pull Request — master (#3)
by
unknown
15:40
created

History::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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;
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
    public function count()
25
    {
26
        return count($this->entries);
27
    }
28
29
    public function getIterator()
30
    {
31
        return new \ArrayIterator(array_values($this->entries));
32
    }
33
34
    /**
35
     * Get the last finished command seen by the history container.
36
     *
37
     * @return CommandInterface
38
     * @throws \LogicException if no commands have been seen.
39
     */
40
    public function getLastCommand()
41
    {
42
        if (!$this->entries) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->entries of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            throw new \LogicException('No commands received');
44
        }
45
46
        return end($this->entries)['command'];
47
    }
48
49
    /**
50
     * Get the last finished request seen by the history container.
51
     *
52
     * @return RequestInterface
53
     * @throws \LogicException if no requests have been seen.
54
     */
55
    public function getLastRequest()
56
    {
57
        if (!$this->entries) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->entries of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
58
            throw new \LogicException('No requests received');
59
        }
60
61
        return end($this->entries)['request'];
62
    }
63
64
    /**
65
     * Get the last received result or exception.
66
     *
67
     * @return ResultInterface|AwsException
68
     * @throws \LogicException if no return values have been received.
69
     */
70
    public function getLastReturn()
71
    {
72
        if (!$this->entries) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->entries of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
73
            throw new \LogicException('No entries');
74
        }
75
76
        $last = end($this->entries);
77
78
        if (isset($last['result'])) {
79
            return $last['result'];
80
        } elseif (isset($last['exception'])) {
81
            return $last['exception'];
82
        } else {
83
            throw new \LogicException('No return value for last entry.');
84
        }
85
    }
86
87
    /**
88
     * Initiate an entry being added to the history.
89
     *
90
     * @param CommandInterface $cmd Command be executed.
91
     * @param RequestInterface $req Request being sent.
92
     *
93
     * @return string Returns the ticket used to finish the entry.
94
     */
95
    public function start(CommandInterface $cmd, RequestInterface $req)
96
    {
97
        $ticket = uniqid();
98
        $this->entries[$ticket] = [
99
            'command'   => $cmd,
100
            'request'   => $req,
101
            'result'    => null,
102
            'exception' => null,
103
        ];
104
105
        return $ticket;
106
    }
107
108
    /**
109
     * Finish adding an entry to the history container.
110
     *
111
     * @param string $ticket Ticket returned from the start call.
112
     * @param mixed  $result The result (an exception or AwsResult).
113
     */
114
    public function finish($ticket, $result)
115
    {
116
        if (!isset($this->entries[$ticket])) {
117
            throw new \InvalidArgumentException('Invalid history ticket');
118
        } elseif (isset($this->entries[$ticket]['result'])
119
            || isset($this->entries[$ticket]['exception'])
120
        ) {
121
            throw new \LogicException('History entry is already finished');
122
        }
123
124
        if ($result instanceof \Exception) {
125
            $this->entries[$ticket]['exception'] = $result;
126
        } else {
127
            $this->entries[$ticket]['result'] = $result;
128
        }
129
130
        if (count($this->entries) >= $this->maxEntries) {
131
            $this->entries = array_slice($this->entries, -$this->maxEntries, null, true);
132
        }
133
    }
134
135
    /**
136
     * Flush the history
137
     */
138
    public function clear()
139
    {
140
        $this->entries = [];
141
    }
142
143
    /**
144
     * Converts the history to an array.
145
     *
146
     * @return array
147
     */
148
    public function toArray()
149
    {
150
        return array_values($this->entries);
151
    }
152
}
153