-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFiles.php
102 lines (86 loc) · 2.77 KB
/
Files.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace Jh\Import\Type;
use Jh\Import\Config;
use Jh\Import\Import\ImporterFactory;
use Illuminate\Support\Collection;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\Directory\WriteFactory;
use Magento\Framework\ObjectManagerInterface;
/**
* @author Aydin Hassan <[email protected]>
*/
class Files implements Type
{
/**
* @var DirectoryList
*/
private $directoryList;
/**
* @var WriteFactory
*/
private $writeFactory;
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var ImporterFactory
*/
private $importerFactory;
/**
* @var FileMatcher
*/
private $filesMatcher;
public function __construct(
DirectoryList $directoryList,
WriteFactory $writeFactory,
ObjectManagerInterface $objectManager,
ImporterFactory $importerFactory,
FileMatcher $filesMatcher
) {
$this->directoryList = $directoryList;
$this->writeFactory = $writeFactory;
$this->objectManager = $objectManager;
$this->importerFactory = $importerFactory;
$this->filesMatcher = $filesMatcher;
}
public function run(Config $config)
{
$filesToProcess = $this->getFilesToProcess($config);
$specification = $this->objectManager->get($config->getSpecificationService());
$writer = $this->objectManager->get($config->getWriterService());
$lastFileIndex = $filesToProcess->count() - 1;
$filesToProcess->each(function ($file, $index) use ($config, $specification, $writer, $lastFileIndex) {
$source = $this->objectManager->create($config->getSourceService(), [
'file' => $file
]);
$importer = $this->importerFactory->create($source, $specification, $writer);
if ($config->get('process_only_last_file') && $index < $lastFileIndex) {
$importer->skip($config);
} else {
$importer->process($config);
}
});
}
private function getFilesToProcess(Config $config): Collection
{
$directoryWriter = $this->writeFactory->create(
sprintf(
'%s/%s',
$this->directoryList->getPath(DirectoryList::VAR_DIR),
$config->get('incoming_directory')
)
);
//ensure directory is created
$directoryWriter->create();
return $this->filesMatcher->matched(
$config->get('match_files'),
array_map(
function (string $file) use ($directoryWriter) {
return $directoryWriter->getAbsolutePath($file);
},
$directoryWriter->read()
)
);
}
}