diff --git a/README.md b/README.md index 3ec966e..f28afdc 100644 --- a/README.md +++ b/README.md @@ -663,6 +663,33 @@ class Price implements \Jh\Import\Writer\Writer } ``` +### Configure default options for import types + +All import types options can have their default values configured through `app/etc/config.php` or `app/etc/env.php`. + +```php + 'system' => [ + 'default' => [ + ... + 'jh_import' => [ + 'default' => [ + 'files' => [ + 'source' => \Jh\Import\Source\Csv::class, + 'archive_date_format' => 'YmdHis', + 'directory_permissions' => 0755, + ], + 'db' => [ + 'connection_name' => 'default', + ], + ] + ], + ... + ], + ... + ], + ... +``` + ## Report Handlers Report handlers deal with debug information and errors that happen during the import process. By default the only report handler diff --git a/src/Archiver/CsvArchiver.php b/src/Archiver/CsvArchiver.php index ed5e7b8..9abc901 100644 --- a/src/Archiver/CsvArchiver.php +++ b/src/Archiver/CsvArchiver.php @@ -131,16 +131,16 @@ public function successful(): void private function ensureDirectoryExists(string $directory) { if (!$this->filesystem->isExists($directory)) { - $this->filesystem->createDirectory($directory, 0777); + $this->filesystem->createDirectory($directory, $this->config->get('directory_permissions')); } } - private function newName(\SplFileObject $file): string + protected function newName(\SplFileObject $file): string { return sprintf( '%s-%s.%s', $file->getBasename('.' . $file->getExtension()), - $this->getDateTime()->format('dmYhis'), + $this->getDateTime()->format($this->config->get('archive_date_format')), $file->getExtension() ); } diff --git a/src/Config/AppConfigProvider.php b/src/Config/AppConfigProvider.php new file mode 100644 index 0000000..d949775 --- /dev/null +++ b/src/Config/AppConfigProvider.php @@ -0,0 +1,30 @@ +scopeConfig = $scopeConfig; + } + + public function getImportTypeOptionDefaultValue(string $importType, string $option) + { + $value = $this->scopeConfig->getValue( + sprintf('%s/%s/%s', self::CONFIG_PATH_PREFIX, $importType, $option), + ScopeConfigInterface::SCOPE_TYPE_DEFAULT, + null + ); + + return $value ?: null; + } +} diff --git a/src/Config/Converter.php b/src/Config/Converter.php index 0b1ad30..b839011 100644 --- a/src/Config/Converter.php +++ b/src/Config/Converter.php @@ -12,7 +12,7 @@ class Converter implements ConverterInterface */ private static $importTypesWithRequiredFields = [ 'files' => [ - 'source' => ['type' => 'string'], + 'source' => ['type' => 'string', 'default' => \Jh\Import\Source\Csv::class], 'incoming_directory' => ['type' => 'string', 'default' => 'jh_import/incoming'], 'archived_directory' => ['type' => 'string', 'default' => 'jh_import/archived'], 'failed_directory' => ['type' => 'string', 'default' => 'jh_import/failed'], @@ -24,10 +24,12 @@ class Converter implements ConverterInterface 'cron_group' => ['type' => 'string', 'default' => 'default'], 'archive_old_files' => ['type' => 'bool', 'default' => false], 'delete_old_files' => ['type' => 'bool', 'default' => false], + 'archive_date_format' => ['type' => 'string', 'default' => 'dmYhis'], + 'directory_permissions' => ['type' => 'int', 'default' => 0755], ], 'db' => [ 'connection_name' => ['type' => 'string'], - 'source' => ['type' => 'string'], + 'source' => ['type' => 'string', 'default' => \Jh\Import\Source\Db::class], 'specification' => ['type' => 'string'], 'writer' => ['type' => 'string'], 'id_field' => ['type' => 'string'], @@ -38,7 +40,7 @@ class Converter implements ConverterInterface 'cron_group' => ['type' => 'string', 'default' => 'default'] ], 'webapi' => [ - 'source' => ['type' => 'string'], + 'source' => ['type' => 'string', 'default' => \Jh\Import\Source\Webapi::class], 'source_id' => ['type' => 'string'], 'specification' => ['type' => 'string'], 'writer' => ['type' => 'string'], @@ -55,6 +57,13 @@ class Converter implements ConverterInterface ] ]; + private AppConfigProvider $appConfigProvider; + + public function __construct(AppConfigProvider $appConfigProvider) + { + $this->appConfigProvider = $appConfigProvider; + } + public function convert($source): array { $names = collect(static::$importTypesWithRequiredFields) @@ -91,24 +100,26 @@ private function getOptions(\DOMElement $import, array $requiredFields, string $ /** @var \DOMNodeList $elements */ $elements = $import->getElementsByTagName($requiredField); + // load default value from app config + $value = $this->appConfigProvider->getImportTypeOptionDefaultValue($importType, $requiredField); + + // override by import config if ($elements->length > 0) { $value = $elements->item(0)->nodeValue; + } + if ($value !== null) { switch ($spec['type']) { case 'bool': return $this->castBool($value); + case 'int': + return $this->castInt($value); case 'string': return $this->castString($value); } - - return $value; } - if (isset($spec['default'])) { - return $spec['default']; - } - - return null; + return $value ?? $spec['default'] ?? null; }); //parse required indexers @@ -145,8 +156,17 @@ private function castString($value): string return (string) $value; } + private function castInt($value): int + { + return (int) $value; + } + private function castBool($value): bool { + if (is_bool($value)) { + return $value; + } + return $value === 'true' || $value === '1'; } } diff --git a/src/etc/imports.xsd b/src/etc/imports.xsd index c07cf6f..ca58d9b 100644 --- a/src/etc/imports.xsd +++ b/src/etc/imports.xsd @@ -16,7 +16,7 @@ - + @@ -53,7 +53,7 @@ - + @@ -86,7 +86,7 @@ - +