Passed
Pull Request — master (#7)
by Sandro
09:05
created

ArrayStreamHandler   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Test Coverage

Coverage 68.52%

Importance

Changes 0
Metric Value
wmc 23
eloc 35
dl 0
loc 162
ccs 37
cts 54
cp 0.6852
rs 10
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A filtered() 0 3 1
A appendStream() 0 4 1
A __construct() 0 5 1
A rewind() 0 3 1
A resultCount() 0 3 1
A fullCount() 0 3 1
A result() 0 3 1
A next() 0 3 1
A cursorId() 0 3 1
A hasMore() 0 3 1
A isCached() 0 3 1
A warnings() 0 3 1
A scannedFull() 0 3 1
A completeResult() 0 8 2
A scannedIndex() 0 3 1
A writesExecuted() 0 3 1
A writesIgnored() 0 3 1
A key() 0 3 1
A valid() 0 8 2
A count() 0 3 1
A current() 0 3 1
1
<?php
2
/**
3
 * Sandro Keil (https://sandro-keil.de)
4
 *
5
 * @link      http://github.com/sandrokeil/arangodb-php-client for the canonical source repository
6
 * @copyright Copyright (c) 2018-2019 Sandro Keil
7
 * @license   http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License
8
 */
9
10
declare(strict_types=1);
11
12
namespace ArangoDb\Statement;
13
14
use ArangoDb\Util\Json;
15
use Psr\Http\Message\StreamInterface;
16
17
class ArrayStreamHandler implements StreamHandler
18
{
19
    /**
20
     * @var mixed
21
     */
22
    private $data = [];
23
24
    /**
25
     * Current position in result set iteration (zero-based)
26
     *
27
     * @var int
28
     */
29
    private $position = 0;
30
31
    /**
32
     * Number of HTTP calls that were made to build the cursor result
33
     *
34
     * @var int
35
     */
36
    private $fetches = 0;
37
38
    /**
39
     * Total length of result set (in number of documents)
40
     *
41
     * @var int
42
     */
43
    private $length;
44
45
    /**
46
     * @var int
47
     */
48
    private $batchSize;
49
50 7
    public function __construct(StreamInterface $stream)
51
    {
52 7
        $this->data[$this->fetches] = Json::decode($stream->getContents());
53 7
        $this->length = count($this->data[$this->fetches]['result']);
54 7
        $this->batchSize = $this->length;
55 7
    }
56
57 2
    public function result(): array
58
    {
59 2
        return $this->data[$this->fetches]['result'];
60
    }
61
62 4
    public function completeResult()
63
    {
64 4
        $completeResult = [[]];
65
66 4
        foreach ($this->data as $result) {
67 4
            $completeResult[] = $result['result'];
68
        }
69 4
        return array_merge(...$completeResult);
70
    }
71
72 4
    public function cursorId(): ?string
73
    {
74 4
        return $this->data[$this->fetches]['id'] ?? null;
75
    }
76
77 7
    public function hasMore(): bool
78
    {
79 7
        return $this->data[$this->fetches]['hasMore'] ?? false;
80
    }
81
82 4
    public function appendStream(StreamInterface $stream): void
83
    {
84 4
        $this->data[++$this->fetches] = Json::decode($stream->getContents());
85 4
        $this->length += count($this->data[$this->fetches]['result']);
86 4
    }
87
88 1
    public function resultCount(): ?int
89
    {
90 1
        return $this->data[$this->fetches]['count'] ?? null;
91
    }
92
93
    /**
94
     * Get the total number of current loaded results.
95
     *
96
     * @return int Total number of laoded results
97
     */
98 1
    public function count()
99
    {
100 1
        return $this->length;
101
    }
102
103
    public function rewind(): void
104
    {
105
        $this->position = 0;
106
    }
107
108
    /**
109
     * Return the current result row
110
     *
111
     * @return array
112
     */
113 3
    public function current(): array
114
    {
115 3
        return $this->data[$this->fetches]['result'][$this->position - ($this->batchSize * $this->fetches)];
116
    }
117
118 3
    public function key(): int
119
    {
120 3
        return $this->position;
121
    }
122
123 3
    public function next(): void
124
    {
125 3
        $this->position++;
126 3
    }
127
128
    /**
129
     * @return bool
130
     */
131 3
    public function valid(): bool
132
    {
133 3
        if ($this->position <= $this->length - 1) {
134
            // we have more results than the current position is
135 3
            return true;
136
        }
137
138 3
        return ($this->position <= $this->length - 1);
139
    }
140
141
    public function writesExecuted(): ?int
142
    {
143
        return $this->data[$this->fetches]['extra']['stats']['writesExecuted'] ?? null;
144
    }
145
146
    public function writesIgnored(): ?int
147
    {
148
        return $this->data[$this->fetches]['extra']['stats']['writesIgnored'] ?? null;
149
    }
150
151
    public function scannedFull(): ?int
152
    {
153
        return $this->data[$this->fetches]['extra']['stats']['scannedFull'] ?? null;
154
    }
155
156
    public function scannedIndex(): ?int
157
    {
158
        return $this->data[$this->fetches]['extra']['stats']['scannedIndex'] ?? null;
159
    }
160
161
    public function filtered(): ?int
162
    {
163
        return $this->data[$this->fetches]['extra']['stats']['filtered'] ?? null;
164
    }
165
166 1
    public function fullCount(): ?int
167
    {
168 1
        return $this->data[$this->fetches]['extra']['stats']['fullCount'] ?? null;
169
    }
170
171
    public function warnings(): array
172
    {
173
        return $this->data[$this->fetches]['extra']['warnings'] ?? [];
174
    }
175
176
    public function isCached(): bool
177
    {
178
        return $this->data[$this->fetches]['cached'] ?? false;
179
    }
180
}
181