Example #1
0
 /**
  * {@inheritDoc}
  */
 protected function setUp()
 {
     $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)->getMockForAbstractClass();
     $this->filesystemMock = $this->getMockBuilder(Filesystem::class)->disableOriginalConstructor()->getMock();
     $this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)->getMockForAbstractClass();
     $this->rowParserMock = $this->getMockBuilder(RowParser::class)->disableOriginalConstructor()->getMock();
     $this->columnResolverFactoryMock = $this->getMockBuilder(ColumnResolverFactory::class)->setMethods(['create'])->disableOriginalConstructor()->getMock();
     $this->dataHashGeneratorMock = $this->getMockBuilder(DataHashGenerator::class)->getMock();
     $this->rowParserMock->expects($this->any())->method('parse')->willReturnArgument(0);
     $this->dataHashGeneratorMock->expects($this->any())->method('getHash')->willReturnCallback(function (array $data) {
         return implode('_', $data);
     });
     $this->import = new Import($this->storeManagerMock, $this->filesystemMock, $this->scopeConfigMock, $this->rowParserMock, $this->columnResolverFactoryMock, $this->dataHashGeneratorMock);
 }
Example #2
0
 /**
  * @param ReadInterface $file
  * @param int $websiteId
  * @param string $conditionShortName
  * @param string $conditionFullName
  * @param int $bunchSize
  * @return \Generator
  * @throws LocalizedException
  */
 public function getData(ReadInterface $file, $websiteId, $conditionShortName, $conditionFullName, $bunchSize = 5000)
 {
     $this->errors = [];
     $headers = $this->getHeaders($file);
     /** @var ColumnResolver $columnResolver */
     $columnResolver = $this->columnResolverFactory->create(['headers' => $headers]);
     $rowNumber = 1;
     $items = [];
     while (false !== ($csvLine = $file->readCsv())) {
         try {
             $rowNumber++;
             if (empty($csvLine)) {
                 continue;
             }
             $rowData = $this->rowParser->parse($csvLine, $rowNumber, $websiteId, $conditionShortName, $conditionFullName, $columnResolver);
             // protect from duplicate
             $hash = $this->dataHashGenerator->getHash($rowData);
             if (array_key_exists($hash, $this->uniqueHash)) {
                 throw new RowException(__('Duplicate Row #%1 (duplicates row #%2)', $rowNumber, $this->uniqueHash[$hash]));
             }
             $this->uniqueHash[$hash] = $rowNumber;
             $items[] = $rowData;
             if (count($items) === $bunchSize) {
                 (yield $items);
                 $items = [];
             }
         } catch (RowException $e) {
             $this->errors[] = $e->getMessage();
         }
     }
     if (count($items)) {
         (yield $items);
     }
 }