/** * {@inheritdoc} */ public function read() { $filePath = null; if (null === $this->fileIterator) { $jobParameters = $this->stepExecution->getJobParameters(); $filePath = $jobParameters->get('filePath'); $this->fileIterator = $this->fileIteratorFactory->create($filePath, $this->options); $this->fileIterator->rewind(); } $this->fileIterator->next(); if ($this->fileIterator->valid() && null !== $this->stepExecution) { $this->stepExecution->incrementSummaryInfo('read_lines'); } $data = $this->fileIterator->current(); if (null === $data) { return null; } $headers = $this->fileIterator->getHeaders(); $countHeaders = count($headers); $countData = count($data); $this->checkColumnNumber($countHeaders, $countData, $data, $filePath); if ($countHeaders > $countData) { $missingValuesCount = $countHeaders - $countData; $missingValues = array_fill(0, $missingValuesCount, ''); $data = array_merge($data, $missingValues); } $item = array_combine($this->fileIterator->getHeaders(), $data); try { $item = $this->converter->convert($item, $this->getArrayConverterOptions()); } catch (DataArrayConversionException $e) { $this->skipItemFromConversionException($item, $e); } return $item; }
/** * {@inheritdoc} */ public function write(array $variantGroups) { $jobParameters = $this->stepExecution->getJobParameters(); $isCopyValues = $jobParameters->get('copyValues'); if ($isCopyValues) { $this->copyValuesToProducts($variantGroups); } $this->incrementCount($variantGroups); $this->bulkSaver->saveAll($variantGroups); $this->bulkDetacher->detachAll($variantGroups); }
/** * {@inheritdoc} */ public function write(array $items) { $jobParameters = $this->stepExecution->getJobParameters(); $realTimeVersioning = $jobParameters->get('realTimeVersioning'); $this->versionManager->setRealTimeVersioning($realTimeVersioning); foreach ($items as $item) { $this->incrementCount($item); } $this->productSaver->saveAll($items); $this->detacher->detachAll($items); }
/** * {@inheritdoc} */ public function process($variantGroup) { $variantGroupStandard = $this->normalizer->normalize($variantGroup, null, ['with_variant_group_values' => true, 'identifier' => $variantGroup->getCode()]); $parameters = $this->stepExecution->getJobParameters(); if ($parameters->has('with_media') && $parameters->get('with_media')) { $directory = $this->stepExecution->getJobExecution()->getExecutionContext()->get(JobInterface::WORKING_DIRECTORY_PARAMETER); $this->fetchMedia($variantGroup, $directory); } $this->objectDetacher->detach($variantGroup); return $variantGroupStandard; }
/** * @param array $headers * * @return array */ protected function sortHeaders(array $headers) { if (null !== $this->columnSorter) { $headers = $this->columnSorter->sort($headers, $this->stepExecution->getJobParameters()->all()); } return $headers; }
/** * Returns the filters from the configuration. * The parameters can be in the 'filters' root node, or in filters data node (e.g. for export). * * @return array */ protected function getConfiguredFilters() { $filters = $this->stepExecution->getJobParameters()->get('filters'); if (array_key_exists('data', $filters)) { $filters = $filters['data']; } return array_filter($filters, function ($filter) { return count($filter) > 0; }); }
/** * Validate that the file is correctly encoded in the provided charset. * * @throws CharsetException * @throws \Exception */ protected function validateEncoding() { $jobParameters = $this->stepExecution->getJobParameters(); $filePath = $jobParameters->get('filePath'); $handle = fopen($filePath, 'r'); if (false === $handle) { throw new \Exception(sprintf('Unable to read the file "%s".', $filePath)); } $errors = []; $lineNo = 0; while (false !== ($line = fgets($handle)) && count($errors) < $this->maxErrors) { $lineNo++; if (false === iconv($this->charset, $this->charset, $line)) { $errors[] = $lineNo; } } fclose($handle); if (count($errors) > 0) { $message = count($errors) === $this->maxErrors ? sprintf('The first %s erroneous lines are %s.', $this->maxErrors, implode(', ', $errors)) : sprintf('The lines %s are erroneous.', implode(', ', $errors)); throw new CharsetException(sprintf('The file "%s" is not correctly encoded in %s. ', $filePath, $this->charset) . $message); } $this->stepExecution->addSummaryInfo('charset_validator.title', $this->charset . ' OK'); }
/** * {@inheritdoc} */ public function process($product) { $parameters = $this->stepExecution->getJobParameters(); $structure = $parameters->get('filters')['structure']; $channel = $this->channelRepository->findOneByIdentifier($structure['scope']); $this->productBuilder->addMissingProductValues($product, [$channel], $channel->getLocales()->toArray()); $productStandard = $this->normalizer->normalize($product, 'json', ['channels' => [$channel->getCode()], 'locales' => array_intersect($channel->getLocaleCodes(), $parameters->get('filters')['structure']['locales'])]); if ($this->areAttributesToFilter($parameters)) { $attributesToFilter = $this->getAttributesToFilter($parameters); $productStandard['values'] = $this->filterValues($productStandard['values'], $attributesToFilter); } if ($parameters->has('with_media') && $parameters->get('with_media')) { $directory = $this->stepExecution->getJobExecution()->getExecutionContext()->get(JobInterface::WORKING_DIRECTORY_PARAMETER); $this->fetchMedia($product, $directory); } else { $mediaAttributes = $this->attributeRepository->findMediaAttributeCodes(); $productStandard['values'] = array_filter($productStandard['values'], function ($attributeCode) use($mediaAttributes) { return !in_array($attributeCode, $mediaAttributes); }, ARRAY_FILTER_USE_KEY); } $this->detacher->detach($product); return $productStandard; }
/** * Returns the file data * * @return array */ protected function getFileData() { $jobParameters = $this->stepExecution->getJobParameters(); $filePath = $jobParameters->get('filePath'); $fileData = current(Yaml::parse(file_get_contents($filePath))); if (null === $fileData) { return null; } foreach ($fileData as $key => $row) { if ($this->codeField && !isset($row[$this->codeField])) { $fileData[$key][$this->codeField] = $key; } } return $this->multiple ? [$fileData] : $fileData; }
/** * @return array */ protected function getConfiguredFilters() { $jobParameters = $this->stepExecution->getJobParameters(); return $jobParameters->get('filters'); }
/** * Get the file path in which to write the data * * @return string */ public function getPath() { $parameters = $this->stepExecution->getJobParameters(); return $parameters->get('filePath'); }
function it_throws_an_exception_if_type_is_not_recognized($columnSorter, FlatItemBuffer $buffer, StepExecution $stepExecution, JobParameters $parameters) { $columnSorter->sort(Argument::any(), [])->willReturn(['colA', 'colB']); $stepExecution->getJobParameters()->willReturn($parameters); $parameters->all()->willReturn([]); $buffer->getHeaders()->willReturn(['colA', 'colB']); $this->shouldThrow('Box\\Spout\\Common\\Exception\\UnsupportedTypeException')->during('flush', [$buffer, ['type' => 'undefined'], Argument::any()]); }