addDependency('config'); $this->addDependency('dataManager'); } public function get() : object { return (object) ($this->getInjection('config')->get('currencyRates') ?? []); } public function set(object $rates) : object { $config = $this->getInjection('config'); $currencyList = $config->get('currencyList') ?? []; foreach (get_object_vars($rates) as $key => $value) { if (!is_string($key) || !in_array($key, $currencyList)) { unset($rates->$key); continue; } if (!is_numeric($value) || is_string($value)) throw new BadRequest(); if ($value < 0) throw new BadRequest(); } foreach ($currencyList as $currency) { if ($currency == $config->get('baseCurrency')) continue; if (!isset($rates->$currency)) { $rates->$currency = $config->get('currencyRates.' . $currency) ?? 1.0; } } $data = (object) ['currencyRates' => $rates]; $config->setData($data); $config->save(); $this->getInjection('dataManager')->rebuildDatabase([]); return (object) ($config->get('currencyRates') ?? []); } }