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

ArrayStreamHandler::isCached()   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 7
    public function __construct(StreamInterface $stream)
39
    {
40 7
        $this->data = Json::decode($stream->getContents());
41 7
        $this->length = count($this->data['result']);
42 7
    }
43
44 5
    public function result(): array
45
    {
46 5
        return $this->data['result'];
47
    }
48
49 3
    public function cursorId(): ?string
50
    {
51 3
        return $this->data['id'] ?? null;
52
    }
53
54 7
    public function hasMore(): bool
55
    {
56 7
        return $this->data['hasMore'] ?? false;
57
    }
58
59 3
    public function appendStream(StreamInterface $stream): void
60
    {
61 3
        $data = Json::decode($stream->getContents());
62 3
        $data['result'] = array_merge($this->data['result'], $data['result']);
63 3
        $this->data = $data;
64 3
        $this->length = count($this->data['result']);
65 3
    }
66
67
    /**
68
     * Get the total number of results in the cursor.
69
     *
70
     * This might issue additional HTTP requests to fetch any outstanding results from the server.
71
     *
72
     * @return int Total number of results
73
     */
74
    public function count()
75
    {
76
        return $this->length;
77
    }
78
79
    /**
80
     * Rewind the cursor, loads first batch, can be repeated (new cursor will be created)
81
     *
82
     * @return void
83
     */
84
    public function rewind()
85
    {
86
        $this->position = 0;
87
    }
88
89
    /**
90
     * Return the current result row depending on entry type
91
     *
92
     * @return string|array|object Data
93
     */
94 3
    public function current()
95
    {
96 3
        return $this->data['result'][$this->position];
97
    }
98
99 3
    public function key(): int
100
    {
101 3
        return $this->position;
102
    }
103
104 3
    public function next(): void
105
    {
106 3
        $this->position++;
107 3
    }
108
109
    /**
110
     * @return bool
111
     */
112 3
    public function valid(): bool
113
    {
114 3
        if ($this->position <= $this->length - 1) {
115
            // we have more results than the current position is
116 3
            return true;
117
        }
118
119 3
        return ($this->position <= $this->length - 1);
120
    }
121
122
    public function writesExecuted(): ?int
123
    {
124
        return $this->data['extra']['stats']['writesExecuted'] ?? null;
125
    }
126
127
    public function writesIgnored(): ?int
128
    {
129
        return $this->data['extra']['stats']['writesIgnored'] ?? null;
130
    }
131
132
    public function scannedFull(): ?int
133
    {
134
        return $this->data['extra']['stats']['scannedFull'] ?? null;
135
    }
136
137
    public function scannedIndex(): ?int
138
    {
139
        return $this->data['extra']['stats']['scannedIndex'] ?? null;
140
    }
141
142
    public function filtered(): ?int
143
    {
144
        return $this->data['extra']['stats']['filtered'] ?? null;
145
    }
146
147
    public function fullCount(): ?int
148
    {
149
        return $this->data['extra']['stats']['fullCount'] ?? null;
150
    }
151
152
    public function warnings(): array
153
    {
154
        return $this->data['extra']['warnings'] ?? [];
155
    }
156
157
    public function isCached(): bool
158
    {
159
        return $this->data['cached'] ?? false;
160
    }
161
}
162