config = $config; $this->configWriter = $configWriter; $this->dataManager = $dataManager; $this->acl = $acl; } /** * @throws Forbidden */ public function get(): stdClass { if (!$this->acl->check('Currency')) { throw new Forbidden(); } if ($this->acl->getLevel('Currency', 'read') !== 'yes') { throw new Forbidden(); } return (object) ( $this->config->get('currencyRates') ?? [] ); } /** * @throws BadRequest * @throws Forbidden * @throws Error */ public function set(stdClass $rates): stdClass { if (!$this->acl->check('Currency')) { throw new Forbidden(); } if ($this->acl->getLevel('Currency', 'edit') !== 'yes') { throw new Forbidden(); } $config = $this->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; } } $this->configWriter->set('currencyRates', $rates); $this->configWriter->save(); $this->dataManager->rebuildDatabase([]); return (object) ( $config->get('currencyRates') ?? [] ); } }