-
Notifications
You must be signed in to change notification settings - Fork 824
/
BulkLoader_Result.php
202 lines (181 loc) · 4.96 KB
/
BulkLoader_Result.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace SilverStripe\Dev;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\View\ArrayData;
/**
* Encapsulates the result of a {@link BulkLoader} import
* (usually through the {@link BulkLoader->processAll()} method).
*
* @todo Refactor to support lazy-loaded DataObjectSets once they are implemented.
*
* @author Ingo Schommer, Silverstripe Ltd. (<firstname>@silverstripe.com)
*/
class BulkLoader_Result implements \Countable
{
use Injectable;
/**
* Stores a map of ID and ClassNames
* which can be reconstructed to DataObjects.
* As imports can get large we just store enough
* information to reconstruct the objects on demand.
* Optionally includes a status message specific to
* the import of this object. This information is stored
* in a custom object property "_BulkLoaderMessage".
*
* Example:
* <code>
* [['ID'=>1, 'ClassName'=>'Member', 'Message'=>'Updated existing record based on ParentID relation']]
* </code>
*
* @var array
*/
protected $created = [];
/**
* see {@link $created}
*
* @var array
*/
protected $updated = [];
/**
* @var array (see {@link $created})
*/
protected $deleted = [];
/**
* Stores the last change.
* It is in the same format as {@link $created} but with an additional key, "ChangeType", which will be set to
* one of 3 strings: "created", "updated", or "deleted"
*/
protected $lastChange = [];
/**
* Returns the count of all objects which were
* created or updated.
*/
public function Count(): int
{
return count($this->created ?? []) + count($this->updated ?? []);
}
/**
* @return int
*/
public function CreatedCount()
{
return count($this->created ?? []);
}
/**
* @return int
*/
public function UpdatedCount()
{
return count($this->updated ?? []);
}
/**
* @return int
*/
public function DeletedCount()
{
return count($this->deleted ?? []);
}
/**
* Returns all created objects. Each object might
* contain specific importer feedback in the "_BulkLoaderMessage" property.
*
* @return ArrayList
*/
public function Created()
{
return $this->mapToArrayList($this->created);
}
/**
* @return ArrayList
*/
public function Updated()
{
return $this->mapToArrayList($this->updated);
}
/**
* @return ArrayList
*/
public function Deleted()
{
$set = new ArrayList();
foreach ($this->deleted as $arrItem) {
$set->push(ArrayData::create($arrItem));
}
return $set;
}
/**
* Returns the last change.
* It is in the same format as {@link $created} but with an additional key, "ChangeType", which will be set to
* one of 3 strings: "created", "updated", or "deleted"
*/
public function LastChange()
{
return $this->lastChange;
}
/**
* @param $obj DataObject
* @param $message string
*/
public function addCreated($obj, $message = null)
{
$this->created[] = $this->lastChange = [
'ID' => $obj->ID,
'ClassName' => get_class($obj),
'Message' => $message
];
$this->lastChange['ChangeType'] = 'created';
}
/**
* @param $obj DataObject
* @param $message string
*/
public function addUpdated($obj, $message = null)
{
$this->updated[] = $this->lastChange = [
'ID' => $obj->ID,
'ClassName' => get_class($obj),
'Message' => $message
];
$this->lastChange['ChangeType'] = 'updated';
}
/**
* @param $obj DataObject
* @param $message string
*/
public function addDeleted($obj, $message = null)
{
$data = $obj->toMap();
$data['_BulkLoaderMessage'] = $message;
$this->deleted[] = $this->lastChange = $data;
$this->lastChange['ChangeType'] = 'deleted';
}
/**
* @param array $arr containing ID and ClassName maps
* @return ArrayList
*/
protected function mapToArrayList($arr)
{
$set = new ArrayList();
foreach ($arr as $arrItem) {
$obj = DataObject::get_by_id($arrItem['ClassName'], $arrItem['ID']);
$obj->_BulkLoaderMessage = $arrItem['Message'];
if ($obj) {
$set->push($obj);
}
}
return $set;
}
/**
* Merges another BulkLoader_Result into this one.
*
* @param BulkLoader_Result $other
*/
public function merge(BulkLoader_Result $other)
{
$this->created = array_merge($this->created, $other->created);
$this->updated = array_merge($this->updated, $other->updated);
$this->deleted = array_merge($this->deleted, $other->deleted);
}
}