Completed
Push — master ( ab4a72...99f0f9 )
by Sergey
05:20
created

ElasticsearchCollection::response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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 22
    public function response(array $response)
24
    {
25 22
        unset($response['hits']['hits']);
26 22
        $this->response = $response;
27 22
    }
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 8
    public function getTotal()
49
    {
50 8
        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 2
    public function getAggregation($name)
74
    {
75 2
        $buckets = [];
76 2
        $items = Arr::get($this->response, 'aggregations.' . $name . '.buckets', []);
77 2
        foreach ($items as $item) {
78 2
            $buckets[] = new Bucket($item['key'], $item['doc_count']);
79 2
        }
80 2
        return $buckets;
81
    }
82
}
83