Skip to content

Commit 8e17655

Browse files
authored
Configurable default options of import types (#57)
* Configure default options of import types through application config * configurable date format of archived files and directory permissions * sources made optional with default classes * updated readme * fixed bool casting * changed permissions to recommended by adobe
1 parent 7eb2598 commit 8e17655

File tree

5 files changed

+93
-16
lines changed

5 files changed

+93
-16
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,33 @@ class Price implements \Jh\Import\Writer\Writer
663663
}
664664
```
665665

666+
### Configure default options for import types
667+
668+
All import types options can have their default values configured through `app/etc/config.php` or `app/etc/env.php`.
669+
670+
```php
671+
'system' => [
672+
'default' => [
673+
...
674+
'jh_import' => [
675+
'default' => [
676+
'files' => [
677+
'source' => \Jh\Import\Source\Csv::class,
678+
'archive_date_format' => 'YmdHis',
679+
'directory_permissions' => 0755,
680+
],
681+
'db' => [
682+
'connection_name' => 'default',
683+
],
684+
]
685+
],
686+
...
687+
],
688+
...
689+
],
690+
...
691+
```
692+
666693
## Report Handlers
667694

668695
Report handlers deal with debug information and errors that happen during the import process. By default the only report handler

src/Archiver/CsvArchiver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,16 @@ public function successful(): void
131131
private function ensureDirectoryExists(string $directory)
132132
{
133133
if (!$this->filesystem->isExists($directory)) {
134-
$this->filesystem->createDirectory($directory, 0777);
134+
$this->filesystem->createDirectory($directory, $this->config->get('directory_permissions'));
135135
}
136136
}
137137

138-
private function newName(\SplFileObject $file): string
138+
protected function newName(\SplFileObject $file): string
139139
{
140140
return sprintf(
141141
'%s-%s.%s',
142142
$file->getBasename('.' . $file->getExtension()),
143-
$this->getDateTime()->format('dmYhis'),
143+
$this->getDateTime()->format($this->config->get('archive_date_format')),
144144
$file->getExtension()
145145
);
146146
}

src/Config/AppConfigProvider.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jh\Import\Config;
6+
7+
use Magento\Framework\App\Config\ScopeConfigInterface;
8+
9+
class AppConfigProvider
10+
{
11+
private const CONFIG_PATH_PREFIX = 'jh_import/default';
12+
13+
protected ScopeConfigInterface $scopeConfig;
14+
15+
public function __construct(ScopeConfigInterface $scopeConfig)
16+
{
17+
$this->scopeConfig = $scopeConfig;
18+
}
19+
20+
public function getImportTypeOptionDefaultValue(string $importType, string $option)
21+
{
22+
$value = $this->scopeConfig->getValue(
23+
sprintf('%s/%s/%s', self::CONFIG_PATH_PREFIX, $importType, $option),
24+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
25+
null
26+
);
27+
28+
return $value ?: null;
29+
}
30+
}

src/Config/Converter.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Converter implements ConverterInterface
1212
*/
1313
private static $importTypesWithRequiredFields = [
1414
'files' => [
15-
'source' => ['type' => 'string'],
15+
'source' => ['type' => 'string', 'default' => \Jh\Import\Source\Csv::class],
1616
'incoming_directory' => ['type' => 'string', 'default' => 'jh_import/incoming'],
1717
'archived_directory' => ['type' => 'string', 'default' => 'jh_import/archived'],
1818
'failed_directory' => ['type' => 'string', 'default' => 'jh_import/failed'],
@@ -24,10 +24,12 @@ class Converter implements ConverterInterface
2424
'cron_group' => ['type' => 'string', 'default' => 'default'],
2525
'archive_old_files' => ['type' => 'bool', 'default' => false],
2626
'delete_old_files' => ['type' => 'bool', 'default' => false],
27+
'archive_date_format' => ['type' => 'string', 'default' => 'dmYhis'],
28+
'directory_permissions' => ['type' => 'int', 'default' => 0755],
2729
],
2830
'db' => [
2931
'connection_name' => ['type' => 'string'],
30-
'source' => ['type' => 'string'],
32+
'source' => ['type' => 'string', 'default' => \Jh\Import\Source\Db::class],
3133
'specification' => ['type' => 'string'],
3234
'writer' => ['type' => 'string'],
3335
'id_field' => ['type' => 'string'],
@@ -38,7 +40,7 @@ class Converter implements ConverterInterface
3840
'cron_group' => ['type' => 'string', 'default' => 'default']
3941
],
4042
'webapi' => [
41-
'source' => ['type' => 'string'],
43+
'source' => ['type' => 'string', 'default' => \Jh\Import\Source\Webapi::class],
4244
'source_id' => ['type' => 'string'],
4345
'specification' => ['type' => 'string'],
4446
'writer' => ['type' => 'string'],
@@ -55,6 +57,13 @@ class Converter implements ConverterInterface
5557
]
5658
];
5759

60+
private AppConfigProvider $appConfigProvider;
61+
62+
public function __construct(AppConfigProvider $appConfigProvider)
63+
{
64+
$this->appConfigProvider = $appConfigProvider;
65+
}
66+
5867
public function convert($source): array
5968
{
6069
$names = collect(static::$importTypesWithRequiredFields)
@@ -91,24 +100,26 @@ private function getOptions(\DOMElement $import, array $requiredFields, string $
91100
/** @var \DOMNodeList $elements */
92101
$elements = $import->getElementsByTagName($requiredField);
93102

103+
// load default value from app config
104+
$value = $this->appConfigProvider->getImportTypeOptionDefaultValue($importType, $requiredField);
105+
106+
// override by import config
94107
if ($elements->length > 0) {
95108
$value = $elements->item(0)->nodeValue;
109+
}
96110

111+
if ($value !== null) {
97112
switch ($spec['type']) {
98113
case 'bool':
99114
return $this->castBool($value);
115+
case 'int':
116+
return $this->castInt($value);
100117
case 'string':
101118
return $this->castString($value);
102119
}
103-
104-
return $value;
105120
}
106121

107-
if (isset($spec['default'])) {
108-
return $spec['default'];
109-
}
110-
111-
return null;
122+
return $value ?? $spec['default'] ?? null;
112123
});
113124

114125
//parse required indexers
@@ -145,8 +156,17 @@ private function castString($value): string
145156
return (string) $value;
146157
}
147158

159+
private function castInt($value): int
160+
{
161+
return (int) $value;
162+
}
163+
148164
private function castBool($value): bool
149165
{
166+
if (is_bool($value)) {
167+
return $value;
168+
}
169+
150170
return $value === 'true' || $value === '1';
151171
}
152172
}

src/etc/imports.xsd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</xs:documentation>
1717
</xs:annotation>
1818
<xs:all>
19-
<xs:element name="source" minOccurs="1" maxOccurs="1" type="xs:string"/>
19+
<xs:element name="source" minOccurs="0" maxOccurs="1" type="xs:string"/>
2020
<xs:element name="incoming_directory" minOccurs="0" maxOccurs="1" type="xs:string"/>
2121
<xs:element name="archived_directory" minOccurs="0" maxOccurs="1" type="xs:string"/>
2222
<xs:element name="failed_directory" minOccurs="0" maxOccurs="1" type="xs:string"/>
@@ -53,7 +53,7 @@
5353
</xs:annotation>
5454
<xs:all>
5555
<xs:element name="connection_name" minOccurs="1" maxOccurs="1" type="xs:string"/>
56-
<xs:element name="source" minOccurs="1" maxOccurs="1" type="xs:string"/>
56+
<xs:element name="source" minOccurs="0" maxOccurs="1" type="xs:string"/>
5757
<xs:element name="specification" minOccurs="1" maxOccurs="1" type="xs:string"/>
5858
<xs:element name="writer" minOccurs="1" maxOccurs="1" type="xs:string"/>
5959
<xs:element name="id_field" minOccurs="1" maxOccurs="1" type="xs:string"/>
@@ -86,7 +86,7 @@
8686
</xs:documentation>
8787
</xs:annotation>
8888
<xs:all>
89-
<xs:element name="source" minOccurs="1" maxOccurs="1" type="xs:string"/>
89+
<xs:element name="source" minOccurs="0" maxOccurs="1" type="xs:string"/>
9090
<xs:element name="source_id" minOccurs="1" maxOccurs="1" type="xs:string"/>
9191
<xs:element name="specification" minOccurs="1" maxOccurs="1" type="xs:string"/>
9292
<xs:element name="writer" minOccurs="1" maxOccurs="1" type="xs:string"/>

0 commit comments

Comments
 (0)