Exemple #1
0
 /**
  * @param \Magento\Framework\Stdlib\String $string
  * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  * @param \Magento\ImportExport\Model\ImportFactory $importFactory
  * @param \Magento\ImportExport\Model\Resource\Helper $resourceHelper
  * @param \Magento\Framework\App\Resource $resource
  * @param array $data
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function __construct(\Magento\Framework\Stdlib\String $string, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\ImportExport\Model\ImportFactory $importFactory, \Magento\ImportExport\Model\Resource\Helper $resourceHelper, \Magento\Framework\App\Resource $resource, array $data = [])
 {
     $this->_scopeConfig = $scopeConfig;
     $this->_dataSourceModel = isset($data['data_source_model']) ? $data['data_source_model'] : $importFactory->create()->getDataSourceModel();
     $this->_connection = isset($data['connection']) ? $data['connection'] : $resource->getConnection('write');
     $this->string = $string;
     $this->_pageSize = isset($data['page_size']) ? $data['page_size'] : (static::XML_PATH_PAGE_SIZE ? (int) $this->_scopeConfig->getValue(static::XML_PATH_PAGE_SIZE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE) : 0);
     $this->_maxDataSize = isset($data['max_data_size']) ? $data['max_data_size'] : $resourceHelper->getMaxDataSize();
     $this->_bunchSize = isset($data['bunch_size']) ? $data['bunch_size'] : (static::XML_PATH_BUNCH_SIZE ? (int) $this->_scopeConfig->getValue(static::XML_PATH_BUNCH_SIZE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE) : 0);
 }
 /**
  * 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;
 }