Example #1
0
 /**
  * Validate data rows and save bunches to DB
  *
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function _saveValidatedBunches()
 {
     $source = $this->getSource();
     $bunchRows = [];
     $startNewBunch = false;
     $source->rewind();
     $this->_dataSourceModel->cleanBunches();
     $masterAttributeCode = $this->getMasterAttributeCode();
     while ($source->valid() || count($bunchRows) || isset($entityGroup)) {
         if ($startNewBunch || !$source->valid()) {
             /* If the end approached add last validated entity group to the bunch */
             if (!$source->valid() && isset($entityGroup)) {
                 foreach ($entityGroup as $key => $value) {
                     $bunchRows[$key] = $value;
                 }
                 unset($entityGroup);
             }
             $this->_dataSourceModel->saveBunch($this->getEntityTypeCode(), $this->getBehavior(), $bunchRows);
             $bunchRows = [];
             $startNewBunch = false;
         }
         if ($source->valid()) {
             // errors limit check
             if ($this->_errorsCount >= $this->_errorsLimit) {
                 return $this;
             }
             $rowData = $source->current();
             if (isset($rowData[$masterAttributeCode]) && trim($rowData[$masterAttributeCode])) {
                 /* Add entity group that passed validation to bunch */
                 if (isset($entityGroup)) {
                     foreach ($entityGroup as $key => $value) {
                         $bunchRows[$key] = $value;
                     }
                     $productDataSize = strlen(serialize($bunchRows));
                     /* Check if the new bunch should be started */
                     $isBunchSizeExceeded = $this->_bunchSize > 0 && count($bunchRows) >= $this->_bunchSize;
                     $startNewBunch = $productDataSize >= $this->_maxDataSize || $isBunchSizeExceeded;
                 }
                 /* And start a new one */
                 $entityGroup = [];
             }
             if (isset($entityGroup) && $this->validateRow($rowData, $source->key())) {
                 /* Add row to entity group */
                 $entityGroup[$source->key()] = $this->_prepareRowForDb($rowData);
             } elseif (isset($entityGroup)) {
                 /* In case validation of one line of the group fails kill the entire group */
                 unset($entityGroup);
             }
             $this->_processedRowsCount++;
             $source->next();
         }
     }
     return $this;
 }
 /**
  * Validate data rows and save bunches to DB.
  *
  * @return $this|void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function _saveValidatedBunches()
 {
     $source = $this->_getSource();
     $currentDataSize = 0;
     $bunchRows = [];
     $startNewBunch = false;
     $nextRowBackup = [];
     $maxDataSize = $this->_resourceHelper->getMaxDataSize();
     $bunchSize = $this->_importExportData->getBunchSize();
     $source->rewind();
     $this->_dataSourceModel->cleanBunches();
     while ($source->valid() || $bunchRows) {
         if ($startNewBunch || !$source->valid()) {
             $this->_dataSourceModel->saveBunch($this->getEntityTypeCode(), $this->getBehavior(), $bunchRows);
             $bunchRows = $nextRowBackup;
             $currentDataSize = strlen(serialize($bunchRows));
             $startNewBunch = false;
             $nextRowBackup = [];
         }
         if ($source->valid()) {
             if ($this->_errorsCount >= $this->_errorsLimit) {
                 // errors limit check
                 return;
             }
             $rowData = $source->current();
             $this->_processedRowsCount++;
             if ($this->validateRow($rowData, $source->key())) {
                 // add row to bunch for save
                 $rowData = $this->_prepareRowForDb($rowData);
                 $rowSize = strlen($this->jsonHelper->jsonEncode($rowData));
                 $isBunchSizeExceeded = $bunchSize > 0 && count($bunchRows) >= $bunchSize;
                 if ($currentDataSize + $rowSize >= $maxDataSize || $isBunchSizeExceeded) {
                     $startNewBunch = true;
                     $nextRowBackup = [$source->key() => $rowData];
                 } else {
                     $bunchRows[$source->key()] = $rowData;
                     $currentDataSize += $rowSize;
                 }
             }
             $source->next();
         }
     }
     return $this;
 }