← All changes
|
app/Features/UserSettings/Storage/RemoteStorage.php
+103
-0
2.0.1
→
2.1.1
View file →
| @@ -1,0 +1,103 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace Metricool\Features\UserSettings\Storage; | |
| 6 | + | |
| 7 | +use Metricool\Bootstrap\App; | |
| 8 | +use Metricool\Http\Metricool\MetricoolApi; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * This storage uses a client to store and retrieve the UserSettings | |
| 12 | + */ | |
| 13 | +class RemoteStorage extends AbstractStorage | |
| 14 | +{ | |
| 15 | + protected object $client; | |
| 16 | + protected string $method; | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * This property is used to avoid multiple requests to the remote client. | |
| 20 | + * It stores the settings retrieved from the client and any changes made | |
| 21 | + * to them before submitting. | |
| 22 | + */ | |
| 23 | + protected array $settings = []; | |
| 24 | + | |
| 25 | + public function __construct(string $name, array $config) | |
| 26 | + { | |
| 27 | + parent::__construct($name); | |
| 28 | + | |
| 29 | + $this->client = App::getInstance()->get(MetricoolApi::class)->userSettings(); | |
| 30 | + $this->method = $config['method'] ?? 'post'; | |
| 31 | + $this->casing = $config['casing'] ?? ''; | |
| 32 | + } | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * @inheritDoc | |
| 36 | + */ | |
| 37 | + public function get(string $key) | |
| 38 | + { | |
| 39 | + if (!empty($this->settings)) { | |
| 40 | + $settingsKey = $this->convertCase($key); | |
| 41 | + return $this->settings[$settingsKey] ?? null; | |
| 42 | + } | |
| 43 | + | |
| 44 | + $value = $this->getMultiple([$key]); | |
| 45 | + return $value[$key]; | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * @inheritDoc | |
| 50 | + */ | |
| 51 | + public function getMultiple(array $keys): array | |
| 52 | + { | |
| 53 | + $data = []; | |
| 54 | + | |
| 55 | + // Retrieve all values from the client for the first time | |
| 56 | + if (empty($this->settings)) { | |
| 57 | + $this->settings = $this->client->get(); | |
| 58 | + } | |
| 59 | + | |
| 60 | + // Retrieve the requested values from the response | |
| 61 | + foreach ($keys as $key) { | |
| 62 | + $data[$key] = $this->settings[$this->convertCase($key)] ?? null; | |
| 63 | + } | |
| 64 | + | |
| 65 | + // Return the requested values | |
| 66 | + return $data; | |
| 67 | + } | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * @inheritDoc | |
| 71 | + */ | |
| 72 | + public function store(string $key, $value): void | |
| 73 | + { | |
| 74 | + $this->storeMultiple([$key => $value]); | |
| 75 | + } | |
| 76 | + | |
| 77 | + /** | |
| 78 | + * @inheritDoc | |
| 79 | + */ | |
| 80 | + public function storeMultiple(array $settings): void | |
| 81 | + { | |
| 82 | + // Create the request data | |
| 83 | + $requestData = []; | |
| 84 | + foreach ($settings as $key => $value) { | |
| 85 | + $requestData[$this->convertCase($key)] = $value; | |
| 86 | + } | |
| 87 | + | |
| 88 | + // Send the request to the client | |
| 89 | + $this->client->{$this->method}($requestData); | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * @inheritDoc | |
| 94 | + */ | |
| 95 | + public function save(): void | |
| 96 | + { | |
| 97 | + if (empty($this->settings)) { | |
| 98 | + return; | |
| 99 | + } | |
| 100 | + | |
| 101 | + $this->client->{$this->method}($this->settings); | |
| 102 | + } | |
| 103 | +} | |