Passed
Pull Request — master (#18)
by Timon
02:57
created

Cursor::close()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 6
1
<?php
2
declare(strict_types=1);
3
4
namespace TBolier\RethinkQL\Response;
5
6
use TBolier\RethinkQL\Connection\ConnectionCursorInterface;
7
use TBolier\RethinkQL\Connection\ConnectionException;
8
use TBolier\RethinkQL\Message\MessageInterface;
9
use TBolier\RethinkQL\Types\Response\ResponseType;
10
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(
49
        ConnectionCursorInterface $connection,
50
        int $token,
51
        ResponseInterface $response,
52
        MessageInterface $message
53
    ) {
54 3
        $this->connection = $connection;
55 3
        $this->token = $token;
56 3
        $this->addResponse($response);
57 3
        $this->message = $message;
58 3
    }
59
60
    /**
61
     * @inheritdoc
62
     * @throws \Exception
63
     */
64 1
    public function current()
65
    {
66 1
        $this->seek();
67
68 1
        if ($this->valid()) {
69 1
            return $this->response->getData()[$this->index];
70
        }
71
    }
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
96
    {
97 1
        return (!$this->isComplete || ($this->index < $this->size));
98
    }
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)
115
    {
116 3
        $this->index = 0;
117 3
        $this->isComplete = $response->getType() === ResponseType::SUCCESS_SEQUENCE;
118 3
        $this->size = \count($response->getData());
119 3
        $this->response = $response;
120 3
    }
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
142
    {
143
        try {
144
            $response = $this->connection->continueQuery($this->token);
145
            $this->addResponse($response);
146
        } catch (\Exception $e) {
147
            $this->isComplete = true;
148
            $this->close();
149
150
            throw $e;
151
        }
152
    }
153
154
    /**
155
     * @return void
156
     */
157
    private function close(): void
158
    {
159
        if (!$this->isComplete) {
160
            $this->connection->stopQuery($this->token);
161
            $this->isComplete = true;
162
        }
163
164
        $this->index = 0;
165
        $this->size = 0;
166
        $this->response = null;
167
    }
168
}
169