Cancelled
Pull Request — master (#22)
by Teye
06:15 queued 01:50
created

GroupByQuery::parseResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Queries;
5
6
use Level23\Druid\Limits\Limit;
7
use Level23\Druid\Types\Granularity;
8
use Level23\Druid\Limits\LimitInterface;
9
use Level23\Druid\Filters\FilterInterface;
10
use Level23\Druid\Context\ContextInterface;
11
use Level23\Druid\Collections\IntervalCollection;
12
use Level23\Druid\Responses\GroupByQueryResponse;
13
use Level23\Druid\Collections\DimensionCollection;
14
use Level23\Druid\Collections\AggregationCollection;
15
use Level23\Druid\Collections\VirtualColumnCollection;
16
use Level23\Druid\HavingFilters\HavingFilterInterface;
17
use Level23\Druid\Collections\PostAggregationCollection;
18
19
class GroupByQuery implements QueryInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $dataSource;
25
26
    /**
27
     * @var \Level23\Druid\Collections\DimensionCollection
28
     */
29
    protected $dimensions;
30
31
    /**
32
     * @var string
33
     */
34
    protected $granularity;
35
36
    /**
37
     * @var \Level23\Druid\Filters\FilterInterface|null
38
     */
39
    protected $filter;
40
41
    /**
42
     * @var \Level23\Druid\Collections\AggregationCollection|null
43
     */
44
    protected $aggregations;
45
46
    /**
47
     * @var \Level23\Druid\Collections\PostAggregationCollection|null
48
     */
49
    protected $postAggregations;
50
51
    /**
52
     * @var \Level23\Druid\Collections\VirtualColumnCollection|null
53
     */
54
    protected $virtualColumns;
55
56
    /**
57
     * @var \Level23\Druid\HavingFilters\HavingFilterInterface|null
58
     */
59
    protected $having;
60
61
    /**
62
     * @var \Level23\Druid\Context\ContextInterface|null
63
     */
64
    protected $context;
65
66
    /**
67
     * @var \Level23\Druid\Limits\LimitInterface|null
68
     */
69
    protected $limit;
70
71
    /**
72
     * @var \Level23\Druid\Collections\IntervalCollection
73
     */
74
    protected $intervals;
75
76
    /**
77
     * @var array
78
     */
79
    protected $subtotals = [];
80
81
    /**
82
     * GroupByQuery constructor.
83
     *
84
     * @param string                                         $dataSource
85
     * @param \Level23\Druid\Collections\DimensionCollection $dimensions
86
     * @param \Level23\Druid\Collections\IntervalCollection  $intervals
87
     * @param null|array|AggregationCollection               $aggregations
88
     * @param string                                         $granularity
89 5
     */
90
    public function __construct(
91
        string $dataSource,
92
        DimensionCollection $dimensions,
93
        IntervalCollection $intervals,
94
        $aggregations = null,
95
        $granularity = 'all'
96 5
    ) {
97 5
        $this->dataSource  = $dataSource;
98 5
        $this->dimensions  = $dimensions;
99 4
        $this->granularity = Granularity::validate($granularity);
100
        $this->intervals   = $intervals;
101 4
102 1
        if ($aggregations) {
103
            $this->setAggregations($aggregations);
104 4
        }
105
    }
106
107
    /**
108
     * Return the query in array format so we can fire it to druid.
109
     *
110
     * @return array
111 1
     */
112
    public function toArray(): array
113
    {
114 1
        $query = [
115 1
            'queryType'   => 'groupBy',
116 1
            'dataSource'  => $this->dataSource,
117 1
            'intervals'   => $this->intervals->toArray(),
118 1
            'dimensions'  => $this->dimensions->toArray(),
119
            'granularity' => $this->granularity,
120
        ];
121 1
122 1
        if ($this->filter) {
123
            $query['filter'] = $this->filter->toArray();
124
        }
125 1
126 1
        if ($this->aggregations) {
127
            $query['aggregations'] = $this->aggregations->toArray();
128
        }
129 1
130 1
        if ($this->postAggregations) {
131
            $query['postAggregations'] = $this->postAggregations->toArray();
132
        }
133 1
134 1
        if ($this->virtualColumns) {
135
            $query['virtualColumns'] = $this->virtualColumns->toArray();
136
        }
137 1
138 1
        if ($this->having) {
139
            $query['having'] = $this->having->toArray();
140
        }
141 1
142 1
        if ($this->context) {
143
            $query['context'] = $this->context->toArray();
144
        }
145 1
146 1
        if ($this->limit) {
147
            $query['limitSpec'] = $this->limit->toArray();
148
        }
149 1
150 1
        if (count($this->subtotals) > 0) {
151
            $query['subtotalsSpec'] = $this->subtotals;
152
        }
153 1
154
        return $query;
155
    }
156
157
    /**
158
     * @param \Level23\Druid\Filters\FilterInterface $filter
159 1
     */
160
    public function setFilter(FilterInterface $filter)
161 1
    {
162 1
        $this->filter = $filter;
163
    }
164
165
    /**
166
     * @param \Level23\Druid\Collections\AggregationCollection|array $aggregations
167 1
     */
168
    public function setAggregations($aggregations)
169 1
    {
170 1
        if (is_array($aggregations)) {
171
            $aggregations = new AggregationCollection(...$aggregations);
172
        }
173 1
174 1
        $this->aggregations = $aggregations;
175
    }
176
177
    /**
178
     * @param \Level23\Druid\Collections\PostAggregationCollection|array $postAggregations
179 1
     */
180
    public function setPostAggregations($postAggregations)
181 1
    {
182 1
        if (is_array($postAggregations)) {
183
            $postAggregations = new PostAggregationCollection(...$postAggregations);
184
        }
185 1
186 1
        $this->postAggregations = $postAggregations;
187
    }
188
189
    /**
190
     * @param \Level23\Druid\HavingFilters\HavingFilterInterface $having
191 1
     */
192
    public function setHaving(HavingFilterInterface $having)
193 1
    {
194 1
        $this->having = $having;
195
    }
196
197
    /**
198
     * @param \Level23\Druid\Context\ContextInterface $context
199 1
     */
200
    public function setContext(ContextInterface $context)
201 1
    {
202 1
        $this->context = $context;
203
    }
204
205
    /**
206
     * @param \Level23\Druid\Limits\LimitInterface|int $limit
207 1
     */
208
    public function setLimit($limit)
209 1
    {
210 1
        if ($limit instanceof LimitInterface) {
211
            $this->limit = $limit;
212
        } else {
213 1
214 1
            if (!$this->limit) {
215
                $this->limit = new Limit();
216
            }
217
218
            $this->limit->setLimit($limit);
219
        }
220
    }
221
222
    /**
223 1
     * The "offset" parameter tells Druid to skip this many rows when returning results.
224
     *
225 1
     * @param int $offset
226
     */
227
    public function setOffset(int $offset)
228
    {
229
        if (!$this->limit) {
230
            $this->limit = new Limit();
231 1
        }
232
        $this->limit->setOffset($offset);
233 1
    }
234 1
235
    /**
236
     * Parse the response into something we can return to the user.
237
     *
238
     * @param array $response
239 1
     *
240
     * @return GroupByQueryResponse
241 1
     */
242 1
    public function parseResponse(array $response): GroupByQueryResponse
243
    {
244
        return new GroupByQueryResponse($response);
245
    }
246
247
    /**
248
     * @param \Level23\Druid\Collections\VirtualColumnCollection $virtualColumns
249
     */
250
    public function setVirtualColumns(VirtualColumnCollection $virtualColumns): void
251
    {
252
        $this->virtualColumns = $virtualColumns;
253
    }
254
255
    /**
256
     * @param array $subtotals
257
     */
258
    public function setSubtotals(array $subtotals): void
259
    {
260
        $this->subtotals = $subtotals;
261
    }
262
}