Skip to content

Commit

Permalink
✨ Added possibility to delete configs (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLambauer authored Oct 13, 2022
1 parent 5c1635f commit 9003888
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
12 changes: 12 additions & 0 deletions Model/Processor/ImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

class ImportProcessor extends AbstractProcessor implements ImportProcessorInterface
{
private const DELETE_CONFIG_FLAG = '!!DELETE';
/**
* @var WriterInterface
*/
Expand Down Expand Up @@ -81,6 +82,17 @@ public function process()
foreach ($configurations as $configPath => $configValues) {
$scopeConfigValues = $this->transformConfigToScopeConfig($configPath, $configValues);
foreach ($scopeConfigValues as $scopeConfigValue) {
if ($scopeConfigValue['value'] === self::DELETE_CONFIG_FLAG) {
$this->configWriter->delete(
$configPath,
$scopeConfigValue['scope'],
$this->scopeConverter->convert($scopeConfigValue['scope_id'], $scopeConfigValue['scope'])
);
$this->getOutput()->writeln(sprintf('<comment>%s => %s</comment>', $configPath, 'DELETED'));
$valuesSet++;
continue;
}

$this->configWriter->save(
$configPath,
$scopeConfigValue['value'],
Expand Down
8 changes: 7 additions & 1 deletion Test/Unit/Model/Processor/ImportProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,21 @@ public function process(): void
0 => 'ABC',
],
],
'test/config/custom_field_to_be_deleted' => [
'default' => [
0 => '!!DELETE',
],
]
];

$readerMock = $this->getMockBuilder(YamlReader::class)
->onlyMethods(['parse'])
->getMock();
$readerMock->expects($this->once())->method('parse')->willReturn($parseResult);

$this->scopeValidatorMock->expects($this->once())->method('validate')->willReturn(true);
$this->scopeValidatorMock->expects($this->exactly(2))->method('validate')->willReturn(true);
$this->configWriterMock->expects($this->once())->method('save');
$this->configWriterMock->expects($this->once())->method('delete');

$processor = new ImportProcessor($this->configWriterMock, $this->scopeValidatorMock, $this->scopeConverterMock);
$processor->setOutput($this->outputMock);
Expand Down
11 changes: 10 additions & 1 deletion docs/config-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

Import configuration values in Magento in an automated way instead of manually clicking through the store configuration.


## Usage

```bash
Expand Down Expand Up @@ -73,6 +72,16 @@ vendorx/general/api_key:
You can then set the environment variable `VENDORX_API_KEY` in your CI/CD configuration to the secret API key.
### Delete Config
Sometimes, it might be helpful to be able to delete certain config values and get back to the default behavior. To do so, your config value has to be a magic-ish string.
```yaml
vendorx/general/api_key:
default:
0: "!!DELETE"
```
### Recursive folder setup
If you choose to store your configuration files in subdirectories, e.g. per vendor, the recommended folder setup should look like this:
Expand Down

0 comments on commit 9003888

Please sign in to comment.