Passed
Pull Request — master (#38)
by Teye
05:25
created

SelectQueryResponse::data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Responses;
5
6
class SelectQueryResponse extends QueryResponse
7
{
8
    /**
9
     * Return the last known paging identifier known by a select query. (If any is executed).
10
     * If no paging identifier is known, an empty array is returned.
11
     *
12
     * The paging identifier will be something like this:
13
     * ```
14
     * Array(
15
     *   'wikipedia_2015-09-12T00:00:00.000Z_2015-09-13T00:00:00.000Z_2019-09-12T14:15:44.694Z' => 19,
16
     * )
17
     * ```
18
     *
19
     * @return array<string,int>
20
     */
21 2
    public function pagingIdentifier(): array
22
    {
23
        /** @var array<string,array<string,int>> $row */
24 2
        $row = $this->getResultRow();
25
26 2
        return $row['pagingIdentifiers'] ?? [];
27
    }
28
29
    /**
30
     * Return the data in a "normalized" way, so we can easily iterate over it
31
     *
32
     * @return array<mixed>
33
     */
34 2
    public function data(): array
35
    {
36
        /** @var array<string,array<string,array<mixed>>> $resultRow */
37 2
        $resultRow = $this->getResultRow();
38
39 2
        return array_map(function ($row) {
40
            /** @var array<string, array<mixed>> $row */
41 1
            return $row['event'];
42 2
        }, $resultRow['events'] ?? []);
43
    }
44
45
    /**
46
     * @return array<string,array<mixed>>
47
     */
48 2
    protected function getResultRow(): array
49
    {
50
        /** @var null|array<string,array<string,array<mixed>>> $row */
51 2
        $row = $this->response[0] ?? null;
52 2
        if ($row === null) {
53 1
            return [];
54
        }
55
56 1
        return $row['result'] ?? [];
57
    }
58
}