Skip to content

Commit 48fb6f5

Browse files
authored
New options to files type import (#58)
* new options - archive already imported files - * added new options into readme
1 parent 8e17655 commit 48fb6f5

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ Optional values are:
147147
* incoming_directory - default: `jh_import/incoming`
148148
* archived_directory - default: `jh_import/archived`
149149
* failed_directory - default: `jh_import/failed`
150+
* archive_already_imported_files - default: `false`
151+
* process_only_last_file - default: `false`
150152

151153
The above directories will be created in the `var` directory of the magento instance if they do not already exist.
152154

@@ -218,6 +220,17 @@ The finished config my look like:
218220
</config>
219221
```
220222

223+
#### archive_already_imported_files
224+
225+
By enabling this option all files reported as already imported will be moved into `failed_directory`
226+
instead of being left in `incoming_directory`.
227+
228+
#### process_only_last_file
229+
230+
This option enabled will process only last file from matched batch of files. Useful when you have multiple files of same content
231+
and you want to save your processing time by importing data only from last one of them. Other matched files from batch
232+
will be moved into `failed_directory`.
233+
221234
### DB import type
222235

223236
The `name` attribute is the unique name for your import and is how you execute it from `\Jh\Import\Import\Manager`. Here we named

src/Config/Converter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Converter implements ConverterInterface
2626
'delete_old_files' => ['type' => 'bool', 'default' => false],
2727
'archive_date_format' => ['type' => 'string', 'default' => 'dmYhis'],
2828
'directory_permissions' => ['type' => 'int', 'default' => 0755],
29+
'archive_already_imported_files' => ['type' => 'bool', 'default' => false],
30+
'process_only_last_file' => ['type' => 'bool', 'default' => false],
2931
],
3032
'db' => [
3133
'connection_name' => ['type' => 'string'],

src/Import/Importer.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Countable;
66
use Exception;
7+
use Jh\Import\Archiver\Archiver;
78
use Jh\Import\Archiver\Factory as ArchiverFactory;
89
use Jh\Import\Config;
910
use Jh\Import\Locker\ImportLockedException;
@@ -107,10 +108,13 @@ public function transform(callable $transform): void
107108
$this->transformers[] = $transform;
108109
}
109110

110-
private function canImport(string $importName, Report $report): bool
111+
private function canImport(Config $config, Report $report, Archiver $archiver): bool
111112
{
112113
if ($this->history->isImported($this->source)) {
113114
$report->addError('This import source has already been imported.');
115+
if ($config->get('archive_already_imported_files')) {
116+
$archiver->failed();
117+
}
114118
return false;
115119
}
116120

@@ -121,7 +125,7 @@ private function canImport(string $importName, Report $report): bool
121125

122126
try {
123127
//check if an import by this name is already running
124-
$this->locker->lock($importName);
128+
$this->locker->lock($config->getImportName());
125129
} catch (ImportLockedException $e) {
126130
$report->addError($e->getMessage());
127131
return false;
@@ -133,9 +137,11 @@ private function canImport(string $importName, Report $report): bool
133137
public function process(Config $config): void
134138
{
135139
$report = $this->reportFactory->createFromSourceAndConfig($this->source, $config);
140+
$archiver = $this->archiverFactory->getArchiverForSource($this->source, $config);
141+
136142
$report->start();
137143

138-
if (!$this->canImport($config->getImportName(), $report)) {
144+
if (!$this->canImport($config, $report, $archiver)) {
139145
$this->endReport($report);
140146
return;
141147
}
@@ -158,7 +164,6 @@ public function process(Config $config): void
158164
}
159165

160166
try {
161-
$archiver = $this->archiverFactory->getArchiverForSource($this->source, $config);
162167
$report->isSuccessful() ? $archiver->successful() : $archiver->failed();
163168
} catch (Exception $e) {
164169
$report->addError(sprintf(
@@ -172,6 +177,12 @@ public function process(Config $config): void
172177
$this->endReport($report);
173178
}
174179

180+
public function skip(Config $config)
181+
{
182+
$archiver = $this->archiverFactory->getArchiverForSource($this->source, $config);
183+
$archiver->failed();
184+
}
185+
175186
private function endReport(Report $report): void
176187
{
177188
$report->finish(new \DateTime(), memory_get_usage(true));

src/Type/Files.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,19 @@ public function run(Config $config)
6060
$specification = $this->objectManager->get($config->getSpecificationService());
6161
$writer = $this->objectManager->get($config->getWriterService());
6262

63-
$filesToProcess->each(function ($file) use ($config, $specification, $writer) {
63+
$lastFileIndex = $filesToProcess->count() - 1;
64+
$filesToProcess->each(function ($file, $index) use ($config, $specification, $writer, $lastFileIndex) {
6465
$source = $this->objectManager->create($config->getSourceService(), [
6566
'file' => $file
6667
]);
6768

68-
$this->importerFactory
69-
->create($source, $specification, $writer)
70-
->process($config);
69+
$importer = $this->importerFactory->create($source, $specification, $writer);
70+
71+
if ($config->get('process_only_last_file') && $index < $lastFileIndex) {
72+
$importer->skip($config);
73+
} else {
74+
$importer->process($config);
75+
}
7176
});
7277
}
7378

src/etc/imports.xsd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
</xs:element>
4343
<xs:element name="archive_old_files" type="xs:boolean" minOccurs="0" maxOccurs="1"/>
4444
<xs:element name="delete_old_files" type="xs:boolean" minOccurs="0" maxOccurs="1"/>
45+
<xs:element name="archive_already_imported_files" type="xs:boolean" minOccurs="0" maxOccurs="1"/>
46+
<xs:element name="process_only_last_file" type="xs:boolean" minOccurs="0" maxOccurs="1"/>
4547
</xs:all>
4648
<xs:attribute name="name" type="xs:string" use="required" />
4749
</xs:complexType>

0 commit comments

Comments
 (0)