Completed
Pull Request — master (#14)
by Sandro
01:45
created

ArrayStreamHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 15
c 1
b 0
f 0
dl 0
loc 50
ccs 0
cts 19
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A raw() 0 3 1
A appendStream() 0 4 1
A __construct() 0 5 1
A result() 0 3 1
A completeResult() 0 8 2
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-2020 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
    use ArrayAccessStreamHandlerTrait;
20
21
    /**
22
     * @var mixed
23
     */
24
    private $data = [];
25
26
    public function __construct(StreamInterface $stream)
27
    {
28
        $this->data[$this->fetches] = Json::decode($stream->getContents());
29
        $this->length = count($this->data[$this->fetches]['result']);
30
        $this->batchSize = $this->length;
31
    }
32
33
    /**
34
     * Return the current result row
35
     *
36
     * @return array
37
     */
38
    public function current(): array
39
    {
40
        return $this->data[$this->fetches]['result'][$this->position - ($this->batchSize * $this->fetches)];
41
    }
42
43
    public function result(): array
44
    {
45
        return $this->data[$this->fetches]['result'];
46
    }
47
48
    public function raw(): array
49
    {
50
        return $this->data[$this->fetches];
51
    }
52
53
    public function completeResult()
54
    {
55
        $completeResult = [[]];
56
57
        foreach ($this->data as $result) {
58
            $completeResult[] = $result['result'];
59
        }
60
        return array_merge(...$completeResult);
61
    }
62
63
    public function appendStream(StreamInterface $stream): void
64
    {
65
        $this->data[++$this->fetches] = Json::decode($stream->getContents());
66
        $this->length += count($this->data[$this->fetches]['result']);
67
    }
68
}
69