BulkResponse::getRows()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
23
declare(strict_types=1);
24
25
namespace Crate\Stdlib;
26
27
final class BulkResponse implements BulkResponseInterface
28
{
29
    /**
30
     * Result object for CrateDB bulk operations.
31
     * https://crate.io/docs/crate/reference/en/latest/interfaces/http.html#bulk-operations
32
     * @var array
33
     */
34
    private $results;
35
36
    /**
37
     * @var string[]
38
     */
39
    private $columnsAsKeys;
40
41
    /**
42
     * @var string[]
43
     */
44
    private $columnsAsValues;
45
46
    /**
47
     * @var int
48
     */
49
    private $duration;
50
51
    /**
52
     * @param array    $results
53
     * @param string[] $columns
54
     * @param float    $duration
55
     */
56 4
    public function __construct(array $results, array $columns, float $duration)
57
    {
58 4
        $this->results         = $results;
59 4
        $this->columnsAsKeys   = array_flip($columns);
60 4
        $this->columnsAsValues = $columns;
61 4
        $this->duration        = $duration;
62
    }
63
64
    /**
65
     * {@Inheritdoc}
66
     */
67 2
    public function map(callable $callback): array
68
    {
69 2
        return array_map($callback, $this->results);
70
    }
71
72
    /**
73
     * {@Inheritdoc}
74
     */
75 2
    public function getColumnIndex($column)
76
    {
77 2
        if (isset($this->columnsAsKeys[$column])) {
78 2
            return $this->columnsAsKeys[$column];
79
        }
80
81 2
        return null;
82
    }
83
84
    /**
85
     * {@Inheritdoc}
86
     */
87 4
    public function getColumns($columnsAsKeys = true): array
88
    {
89 4
        return $columnsAsKeys ? $this->columnsAsKeys : $this->columnsAsValues;
90
    }
91
92
    /**
93
     * {@Inheritdoc}
94
     */
95
    public function getResults(): array
96
    {
97
        return $this->results;
98
    }
99
100
    /**
101
     * {@Inheritdoc}
102
     */
103 2
    public function getRows(): array
104
    {
105 2
        return $this->getResults();
106
    }
107
108
    /**
109
     * {@Inheritdoc}
110
     */
111 2
    public function current(): array
112
    {
113 2
        return current($this->results);
114
    }
115
116
    /**
117
     * {@Inheritdoc}
118
     */
119 2
    public function next(): void
120
    {
121 2
        next($this->results);
122
    }
123
124
    /**
125
     * {@Inheritdoc}
126
     */
127 1
    #[\ReturnTypeWillChange]
128 1
    public function key()
129
    {
130 2
        return key($this->results);
131
    }
132
133
    /**
134
     * {@Inheritdoc}
135
     */
136 2
    public function valid(): bool
137
    {
138 2
        return $this->key() !== null;
139
    }
140
141
    /**
142
     * {@Inheritdoc}
143
     */
144 2
    public function rewind(): void
145
    {
146 2
        reset($this->results);
147
    }
148
149
    /**
150
     * {@Inheritdoc}
151
     */
152 2
    public function count(): int
153
    {
154 2
        $count = 0;
155 2
        foreach ($this->results as $element) {
156 2
            $count += $element["rowcount"];
157
        }
158 2
        return $count;
159
    }
160
}
161