Passed
Push — master ( e44bbb...5bf3f3 )
by Timon
02:17
created

Cursor::request()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 3
nop 0
crap 6
1
<?php
2
3
namespace TBolier\RethinkQL\Response;
4
5
use TBolier\RethinkQL\Connection\ConnectionCursorInterface;
6
use TBolier\RethinkQL\Connection\ConnectionException;
7
use TBolier\RethinkQL\Query\MessageInterface;
8
use TBolier\RethinkQL\Types\Response\ResponseType;
9
10
class Cursor implements \Iterator
11
{
12
    /**
13
     * @var ConnectionCursorInterface
14
     */
15
    private $connection;
16
17
    /**
18
     * @var int
19
     */
20
    private $token;
21
22
    /**
23
     * @var ResponseInterface
24
     */
25
    private $response;
26
27
    /**
28
     * @var int
29
     */
30
    private $index;
31
32
    /**
33
     * @var int
34
     */
35
    private $size;
36
37
    /**
38
     * @var bool
39
     */
40
    private $isComplete;
41
42
    /**
43
     * @var MessageInterface
44
     */
45
    private $message;
46
47
    /**
48
     * @param ConnectionCursorInterface $connection
49
     * @param int $token
50
     * @param ResponseInterface $response
51
     * @param MessageInterface $message
52
     */
53 2
    public function __construct(ConnectionCursorInterface $connection, int $token, ResponseInterface $response, MessageInterface $message)
54
    {
55 2
        $this->connection = $connection;
56 2
        $this->token = $token;
57 2
        $this->addResponse($response);
58 2
        $this->message = $message;
59 2
    }
60
61
    /**
62
     * @param ResponseInterface $response
63
     */
64 2
    private function addResponse(ResponseInterface $response)
65
    {
66 2
        $this->index = 0;
67 2
        $this->isComplete = $response->getType() === ResponseType::SUCCESS_SEQUENCE;
68 2
        $this->size = \count($response->getData());
69 2
        $this->response = $response;
70 2
    }
71
72
    /**
73
     * @return void
74
     * @throws \Exception
75
     */
76 2
    private function seek(): void
77
    {
78 2
        while ($this->index === $this->size) {
79 1
            if ($this->isComplete) {
80 1
                return;
81
            }
82
83
            $this->request();
84
        }
85 2
    }
86
87
    /**
88
     * @return void
89
     * @throws \Exception
90
     */
91
    private function request(): void
92
    {
93
        try {
94
            $response = $this->connection->continueQuery($this->token);
95
            $this->addResponse($response);
96
        } catch (\Exception $e) {
97
            $this->isComplete = true;
98
            $this->close();
99
100
            throw $e;
101
        }
102
    }
103
104
    /**
105
     * @return void
106
     */
107 1
    private function close(): void
108
    {
109 1
        if (!$this->isComplete) {
110
            $this->connection->stopQuery($this->token);
111
            $this->isComplete = true;
112
        }
113
114 1
        $this->index = 0;
115 1
        $this->size = 0;
116 1
        $this->response = null;
117 1
    }
118
119
    /**
120
     * @inheritdoc
121
     * @throws \Exception
122
     */
123 1
    public function current()
124
    {
125 1
        $this->seek();
126
127 1
        if ($this->valid()) {
128 1
            return $this->response->getData()[$this->index];
129
        }
130
    }
131
132
    /**
133
     * @inheritdoc
134
     * @throws \Exception
135
     */
136 1
    public function next(): void
137
    {
138 1
        $this->index++;
139
140 1
        $this->seek();
141 1
    }
142
143
    /**
144
     * @inheritdoc
145
     */
146 1
    public function key(): int
147
    {
148 1
        return $this->index;
149
    }
150
151
    /**
152
     * @inheritdoc
153
     */
154 2
    public function valid(): bool
155
    {
156 2
        return (!$this->isComplete || ($this->index < $this->size));
157
    }
158
159
    /**
160
     * @inheritdoc
161
     * @throws ConnectionException
162
     */
163 1
    public function rewind(): void
164
    {
165 1
        $this->close();
166
167 1
        $this->addResponse($this->connection->rewindFromCursor($this->message));
168 1
    }
169
}
170