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
|
|
|
/** |
26
|
|
|
* @param array $response |
27
|
|
|
* |
28
|
|
|
* @return BulkResponseAggregator |
29
|
|
|
*/ |
30
|
|
|
public function addResponse(Array $response) |
31
|
|
|
{ |
32
|
|
|
$this->response[] = $response; |
|
|
|
|
33
|
|
|
|
34
|
|
|
$this->parseErrors($response); |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
public function hasErrors() |
44
|
|
|
{ |
45
|
|
|
return count($this->errors) > 0; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function getErrors() |
53
|
|
|
{ |
54
|
|
|
return $this->errors; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $response |
59
|
|
|
*/ |
60
|
|
|
protected function parseErrors(Array $response) |
61
|
|
|
{ |
62
|
|
|
$errors = []; |
63
|
|
|
|
64
|
|
|
$items = array_get($response, 'items', []); |
65
|
|
|
foreach($items as $item) { |
66
|
|
|
|
67
|
|
|
$item = array_first($item); |
68
|
|
|
|
69
|
|
|
if(!array_has($item, 'error')) { |
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$index = array_get($item, '_index'); |
74
|
|
|
$type = array_get($item, '_type'); |
75
|
|
|
$id = array_get($item, '_id'); |
76
|
|
|
|
77
|
|
|
$errorType = array_get($item, 'error.type'); |
78
|
|
|
$errorReason = array_get($item, 'error.reason'); |
79
|
|
|
|
80
|
|
|
$causeType = array_get($item, 'error.caused_by.type'); |
81
|
|
|
$causeReason = array_get($item, 'error.caused_by.reason'); |
82
|
|
|
|
83
|
|
|
$errors[] = sprintf('Error "%s" reason "%s". Cause "%s" reason "%s". Index "%s", type "%s", id "%s"', |
84
|
|
|
$errorType, $errorReason, $causeType, $causeReason, $index, $type, $id); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if($errors) { |
88
|
|
|
$this->errors = array_merge($this->errors, $errors); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* |
95
|
|
|
*/ |
96
|
|
|
public function reset() |
97
|
|
|
{ |
98
|
|
|
$this->errors = []; |
99
|
|
|
$this->responses = []; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|