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

ElasticsearchCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 38.24%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 74
ccs 13
cts 34
cp 0.3824
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getTook() 0 4 1
A isTimedOut() 0 4 1
A getMaxScore() 0 4 1
A getShards() 0 4 1
A response() 0 5 1
A getTotal() 0 4 1
A getAggregation() 0 9 2
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