Skip to content

Commit

Permalink
New options to files type import (#58)
Browse files Browse the repository at this point in the history
* new options
- archive already imported files
-

* added new options into readme
  • Loading branch information
freezy-sk authored Nov 16, 2022
1 parent 8e17655 commit 48fb6f5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -218,6 +220,17 @@ The finished config my look like:
</config>
```

#### 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
Expand Down
2 changes: 2 additions & 0 deletions src/Config/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
19 changes: 15 additions & 4 deletions src/Import/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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(
Expand All @@ -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));
Expand Down
13 changes: 9 additions & 4 deletions src/Type/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/etc/imports.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
</xs:element>
<xs:element name="archive_old_files" type="xs:boolean" minOccurs="0" maxOccurs="1"/>
<xs:element name="delete_old_files" type="xs:boolean" minOccurs="0" maxOccurs="1"/>
<xs:element name="archive_already_imported_files" type="xs:boolean" minOccurs="0" maxOccurs="1"/>
<xs:element name="process_only_last_file" type="xs:boolean" minOccurs="0" maxOccurs="1"/>
</xs:all>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
Expand Down

0 comments on commit 48fb6f5

Please sign in to comment.