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

VpackStreamHandler::toType()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 30
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 Psr\Http\Message\StreamInterface;
15
use Velocypack\Vpack;
0 ignored issues
show
Bug introduced by
The type Velocypack\Vpack was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
/**
18
 * ArangoDB Velocypack stream handler
19
 *
20
 * @see https://github.com/martin-schilling/php-velocypack
21
 */
22
class VpackStreamHandler implements StreamHandler
23
{
24
    use ArrayAccessStreamHandlerTrait;
25
26
    public const RESULT_TYPE_ARRAY = 0;
27
    public const RESULT_TYPE_JSON = 1;
28
    public const RESULT_TYPE_BINARY = 2;
29
    public const RESULT_TYPE_OBJECT = 3;
30
31
    /**
32
     * @var Vpack[]
33
     */
34
    private $data = [];
35
36
    /**
37
     * @var int
38
     */
39
    private $resultType;
40
41
42
    public function __construct(StreamInterface $stream, int $resultType)
43
    {
44
        $this->data[$this->fetches] = Vpack::fromBinary($stream->getContents());
45
        $this->length = count($this->data[$this->fetches]['result']);
46
        $this->batchSize = $this->length;
47
        $this->resultType = $resultType;
48
    }
49
50
    public function current()
51
    {
52
        return $this->toType(
53
            $this->data[$this->fetches]['result'][$this->position - ($this->batchSize * $this->fetches)]
54
        );
55
    }
56
57
    public function result()
58
    {
59
        return $this->toType($this->data[$this->fetches]['result']);
60
    }
61
62
    public function completeResult()
63
    {
64
        $data = $this->resultType === self::RESULT_TYPE_ARRAY ? [[]] : [];
65
66
        $i = 0;
67
68
        foreach ($this->data as $vpack) {
69
            switch ($this->resultType) {
70
                case self::RESULT_TYPE_ARRAY:
71
                    $data[] = $vpack['result']->toArray();
72
                    break;
73
                case self::RESULT_TYPE_JSON:
74
                default:
75
//                    $data .= $i === 0 ? '' : ',';
76
//                    $data .= $vpack['result']->toJson();
77
                    $value = $vpack['result']->toJson();
78
                    $value = rtrim($value, ']');
79
                    $value = ltrim($value, '[');
80
                    $data[] = $value;
81
                    break;
82
            }
83
            $i++;
84
        }
85
        if ($this->resultType === self::RESULT_TYPE_JSON) {
86
            $data = implode(',', $data);
87
88
            if ($i > 0) {
89
                return '[' . $data . ']';
90
            }
91
            return $data;
92
        }
93
94
        return  array_merge(...$data);
95
96
        // TODO needs append method
97
//        $vpack = new Vpack();
98
//        foreach ($this->data as $result) {
99
//            $vpack->append($result['result']);
100
//        }
101
//        return $this->toType($vpack);
102
    }
103
104
    public function appendStream(StreamInterface $stream): void
105
    {
106
        $this->data[++$this->fetches] = Vpack::fromBinary($stream->getContents());
107
        $this->length += count($this->data[$this->fetches]['result']);
108
    }
109
110
    private function toType(Vpack $vpack)
111
    {
112
        switch ($this->resultType) {
113
            case self::RESULT_TYPE_OBJECT:
114
                return (object)$vpack->toArray();
115
            case self::RESULT_TYPE_ARRAY:
116
                return $vpack->toArray();
117
            case self::RESULT_TYPE_BINARY:
118
                return $vpack->toBinary();
119
            case self::RESULT_TYPE_JSON:
120
            default:
121
                return $vpack->toJson();
122
        }
123
    }
124
}
125