Passed
Pull Request — master (#7)
by Sandro
02:17
created

ArrayStreamHandler::warnings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
     * Total length of result set (in number of documents)
33
     *
34
     * @var int
35
     */
36
    private $length;
37
38 8
    public function __construct(StreamInterface $stream)
39
    {
40 8
        $this->data = Json::decode($stream->getContents());
41 8
        $this->length = count($this->data['result']);
42 8
    }
43
44 6
    public function result(): array
45
    {
46 6
        return $this->data['result'];
47
    }
48
49 4
    public function cursorId(): ?string
50
    {
51 4
        return $this->data['id'] ?? null;
52
    }
53
54 8
    public function hasMore(): bool
55
    {
56 8
        return $this->data['hasMore'] ?? false;
57
    }
58
59 4
    public function appendStream(StreamInterface $stream): void
60
    {
61 4
        $data = Json::decode($stream->getContents());
62 4
        $data['result'] = array_merge($this->data['result'], $data['result']);
63 4
        $this->data = $data;
64 4
        $this->length = count($this->data['result']);
65 4
    }
66
67 1
    public function resultCount(): ?int
68
    {
69 1
        return $this->data['count'] ?? null;
70
    }
71
72
    /**
73
     * Get the total number of current loaded results.
74
     *
75
     * @return int Total number of laoded results
76
     */
77 1
    public function count()
78
    {
79 1
        return $this->length;
80
    }
81
82
    public function rewind(): void
83
    {
84
        $this->position = 0;
85
    }
86
87
    /**
88
     * Return the current result row
89
     *
90
     * @return array
91
     */
92 3
    public function current(): array
93
    {
94 3
        return $this->data['result'][$this->position];
95
    }
96
97 3
    public function key(): int
98
    {
99 3
        return $this->position;
100
    }
101
102 3
    public function next(): void
103
    {
104 3
        $this->position++;
105 3
    }
106
107
    /**
108
     * @return bool
109
     */
110 3
    public function valid(): bool
111
    {
112 3
        if ($this->position <= $this->length - 1) {
113
            // we have more results than the current position is
114 3
            return true;
115
        }
116
117 3
        return ($this->position <= $this->length - 1);
118
    }
119
120
    public function writesExecuted(): ?int
121
    {
122
        return $this->data['extra']['stats']['writesExecuted'] ?? null;
123
    }
124
125
    public function writesIgnored(): ?int
126
    {
127
        return $this->data['extra']['stats']['writesIgnored'] ?? null;
128
    }
129
130
    public function scannedFull(): ?int
131
    {
132
        return $this->data['extra']['stats']['scannedFull'] ?? null;
133
    }
134
135
    public function scannedIndex(): ?int
136
    {
137
        return $this->data['extra']['stats']['scannedIndex'] ?? null;
138
    }
139
140
    public function filtered(): ?int
141
    {
142
        return $this->data['extra']['stats']['filtered'] ?? null;
143
    }
144
145 1
    public function fullCount(): ?int
146
    {
147 1
        return $this->data['extra']['stats']['fullCount'] ?? null;
148
    }
149
150
    public function warnings(): array
151
    {
152
        return $this->data['extra']['warnings'] ?? [];
153
    }
154
155
    public function isCached(): bool
156
    {
157
        return $this->data['cached'] ?? false;
158
    }
159
}
160