Completed
Push — master ( 18b606...1191a0 )
by yuuki
04:00
created

QueryResult::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Ytake\PrestoClient;
5
6
/**
7
 * Class QueryResult
8
 */
9
final class QueryResult
10
{
11
    /** @var string */
12
    private $id;
13
14
    /** @var string */
15
    private $infoUri;
16
17
    /** @var string */
18
    private $partialCancelUri;
19
20
    /** @var string */
21
    private $nextUri;
22
23
    /** @var \stdClass[] */
24
    private $columns = [];
25
26
    /** @var array */
27
    private $data = [];
28
29
    /** @var StatementStats|null */
30
    private $stats;
31
32
    /** @var QueryError|null */
33
    private $error;
34
35
    /**
36
     * QueryResult constructor.
37
     *
38
     * @param string $content
39
     */
40
    public function set(string $content)
41
    {
42
        $parsed = $this->parseContent($content);
43
        $this->id = $parsed->id;
44
        $this->infoUri = $parsed->infoUri;
45
        $this->partialCancelUri = $parsed->partialCancelUri ?? null;
46
        $this->nextUri = $parsed->nextUri ?? null;
47
        $this->columns = [];
48
        if (isset($parsed->columns)) {
49
            $this->columnTransfer($parsed->columns);
50
        }
51
        $this->data = $parsed->data ?? [];
52
        $this->stats = isset($parsed->stats) ? $this->statsTransfer($parsed->stats) : null;
53
        $this->error = isset($parsed->error) ? $this->errorTransfer($parsed->error) : null;
54
    }
55
56
    /**
57
     * @return string|null
58
     */
59
    public function getId()
60
    {
61
        return $this->id;
62
    }
63
64
    /**
65
     * @return string|null
66
     */
67
    public function getInfoUri()
68
    {
69
        return $this->infoUri;
70
    }
71
72
    /**
73
     * @return string|null
74
     */
75
    public function getNextUri()
76
    {
77
        return $this->nextUri;
78
    }
79
80
    /**
81
     * @return QueryError|null
82
     */
83
    public function getError()
84
    {
85
        return $this->error;
86
    }
87
88
    /**
89
     * @return string|null
90
     */
91
    public function getPartialCancelUri()
92
    {
93
        return $this->partialCancelUri;
94
    }
95
96
    /**
97
     * @return \Generator
98
     */
99
    public function getData(): \Generator
100
    {
101
        if (!count($this->data)) {
102
            yield;
103
        }
104
        foreach ($this->data as $data) {
105
            $fixData = new FixData();
106
            $column = $this->getColumns();
107
            for ($i = 0; $i < count($column); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
108
                $fixData->add($column[$i]->getName(), $data[$i]);
109
            }
110
            yield $fixData;
111
        }
112
    }
113
114
    /**
115
     * @param string $content
116
     *
117
     * @return \stdClass
118
     */
119
    private function parseContent(string $content): \stdClass
120
    {
121
        $parsed = json_decode($content);
122
        if ($parsed === null && json_last_error() !== JSON_ERROR_NONE) {
123
            throw new \RuntimeException;
124
        }
125
126
        return $parsed;
127
    }
128
129
    /**
130
     * @param \stdClass $jsonContent
131
     *
132
     * @return StatementStats
133
     */
134
    private function statsTransfer(\stdClass $jsonContent): StatementStats
135
    {
136
        return new StatementStats($jsonContent);
137
    }
138
139
    /**
140
     * @param \stdClass $jsonContent
141
     *
142
     * @return QueryError
143
     */
144
    private function errorTransfer(\stdClass $jsonContent): QueryError
145
    {
146
        return new QueryError($jsonContent);
147
    }
148
149
    /**
150
     * @param array $columns
151
     */
152
    private function columnTransfer(array $columns)
153
    {
154
        foreach ($columns as $column) {
155
            $this->columns[] = new Column($column);
156
        }
157
    }
158
159
    /**
160
     * @return StatementStats|null
161
     */
162
    public function getStats()
163
    {
164
        return $this->stats;
165
    }
166
167
    /**
168
     * @return Column[]
169
     */
170
    public function getColumns(): array
171
    {
172
        return $this->columns;
173
    }
174
}
175