|
2 | 2 |
|
3 | 3 | namespace Drupal\os2forms_sync\Helper; |
4 | 4 |
|
5 | | -use Drupal\Core\State\StateInterface; |
| 5 | +use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; |
| 6 | +use Drupal\Core\KeyValueStore\KeyValueStoreInterface; |
6 | 7 | use Drupal\os2forms_sync\Exception\InvalidSettingException; |
7 | 8 | use Symfony\Component\OptionsResolver\OptionsResolver; |
8 | 9 |
|
|
11 | 12 | */ |
12 | 13 | final class Settings { |
13 | 14 | /** |
14 | | - * The state. |
| 15 | + * The store. |
15 | 16 | * |
16 | | - * @var \Drupal\Core\State\StateInterface |
| 17 | + * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface |
17 | 18 | */ |
18 | | - private StateInterface $state; |
| 19 | + private KeyValueStoreInterface $store; |
19 | 20 |
|
20 | 21 | /** |
21 | | - * The key prefix. |
| 22 | + * The key value collection name. |
22 | 23 | * |
23 | 24 | * @var string |
24 | 25 | */ |
25 | | - private $stateKey = 'os2forms_sync'; |
| 26 | + private $collection = 'os2forms_sync'; |
26 | 27 |
|
27 | 28 | /** |
28 | 29 | * Constructor. |
29 | 30 | */ |
30 | | - public function __construct(StateInterface $state) { |
31 | | - $this->state = $state; |
| 31 | + public function __construct(KeyValueFactoryInterface $keyValueFactory) { |
| 32 | + $this->store = $keyValueFactory->get($this->collection); |
32 | 33 | } |
33 | 34 |
|
34 | 35 | /** |
@@ -70,20 +71,21 @@ private function get(string $key, $default = NULL) { |
70 | 71 | throw new InvalidSettingException(sprintf('Setting %s is not defined', $key)); |
71 | 72 | } |
72 | 73 |
|
73 | | - $settings = $this->state->get($this->stateKey); |
74 | | - return $settings[$key] ?? $default; |
| 74 | + return $this->store->get($key, $default); |
75 | 75 | } |
76 | 76 |
|
77 | 77 | /** |
78 | | - * Set setting. |
| 78 | + * Set settings. |
79 | 79 | * |
80 | 80 | * @throws \Symfony\Component\OptionsResolver\Exception\ExceptionInterface |
81 | 81 | * |
82 | 82 | * @phpstan-param array<string, mixed> $settings |
83 | 83 | */ |
84 | 84 | public function setSettings(array $settings): self { |
85 | 85 | $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 | + } |
87 | 89 |
|
88 | 90 | return $this; |
89 | 91 | } |
|
0 commit comments