diff --git a/CHANGELOG.md b/CHANGELOG.md index ab3b30c..2be7f5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/os2forms_sync.services.yml b/os2forms_sync.services.yml index a83d60a..c618e39 100644 --- a/os2forms_sync.services.yml +++ b/os2forms_sync.services.yml @@ -1,7 +1,7 @@ services: Drupal\os2forms_sync\Helper\Settings: arguments: - - '@state' + - '@keyvalue' Drupal\os2forms_sync\Helper\WebformHelper: arguments: diff --git a/src/Helper/Settings.php b/src/Helper/Settings.php index 828aabb..400a634 100644 --- a/src/Helper/Settings.php +++ b/src/Helper/Settings.php @@ -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; @@ -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); } /** @@ -70,12 +71,11 @@ 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 * @@ -83,7 +83,9 @@ private function get(string $key, $default = NULL) { */ 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; }