diff --git a/README.md b/README.md index f28afdc..a04d984 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,8 @@ Optional values are: * incoming_directory - default: `jh_import/incoming` * archived_directory - default: `jh_import/archived` * failed_directory - default: `jh_import/failed` + * archive_already_imported_files - default: `false` + * process_only_last_file - default: `false` The above directories will be created in the `var` directory of the magento instance if they do not already exist. @@ -218,6 +220,17 @@ The finished config my look like: ``` +#### archive_already_imported_files + +By enabling this option all files reported as already imported will be moved into `failed_directory` + instead of being left in `incoming_directory`. + +#### process_only_last_file + +This option enabled will process only last file from matched batch of files. Useful when you have multiple files of same content + and you want to save your processing time by importing data only from last one of them. Other matched files from batch + will be moved into `failed_directory`. + ### DB import type The `name` attribute is the unique name for your import and is how you execute it from `\Jh\Import\Import\Manager`. Here we named diff --git a/src/Config/Converter.php b/src/Config/Converter.php index b839011..1fa35df 100644 --- a/src/Config/Converter.php +++ b/src/Config/Converter.php @@ -26,6 +26,8 @@ class Converter implements ConverterInterface 'delete_old_files' => ['type' => 'bool', 'default' => false], 'archive_date_format' => ['type' => 'string', 'default' => 'dmYhis'], 'directory_permissions' => ['type' => 'int', 'default' => 0755], + 'archive_already_imported_files' => ['type' => 'bool', 'default' => false], + 'process_only_last_file' => ['type' => 'bool', 'default' => false], ], 'db' => [ 'connection_name' => ['type' => 'string'], diff --git a/src/Import/Importer.php b/src/Import/Importer.php index bcbf53e..e343bff 100644 --- a/src/Import/Importer.php +++ b/src/Import/Importer.php @@ -4,6 +4,7 @@ use Countable; use Exception; +use Jh\Import\Archiver\Archiver; use Jh\Import\Archiver\Factory as ArchiverFactory; use Jh\Import\Config; use Jh\Import\Locker\ImportLockedException; @@ -107,10 +108,13 @@ public function transform(callable $transform): void $this->transformers[] = $transform; } - private function canImport(string $importName, Report $report): bool + private function canImport(Config $config, Report $report, Archiver $archiver): bool { if ($this->history->isImported($this->source)) { $report->addError('This import source has already been imported.'); + if ($config->get('archive_already_imported_files')) { + $archiver->failed(); + } return false; } @@ -121,7 +125,7 @@ private function canImport(string $importName, Report $report): bool try { //check if an import by this name is already running - $this->locker->lock($importName); + $this->locker->lock($config->getImportName()); } catch (ImportLockedException $e) { $report->addError($e->getMessage()); return false; @@ -133,9 +137,11 @@ private function canImport(string $importName, Report $report): bool public function process(Config $config): void { $report = $this->reportFactory->createFromSourceAndConfig($this->source, $config); + $archiver = $this->archiverFactory->getArchiverForSource($this->source, $config); + $report->start(); - if (!$this->canImport($config->getImportName(), $report)) { + if (!$this->canImport($config, $report, $archiver)) { $this->endReport($report); return; } @@ -158,7 +164,6 @@ public function process(Config $config): void } try { - $archiver = $this->archiverFactory->getArchiverForSource($this->source, $config); $report->isSuccessful() ? $archiver->successful() : $archiver->failed(); } catch (Exception $e) { $report->addError(sprintf( @@ -172,6 +177,12 @@ public function process(Config $config): void $this->endReport($report); } + public function skip(Config $config) + { + $archiver = $this->archiverFactory->getArchiverForSource($this->source, $config); + $archiver->failed(); + } + private function endReport(Report $report): void { $report->finish(new \DateTime(), memory_get_usage(true)); diff --git a/src/Type/Files.php b/src/Type/Files.php index 0218576..ff879a2 100644 --- a/src/Type/Files.php +++ b/src/Type/Files.php @@ -60,14 +60,19 @@ public function run(Config $config) $specification = $this->objectManager->get($config->getSpecificationService()); $writer = $this->objectManager->get($config->getWriterService()); - $filesToProcess->each(function ($file) use ($config, $specification, $writer) { + $lastFileIndex = $filesToProcess->count() - 1; + $filesToProcess->each(function ($file, $index) use ($config, $specification, $writer, $lastFileIndex) { $source = $this->objectManager->create($config->getSourceService(), [ 'file' => $file ]); - $this->importerFactory - ->create($source, $specification, $writer) - ->process($config); + $importer = $this->importerFactory->create($source, $specification, $writer); + + if ($config->get('process_only_last_file') && $index < $lastFileIndex) { + $importer->skip($config); + } else { + $importer->process($config); + } }); } diff --git a/src/etc/imports.xsd b/src/etc/imports.xsd index ca58d9b..ace4d30 100644 --- a/src/etc/imports.xsd +++ b/src/etc/imports.xsd @@ -42,6 +42,8 @@ + +