Completed
Push — develop ( e8c61d...a2352b )
by Sam
18s queued 10s
created

BulkResponseAggregator::hasErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
     * @var array
13
     */
14
    private $responses = [];
15
16
    /**
17
     * BulkResponseAggregator constructor.
18
     */
19
    public function __construct()
20
    {
21
    }
22
23
24
    /**
25
     * @param array $response
26
     *
27
     * @return BulkResponseAggregator
28
     */
29
    public function addResponse(array $response)
30
    {
31
        $this->responses[] = $response;
32
33
        $this->parseErrors($response);
34
35
        return $this;
36
    }
37
38
39
    /**
40
     * @return bool
41
     */
42
    public function hasErrors()
43
    {
44
        return count($this->errors) > 0;
45
    }
46
47
48
    /**
49
     * @return array
50
     */
51
    public function getErrors()
52
    {
53
        return $this->errors;
54
    }
55
56
    /**
57
     * @param array $response
58
     */
59
    protected function parseErrors(array $response)
60
    {
61
        $items = array_get($response, 'items', []);
62
        foreach ($items as $item) {
63
            $item = array_first($item);
64
65
            if (!array_has($item, 'error')) {
66
                continue;
67
            }
68
69
            $index = array_get($item, '_index');
70
            $type = array_get($item, '_type');
71
            $id = array_get($item, '_id');
72
73
            $errorType = array_get($item, 'error.type');
74
            $errorReason = array_get($item, 'error.reason');
75
76
            $causeType = array_get($item, 'error.caused_by.type');
77
            $causeReason = array_get($item, 'error.caused_by.reason');
78
79
            $this->errors[] = sprintf('Error "%s" reason "%s". Cause "%s" reason "%s". Index "%s", type "%s", id "%s"',
80
                $errorType, $errorReason, $causeType, $causeReason, $index, $type, $id);
81
        }
82
    }
83
84
85
    /**
86
     *
87
     */
88
    public function reset()
89
    {
90
        $this->errors = [];
91
        $this->responses = [];
92
    }
93
}
94