Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ Versioning](https://semver.org/spec/v2.0.0.html).
### Added

- Added OS2Forms sync module
- Used keyvalue for settings storage

[Unreleased]: https://github.com/itk-dev/os2forms_sync/compare/main...HEAD
2 changes: 1 addition & 1 deletion os2forms_sync.services.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
Drupal\os2forms_sync\Helper\Settings:
arguments:
- '@state'
- '@keyvalue'

Drupal\os2forms_sync\Helper\WebformHelper:
arguments:
Expand Down
26 changes: 14 additions & 12 deletions src/Helper/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Drupal\os2forms_sync\Helper;

use Drupal\Core\State\StateInterface;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
use Drupal\os2forms_sync\Exception\InvalidSettingException;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -11,24 +12,24 @@
*/
final class Settings {
/**
* The state.
* The store.
*
* @var \Drupal\Core\State\StateInterface
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
*/
private StateInterface $state;
private KeyValueStoreInterface $store;

/**
* The key prefix.
* The key value collection name.
*
* @var string
*/
private $stateKey = 'os2forms_sync';
private $collection = 'os2forms_sync';

/**
* Constructor.
*/
public function __construct(StateInterface $state) {
$this->state = $state;
public function __construct(KeyValueFactoryInterface $keyValueFactory) {
$this->store = $keyValueFactory->get($this->collection);
}

/**
Expand Down Expand Up @@ -70,20 +71,21 @@ private function get(string $key, $default = NULL) {
throw new InvalidSettingException(sprintf('Setting %s is not defined', $key));
}

$settings = $this->state->get($this->stateKey);
return $settings[$key] ?? $default;
return $this->store->get($key, $default);
}

/**
* Set setting.
* Set settings.
*
* @throws \Symfony\Component\OptionsResolver\Exception\ExceptionInterface
*
* @phpstan-param array<string, mixed> $settings
*/
public function setSettings(array $settings): self {
$settings = $this->getSettingsResolver()->resolve($settings);
$this->state->set($this->stateKey, $settings);
foreach ($settings as $key => $value) {
$this->store->set($key, $value);
}

return $this;
}
Expand Down