1 | <?php |
||
11 | class Cursor implements \Iterator |
||
12 | { |
||
13 | /** |
||
14 | * @var ConnectionCursorInterface |
||
15 | */ |
||
16 | private $connection; |
||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | private $index; |
||
21 | /** |
||
22 | * @var bool |
||
23 | */ |
||
24 | private $isComplete; |
||
25 | /** |
||
26 | * @var MessageInterface |
||
27 | */ |
||
28 | private $message; |
||
29 | /** |
||
30 | * @var ResponseInterface |
||
31 | */ |
||
32 | private $response; |
||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | private $size; |
||
37 | /** |
||
38 | * @var int |
||
39 | */ |
||
40 | private $token; |
||
41 | |||
42 | /** |
||
43 | * @param ConnectionCursorInterface $connection |
||
44 | * @param int $token |
||
45 | * @param ResponseInterface $response |
||
46 | * @param MessageInterface $message |
||
47 | */ |
||
48 | 3 | public function __construct( |
|
59 | |||
60 | /** |
||
61 | * @inheritdoc |
||
62 | * @throws \Exception |
||
63 | */ |
||
64 | 1 | public function current() |
|
72 | |||
73 | /** |
||
74 | * @inheritdoc |
||
75 | * @throws \Exception |
||
76 | */ |
||
77 | public function next(): void |
||
78 | { |
||
79 | $this->index++; |
||
80 | |||
81 | $this->seek(); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @inheritdoc |
||
86 | */ |
||
87 | public function key(): int |
||
88 | { |
||
89 | return $this->index; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @inheritdoc |
||
94 | */ |
||
95 | 1 | public function valid(): bool |
|
99 | |||
100 | /** |
||
101 | * @inheritdoc |
||
102 | * @throws ConnectionException |
||
103 | */ |
||
104 | public function rewind(): void |
||
105 | { |
||
106 | $this->close(); |
||
107 | |||
108 | $this->addResponse($this->connection->rewindFromCursor($this->message)); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @param ResponseInterface $response |
||
113 | */ |
||
114 | 3 | private function addResponse(ResponseInterface $response) |
|
121 | |||
122 | /** |
||
123 | * @return void |
||
124 | * @throws \Exception |
||
125 | */ |
||
126 | 1 | private function seek(): void |
|
127 | { |
||
128 | 1 | while ($this->index === $this->size) { |
|
129 | if ($this->isComplete) { |
||
130 | return; |
||
131 | } |
||
132 | |||
133 | $this->request(); |
||
134 | } |
||
135 | 1 | } |
|
136 | |||
137 | /** |
||
138 | * @return void |
||
139 | * @throws \Exception |
||
140 | */ |
||
141 | private function request(): void |
||
153 | |||
154 | /** |
||
155 | * @return void |
||
156 | */ |
||
157 | private function close(): void |
||
168 | } |
||
169 |