Completed
Pull Request — master (#22)
by Sergey
12:32
created

ElasticsearchCollectionResponse::hits()   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
3
namespace Isswp101\Persimmon\Response;
4
5
use Isswp101\Persimmon\Aggregations\Bucket;
6
use Isswp101\Persimmon\Aggregations\BucketCollection;
7
use Isswp101\Persimmon\Collection\Collection;
8
9
class ElasticsearchCollectionResponse
10
{
11
    private $response;
12
    private $aggregations;
13
14
    public function __construct(array $response)
15
    {
16
        $this->response = $response;
17
    }
18
19
    public function getTook(): int
20
    {
21
        return $this->response['took'] ?? 0;
22
    }
23
24
    public function isTimedOut(): bool
25
    {
26
        return $this->response['timed_out'] ?? false;
27
    }
28
29
    public function getTotalShards(): int
30
    {
31
        return $this->response['_shards']['total'] ?? 0;
32
    }
33
34
    public function getSuccessfulShardsCount(): int
35
    {
36
        return $this->response['_shards']['successful'] ?? 0;
37
    }
38
39
    public function getFailedShardsCount(): int
40
    {
41
        return $this->response['_shards']['failed'] ?? 0;
42
    }
43
44
    public function getTotal(): int
45
    {
46
        return $this->response['hits']['total'] ?? 0;
47
    }
48
49
    public function getMaxScore(): int
50
    {
51
        return $this->response['hits']['max_score'] ?? 0;
52
    }
53
54
    public function hits(): array
55
    {
56
        return $this->response['hits']['hits'] ?? [];
57
    }
58
59
    public function getAggregationBuckets(): BucketCollection
60
    {
61
        if ($this->aggregations != null) {
62
            return $this->aggregations;
63
        }
64
        $this->aggregations = new BucketCollection();
65
        foreach ($this->response['aggregations'] ?? [] as $name => $aggregation) {
66
            $buckets = new Collection();
67
            foreach ($aggregation['buckets'] ?? [] as $bucket) {
68
                $buckets->put($bucket['key'], new Bucket($bucket['key'], $bucket['doc_count']));
69
            }
70
            $this->aggregations->put($name, $buckets);
71
        }
72
        return $this->aggregations;
73
    }
74
}