|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SalesforceBulkApi\services; |
|
4
|
|
|
|
|
5
|
|
|
use SalesforceBulkApi\api\BatchApiSF; |
|
6
|
|
|
use SalesforceBulkApi\api\JobApiSF; |
|
7
|
|
|
use SalesforceBulkApi\conf\LoginParams; |
|
8
|
|
|
use SalesforceBulkApi\dto\BatchInfoDto; |
|
9
|
|
|
use SalesforceBulkApi\dto\CreateJobDto; |
|
10
|
|
|
use SalesforceBulkApi\dto\ResultAtBatchDto; |
|
11
|
|
|
use SalesforceBulkApi\objects\SFBatchErrors; |
|
12
|
|
|
use SalesforceBulkApi\objects\SFJob; |
|
13
|
|
|
|
|
14
|
|
|
class JobSFApiService |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var ApiSalesforce |
|
18
|
|
|
*/ |
|
19
|
|
|
private $api; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var SFJob |
|
23
|
|
|
*/ |
|
24
|
|
|
private $job; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param LoginParams $params |
|
28
|
|
|
* @param array $guzzleHttpClientConfig |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct( |
|
31
|
|
|
LoginParams $params, array $guzzleHttpClientConfig = ['timeout' => 3] |
|
32
|
|
|
) { |
|
33
|
|
|
$this->api = new ApiSalesforce($params, $guzzleHttpClientConfig); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param CreateJobDto $dto |
|
38
|
|
|
* |
|
39
|
|
|
* @return $this |
|
40
|
|
|
* @throws \Exception |
|
41
|
|
|
*/ |
|
42
|
|
|
public function initJob(CreateJobDto $dto) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->job = new SFJob(); |
|
45
|
|
|
$this->job->setJobInfo(JobApiSF::create($this->api, $dto)); |
|
46
|
|
|
|
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param array $data |
|
52
|
|
|
* |
|
53
|
|
|
* @return $this |
|
54
|
|
|
* @throws \Exception |
|
55
|
|
|
*/ |
|
56
|
|
|
public function addBatchToJob(array $data) |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->addQueryBatchToJob(json_encode($data)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $query |
|
63
|
|
|
* |
|
64
|
|
|
* @return $this |
|
65
|
|
|
* @throws \Exception |
|
66
|
|
|
*/ |
|
67
|
|
|
public function addQueryBatchToJob($query) |
|
68
|
|
|
{ |
|
69
|
|
|
$batch = BatchApiSF::addToJob( |
|
70
|
|
|
$this->api, $this->job->getJobInfo(), $query |
|
71
|
|
|
); |
|
72
|
|
|
$this->job->addBatchInfo($batch); |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return $this |
|
79
|
|
|
* @throws \Exception |
|
80
|
|
|
*/ |
|
81
|
|
|
public function closeJob() |
|
82
|
|
|
{ |
|
83
|
|
|
$job = JobApiSF::close($this->api, $this->job->getJobInfo()); |
|
84
|
|
|
$this->job->setJobInfo($job); |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return $this |
|
91
|
|
|
* @throws \Exception |
|
92
|
|
|
*/ |
|
93
|
|
|
public function waitingForComplete() |
|
94
|
|
|
{ |
|
95
|
|
|
$batches = BatchApiSF::infoForAllInJob( |
|
96
|
|
|
$this->api, $this->job->getJobInfo() |
|
97
|
|
|
); |
|
98
|
|
|
$this->job->setBatchesInfo($batches); |
|
99
|
|
|
foreach ($batches as $batch) { |
|
100
|
|
|
if (in_array( |
|
101
|
|
|
$batch->getState(), |
|
102
|
|
|
[BatchInfoDto::STATE_IN_PROGRESS, BatchInfoDto::STATE_QUEUED] |
|
103
|
|
|
)) { |
|
104
|
|
|
sleep(rand(1, 3)); |
|
105
|
|
|
return $this->waitingForComplete(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return ResultAtBatchDto[][] |
|
114
|
|
|
* @throws \Exception |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getResults() |
|
117
|
|
|
{ |
|
118
|
|
|
$results = []; |
|
119
|
|
|
foreach ($this->job->getBatchesInfo() as $batchInfoDto) { |
|
120
|
|
|
$results[$batchInfoDto->getId()] = BatchApiSF::results( |
|
121
|
|
|
$this->api, $batchInfoDto |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
$this->job->setBatchesResults($results); |
|
125
|
|
|
|
|
126
|
|
|
return $results; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return array |
|
131
|
|
|
* @throws \Exception |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getQueriesResults() |
|
134
|
|
|
{ |
|
135
|
|
|
if (!$this->job->getBatchesResults()) { |
|
136
|
|
|
return []; |
|
137
|
|
|
} |
|
138
|
|
|
$queriesResults = []; |
|
139
|
|
|
foreach ($this->job->getBatchesResults() as $batchId => $results) { |
|
140
|
|
|
$queriesResults[$batchId] = []; |
|
141
|
|
|
/** @var ResultAtBatchDto $result */ |
|
142
|
|
|
foreach ($results as $result) { |
|
143
|
|
|
$resultId = $result->getId() ?: $result->getResult(); |
|
144
|
|
|
$batchesInfo = $this->job->getBatchesInfo(); |
|
145
|
|
|
if (empty($batchesInfo[$batchId])) { |
|
146
|
|
|
return $queriesResults; |
|
147
|
|
|
} |
|
148
|
|
|
$queriesResults[$batchId][$resultId] = BatchApiSF::result( |
|
149
|
|
|
$this->api, $batchesInfo[$batchId], $resultId |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $queriesResults; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return SFBatchErrors[] |
|
159
|
|
|
* @throws \Exception |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getErrors() |
|
162
|
|
|
{ |
|
163
|
|
|
$errors = []; |
|
164
|
|
|
foreach ($this->job->getBatchesInfo() as $batchInfoDto) { |
|
165
|
|
|
$error = new SFBatchErrors(); |
|
166
|
|
|
$error->setBatchInfo($batchInfoDto); |
|
167
|
|
|
if ($batchInfoDto->getState() != BatchInfoDto::STATE_COMPLETED) { |
|
168
|
|
|
if ($batchInfoDto->getState() == BatchInfoDto::STATE_FAILED) { |
|
169
|
|
|
$errors[] = $error; |
|
170
|
|
|
continue; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
$results = $this->job->getBatchesResults(); |
|
174
|
|
|
if (empty($results[$batchInfoDto->getId()])) { |
|
175
|
|
|
$results = BatchApiSF::results($this->api, $batchInfoDto); |
|
176
|
|
|
} else { |
|
177
|
|
|
$results = $results[$batchInfoDto->getId()]; |
|
178
|
|
|
} |
|
179
|
|
|
$i = 0; |
|
180
|
|
|
foreach ($results as $result) { |
|
181
|
|
|
if (!$result->isSuccess() && $result->getErrors()) { |
|
182
|
|
|
$error->addError($i, json_encode($result->getErrors())); |
|
183
|
|
|
} |
|
184
|
|
|
++$i; |
|
185
|
|
|
} |
|
186
|
|
|
if (!empty($error->getErrorNumbers())) { |
|
187
|
|
|
$errors[] = $error; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $errors; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return string |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getJobId() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->job->getJobInfo()->getId(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @return string |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getJobObject() |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->job->getJobInfo()->getObject(); |
|
208
|
|
|
} |
|
209
|
|
|
} |