Completed
Push — develop ( c77851...b5a710 )
by Sam
13s
created

BulkResponseAggregator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 21
dl 0
loc 73
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hasErrors() 0 3 1
A addResponse() 0 5 1
A parseErrors() 0 22 3
A getErrors() 0 3 1
A reset() 0 3 1
1
<?php namespace Nord\Lumen\Elasticsearch\Documents\Bulk;
2
3
class BulkResponseAggregator
4
{
5
6
    /**
7
     * @var array
8
     */
9
    private $errors = [];
10
11
    /**
12
     * @param array $response
13
     *
14
     * @return BulkResponseAggregator
15
     */
16
    public function addResponse(array $response)
17
    {
18
        $this->parseErrors($response);
19
20
        return $this;
21
    }
22
23
24
    /**
25
     * @return bool
26
     */
27
    public function hasErrors()
28
    {
29
        return count($this->errors) > 0;
30
    }
31
32
33
    /**
34
     * @return array
35
     */
36
    public function getErrors()
37
    {
38
        return $this->errors;
39
    }
40
41
    /**
42
     * @param array $response
43
     */
44
    protected function parseErrors(array $response)
45
    {
46
        $items = array_get($response, 'items', []);
47
        foreach ($items as $item) {
48
            $item = array_first($item);
49
50
            if (!array_has($item, 'error')) {
51
                continue;
52
            }
53
54
            $index = array_get($item, '_index');
55
            $type = array_get($item, '_type');
56
            $id = array_get($item, '_id');
57
58
            $errorType = array_get($item, 'error.type');
59
            $errorReason = array_get($item, 'error.reason');
60
61
            $causeType = array_get($item, 'error.caused_by.type');
62
            $causeReason = array_get($item, 'error.caused_by.reason');
63
64
            $this->errors[] = sprintf('Error "%s" reason "%s". Cause "%s" reason "%s". Index "%s", type "%s", id "%s"',
65
                $errorType, $errorReason, $causeType, $causeReason, $index, $type, $id);
66
        }
67
    }
68
69
70
    /**
71
     *
72
     */
73
    public function reset()
74
    {
75
        $this->errors = [];
76
    }
77
}
78