Passed
Push — master ( 112cee...b68050 )
by Michel
03:20
created

Response::getData()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 7.392

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.2222
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
cc 7
eloc 10
nc 7
nop 0
crap 7.392
1
<?php
2
declare(strict_types = 1);
3
4
namespace TBolier\RethinkQL\Response;
5
6
use TBolier\RethinkQL\Types\Frame\FrameType;
7
use TBolier\RethinkQL\Types\Query\QueryType;
8
9
class Response implements ResponseInterface
10
{
11
    /**
12
     * @var array|null
13
     */
14
    private $backtrace;
15
    /**
16
     * @var array|null
17
     */
18
    private $data;
19
    /**
20
     * @var array|null
21
     */
22
    private $note;
23
    /**
24
     * @var array|null
25
     */
26
    private $profile;
27
    /**
28
     * @var int
29
     */
30
    private $type;
31
32
    /**
33
     * @param int|null $t
34
     * @param array|null $r
35
     * @param array|null $b
36 17
     * @param array|null $p
37
     * @param array|null $n
38 17
     */
39 17
    public function __construct(int $t = null, array $r = null, array $b = null, array $p = null, array $n = null)
40 17
    {
41 17
        !$t ?: $this->type = $t;
42 17
        !$r ?: $this->data = $r;
43 17
        !$b ?: $this->backtrace = $b;
44
        !$p ?: $this->profile = $p;
45
        !$n ?: $this->note = $n;
46
    }
47
48 17
    /**
49
     * @inheritdoc
50 17
     */
51
    public function getType(): ?int
52
    {
53
        return $this->type;
54
    }
55
56 17
    /**
57
     * @inheritdoc
58 17
     */
59
    public function getData()
60
    {
61
        if (!\is_array($this->data)) {
62 17
            return null;
63
        }
64
65
        if (isset($this->data[0]['$reql_type$']) && $this->data[0]['$reql_type$'] === 'GROUPED_DATA') {
66
            $groups = [];
67
            foreach ($this->data[0]['data'] as $group) {
68 1
                $groups[] = [
69
                    'group' => $group[0],
70 1
                    'reduction' => $group[1],
71
                ];
72
            }
73
74
            return $groups;
75
        }
76 1
77
        return \count($this->data) === 1 && array_key_exists(0, $this->data) ? $this->data[0] : $this->data;
78 1
    }
79
80
    /**
81
     * @return bool
82
     */
83
    public function isAtomic(): bool
84 1
    {
85
        return \is_string($this->data) || \count($this->data) === 1;
86 1
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function getBacktrace(): ?array
92 1
    {
93
        return $this->backtrace;
94 1
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    public function getProfile(): ?array
100
    {
101
        return $this->profile;
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107
    public function getNote(): ?array
108
    {
109
        return $this->note;
110
    }
111
}
112