Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable default options of import types #57

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Archiver/CsvArchiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
freezy-sk marked this conversation as resolved.
Show resolved Hide resolved
{
return sprintf(
'%s-%s.%s',
$file->getBasename('.' . $file->getExtension()),
$this->getDateTime()->format('dmYhis'),
$this->getDateTime()->format($this->config->get('archive_date_format')),
$file->getExtension()
);
}
Expand Down
30 changes: 30 additions & 0 deletions src/Config/AppConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Jh\Import\Config;

use Magento\Framework\App\Config\ScopeConfigInterface;

class AppConfigProvider
{
private const CONFIG_PATH_PREFIX = 'jh_import/default';

protected ScopeConfigInterface $scopeConfig;

public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->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;
}
}
36 changes: 26 additions & 10 deletions src/Config/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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' => 0777],
bartoszkubicki marked this conversation as resolved.
Show resolved Hide resolved
],
'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'],
Expand All @@ -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'],
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -145,6 +156,11 @@ private function castString($value): string
return (string) $value;
}

private function castInt($value): int
{
return (int) $value;
}

private function castBool($value): bool
{
return $value === 'true' || $value === '1';
Expand Down
6 changes: 3 additions & 3 deletions src/etc/imports.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</xs:documentation>
</xs:annotation>
<xs:all>
<xs:element name="source" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="source" minOccurs="0" maxOccurs="1" type="xs:string"/>
<xs:element name="incoming_directory" minOccurs="0" maxOccurs="1" type="xs:string"/>
<xs:element name="archived_directory" minOccurs="0" maxOccurs="1" type="xs:string"/>
<xs:element name="failed_directory" minOccurs="0" maxOccurs="1" type="xs:string"/>
Expand Down Expand Up @@ -53,7 +53,7 @@
</xs:annotation>
<xs:all>
<xs:element name="connection_name" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="source" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="source" minOccurs="0" maxOccurs="1" type="xs:string"/>
<xs:element name="specification" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="writer" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="id_field" minOccurs="1" maxOccurs="1" type="xs:string"/>
Expand Down Expand Up @@ -86,7 +86,7 @@
</xs:documentation>
</xs:annotation>
<xs:all>
<xs:element name="source" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="source" minOccurs="0" maxOccurs="1" type="xs:string"/>
<xs:element name="source_id" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="specification" minOccurs="1" maxOccurs="1" type="xs:string"/>
<xs:element name="writer" minOccurs="1" maxOccurs="1" type="xs:string"/>
Expand Down