Skip to content

Commit 51528ae

Browse files
authored
Merge pull request #2 from itk-dev/feature/keyvalue
Used keyvalue for settings storage
2 parents c2d627f + 4823171 commit 51528ae

3 files changed

Lines changed: 16 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ Versioning](https://semver.org/spec/v2.0.0.html).
1212
### Added
1313

1414
- Added OS2Forms sync module
15+
- Used keyvalue for settings storage
1516

1617
[Unreleased]: https://github.com/itk-dev/os2forms_sync/compare/main...HEAD

os2forms_sync.services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
services:
22
Drupal\os2forms_sync\Helper\Settings:
33
arguments:
4-
- '@state'
4+
- '@keyvalue'
55

66
Drupal\os2forms_sync\Helper\WebformHelper:
77
arguments:

src/Helper/Settings.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace Drupal\os2forms_sync\Helper;
44

5-
use Drupal\Core\State\StateInterface;
5+
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
6+
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
67
use Drupal\os2forms_sync\Exception\InvalidSettingException;
78
use Symfony\Component\OptionsResolver\OptionsResolver;
89

@@ -11,24 +12,24 @@
1112
*/
1213
final class Settings {
1314
/**
14-
* The state.
15+
* The store.
1516
*
16-
* @var \Drupal\Core\State\StateInterface
17+
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
1718
*/
18-
private StateInterface $state;
19+
private KeyValueStoreInterface $store;
1920

2021
/**
21-
* The key prefix.
22+
* The key value collection name.
2223
*
2324
* @var string
2425
*/
25-
private $stateKey = 'os2forms_sync';
26+
private $collection = 'os2forms_sync';
2627

2728
/**
2829
* Constructor.
2930
*/
30-
public function __construct(StateInterface $state) {
31-
$this->state = $state;
31+
public function __construct(KeyValueFactoryInterface $keyValueFactory) {
32+
$this->store = $keyValueFactory->get($this->collection);
3233
}
3334

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

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

7777
/**
78-
* Set setting.
78+
* Set settings.
7979
*
8080
* @throws \Symfony\Component\OptionsResolver\Exception\ExceptionInterface
8181
*
8282
* @phpstan-param array<string, mixed> $settings
8383
*/
8484
public function setSettings(array $settings): self {
8585
$settings = $this->getSettingsResolver()->resolve($settings);
86-
$this->state->set($this->stateKey, $settings);
86+
foreach ($settings as $key => $value) {
87+
$this->store->set($key, $value);
88+
}
8789

8890
return $this;
8991
}

0 commit comments

Comments
 (0)