Completed
Push — master ( be5d10...3892c2 )
by Sergey
07:31
created

ElasticsearchCollection::getAggregation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 9
cp 0.7778
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2.0438
1
<?php
2
3
namespace Isswp101\Persimmon\Collection;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
use Isswp101\Persimmon\QueryBuilder\Aggregations\Bucket;
8
9
class ElasticsearchCollection extends Collection
10
{
11
    /**
12
     * Elasticsearch response.
13
     *
14
     * @var array
15
     */
16
    protected $response = [];
17
18
    /**
19
     * Put response.
20
     *
21
     * @param array $response
22
     */
23 11
    public function response(array $response)
24
    {
25 11
        unset($response['hits']['hits']);
26 11
        $this->response = $response;
27 11
    }
28
29
    /**
30
     * @return int
31
     */
32
    public function getTook()
33
    {
34
        return $this->response['took'];
35
    }
36
37
    /**
38
     * @return bool
39
     */
40
    public function isTimedOut()
41
    {
42
        return $this->response['timed_out'];
43
    }
44
45
    /**
46
     * @return int
47
     */
48 4
    public function getTotal()
49
    {
50 4
        return $this->response['hits']['total'];
51
    }
52
53
    /**
54
     * @return int
55
     */
56
    public function getMaxScore()
57
    {
58
        return $this->response['hits']['max_score'];
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function getShards()
65
    {
66
        return $this->response['_shards'];
67
    }
68
69
    /**
70
     * @param string $name
71
     * @return Bucket[]
72
     */
73 1
    public function getAggregation($name)
74
    {
75 1
        $buckets = [];
76 1
        $items = Arr::get($this->response, 'aggregations.' . $name . '.buckets', []);
77 1
        foreach ($items as $item) {
78 1
            $buckets[] = new Bucket($item['key'], $item['doc_count']);
79 1
        }
80 1
        return $buckets;
81
    }
82
}
83