Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProcessingErrorAggregator.php
Go to the documentation of this file.
1 <?php
8 
13 {
18 
22  protected $allowedErrorsCount = 0;
23 
27  protected $items = [];
28 
32  protected $invalidRows = [];
33 
37  protected $skippedRows = [];
38 
42  protected $errorStatistics = [];
43 
47  protected $messageTemplate = [];
48 
52  protected $errorFactory;
53 
57  public function __construct(
58  \Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorFactory $errorFactory
59  ) {
60  $this->errorFactory = $errorFactory;
61  }
62 
72  public function addError(
73  $errorCode,
75  $rowNumber = null,
76  $columnName = null,
77  $errorMessage = null,
78  $errorDescription = null
79  ) {
80  if ($this->isErrorAlreadyAdded($rowNumber, $errorCode, $columnName)) {
81  return $this;
82  }
83  $this->processErrorStatistics($errorLevel);
84  if ($errorLevel == ProcessingError::ERROR_LEVEL_CRITICAL) {
85  $this->processInvalidRow($rowNumber);
86  }
87  $errorMessage = $this->getErrorMessage($errorCode, $errorMessage, $columnName);
88 
90  $newError = $this->errorFactory->create();
91  $newError->init($errorCode, $errorLevel, $rowNumber, $columnName, $errorMessage, $errorDescription);
92  $this->items['rows'][$rowNumber][] = $newError;
93  $this->items['codes'][$errorCode][] = $newError;
94  $this->items['messages'][$errorMessage][] = $newError;
95  return $this;
96  }
97 
102  public function addRowToSkip($rowNumber)
103  {
104  $rowNumber = (int)$rowNumber;
105  if (!in_array($rowNumber, $this->skippedRows)) {
106  $this->skippedRows[] = $rowNumber;
107  }
108 
109  return $this;
110  }
111 
116  protected function processInvalidRow($rowNumber)
117  {
118  if (null !== $rowNumber) {
119  $rowNumber = (int)$rowNumber;
120  if (!in_array($rowNumber, $this->invalidRows)) {
121  $this->invalidRows[] = $rowNumber;
122  }
123  }
124 
125  return $this;
126  }
127 
134  {
135  $this->messageTemplate[$code] = $template;
136 
137  return $this;
138  }
139 
144  public function isRowInvalid($rowNumber)
145  {
146  return in_array((int)$rowNumber, array_merge($this->invalidRows, $this->skippedRows));
147  }
148 
152  public function getInvalidRowsCount()
153  {
154  return count($this->invalidRows);
155  }
156 
163  public function initValidationStrategy($validationStrategy, $allowedErrorCount = 0)
164  {
165  $allowedStrategy = [
168  ];
169  if (!in_array($validationStrategy, $allowedStrategy)) {
170  throw new \Magento\Framework\Exception\LocalizedException(
171  __('ImportExport: Import Data validation - Validation strategy not found')
172  );
173  }
174  $this->validationStrategy = $validationStrategy;
175  $this->allowedErrorsCount = (int)$allowedErrorCount;
176 
177  return $this;
178  }
179 
183  public function hasToBeTerminated()
184  {
185  return $this->hasFatalExceptions() || $this->isErrorLimitExceeded();
186  }
187 
191  public function isErrorLimitExceeded()
192  {
193  $isExceeded = false;
195  if ($errorsCount > 0
196  && $this->validationStrategy == self::VALIDATION_STRATEGY_STOP_ON_ERROR
197  && $errorsCount >= $this->allowedErrorsCount
198  ) {
199  $isExceeded = true;
200  }
201 
202  return $isExceeded;
203  }
204 
208  public function hasFatalExceptions()
209  {
211  }
212 
216  public function getAllErrors()
217  {
218  $result = [];
219  if (empty($this->items)) {
220  return $result;
221  }
222 
223  foreach (array_values($this->items['rows']) as $errors) {
224  $result = array_merge($result, $errors);
225  }
226 
227  return $result;
228  }
229 
234  public function getErrorsByCode(array $codes)
235  {
236  $result = [];
237  foreach ($codes as $code) {
238  if (isset($this->items['codes'][$code])) {
239  $result = array_merge($result, $this->items['codes'][$code]);
240  }
241  }
242 
243  return $result;
244  }
245 
250  public function getErrorByRowNumber($rowNumber)
251  {
252  $result = [];
253  if (isset($this->items['rows'][$rowNumber])) {
254  $result = $this->items['rows'][$rowNumber];
255  }
256 
257  return $result;
258  }
259 
266  public function getRowsGroupedByErrorCode(
267  array $errorCode = [],
268  array $excludedCodes = [],
269  $replaceCodeWithMessage = true
270  ) {
271  if (empty($this->items)) {
272  return [];
273  }
274  $allCodes = array_keys($this->items['codes']);
275  if (!empty($excludedCodes)) {
276  $allCodes = array_diff($allCodes, $excludedCodes);
277  }
278  if (!empty($errorCode)) {
279  $allCodes = array_intersect($errorCode, $allCodes);
280  }
281 
282  $result = [];
283  foreach ($allCodes as $code) {
284  $errors = $this->getErrorsByCode([$code]);
285  foreach ($errors as $error) {
286  $key = $replaceCodeWithMessage ? $error->getErrorMessage() : $code;
287  $result[$key][] = $error->getRowNumber() + 1;
288  }
289  }
290 
291  return $result;
292  }
293 
297  public function getAllowedErrorsCount()
298  {
300  }
301 
306  public function getErrorsCount(
307  array $errorLevels = [
310  ]
311  ) {
312  $result = 0;
313  foreach ($errorLevels as $errorLevel) {
314  $result += isset($this->errorStatistics[$errorLevel]) ? $this->errorStatistics[$errorLevel] : 0;
315  }
316 
317  return $result;
318  }
319 
323  public function clear()
324  {
325  $this->items = [];
326  $this->errorStatistics = [];
327  $this->invalidRows = [];
328  $this->skippedRows = [];
329 
330  return $this;
331  }
332 
339  protected function isErrorAlreadyAdded($rowNum, $errorCode, $columnName = null)
340  {
341  $errors = $this->getErrorsByCode([$errorCode]);
342  foreach ($errors as $error) {
343  if ($rowNum == $error->getRowNumber() && $columnName == $error->getColumnName()) {
344  return true;
345  }
346  }
347  return false;
348  }
349 
356  protected function getErrorMessage($errorCode, $errorMessage, $columnName)
357  {
358  if (null === $errorMessage && isset($this->messageTemplate[$errorCode])) {
359  $errorMessage = (string)__($this->messageTemplate[$errorCode]);
360  }
361  if ($columnName && $errorMessage) {
362  $errorMessage = sprintf($errorMessage, $columnName);
363  }
364  if (!$errorMessage) {
365  $errorMessage = $errorCode;
366  }
367 
368  return $errorMessage;
369  }
370 
375  protected function processErrorStatistics($errorLevel)
376  {
377  if (!empty($errorLevel)) {
378  isset($this->errorStatistics[$errorLevel]) ?
379  $this->errorStatistics[$errorLevel]++ : $this->errorStatistics[$errorLevel] = 1;
380  }
381 
382  return $this;
383  }
384 }
__construct(\Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorFactory $errorFactory)
getRowsGroupedByErrorCode(array $errorCode=[], array $excludedCodes=[], $replaceCodeWithMessage=true)
__()
Definition: __.php:13
addError( $errorCode, $errorLevel=ProcessingError::ERROR_LEVEL_CRITICAL, $rowNumber=null, $columnName=null, $errorMessage=null, $errorDescription=null)
getErrorsCount(array $errorLevels=[ProcessingError::ERROR_LEVEL_CRITICAL, ProcessingError::ERROR_LEVEL_NOT_CRITICAL])
$template
Definition: export.php:12
$errors
Definition: overview.phtml:9
$code
Definition: info.phtml:12