|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SilverShop\Currency; |
| 6 | + |
| 7 | +use SilverShop\Extension\ShopConfigExtension; |
| 8 | +use SilverShop\ShopTools; |
| 9 | +use SilverStripe\Core\Config\Configurable; |
| 10 | +use SilverStripe\Core\Injector\Injectable; |
| 11 | + |
| 12 | +/** |
| 13 | + * Simple currency service that uses statically configured exchange rates. |
| 14 | + * |
| 15 | + * Exchange rates are defined relative to the shop's base currency. |
| 16 | + * For example, if the base currency is "NZD" and the rate for "USD" is 0.65, |
| 17 | + * then 1 NZD = 0.65 USD. |
| 18 | + * |
| 19 | + * Configuration example (YAML): |
| 20 | + * <code> |
| 21 | + * SilverShop\Currency\SimpleCurrencyService: |
| 22 | + * exchange_rates: |
| 23 | + * USD: 0.65 |
| 24 | + * EUR: 0.58 |
| 25 | + * GBP: 0.50 |
| 26 | + * </code> |
| 27 | + */ |
| 28 | +class SimpleCurrencyService implements CurrencyService |
| 29 | +{ |
| 30 | + use Injectable; |
| 31 | + use Configurable; |
| 32 | + |
| 33 | + /** |
| 34 | + * Session key for storing the active currency |
| 35 | + */ |
| 36 | + private static string $session_key = 'SilverShop.activeCurrency'; |
| 37 | + |
| 38 | + /** |
| 39 | + * Exchange rates relative to the shop base currency. |
| 40 | + * Keys are currency codes, values are the exchange rate (base → foreign). |
| 41 | + * |
| 42 | + * @var array<string, float> |
| 43 | + */ |
| 44 | + private static array $exchange_rates = []; |
| 45 | + |
| 46 | + public function convert(float $price, string $from, string $to): float |
| 47 | + { |
| 48 | + if ($from === $to) { |
| 49 | + return $price; |
| 50 | + } |
| 51 | + |
| 52 | + $rate = $this->getExchangeRate($from, $to); |
| 53 | + return $price * $rate; |
| 54 | + } |
| 55 | + |
| 56 | + public function getActiveCurrency(): string |
| 57 | + { |
| 58 | + $session = ShopTools::getSession(); |
| 59 | + $currency = $session->get(self::config()->session_key); |
| 60 | + |
| 61 | + if ($currency) { |
| 62 | + return (string)$currency; |
| 63 | + } |
| 64 | + |
| 65 | + return ShopConfigExtension::get_site_currency(); |
| 66 | + } |
| 67 | + |
| 68 | + public function setActiveCurrency(string $currency): void |
| 69 | + { |
| 70 | + $session = ShopTools::getSession(); |
| 71 | + $session->set(self::config()->session_key, $currency); |
| 72 | + } |
| 73 | + |
| 74 | + public function getExchangeRate(string $from, string $to): float |
| 75 | + { |
| 76 | + if ($from === $to) { |
| 77 | + return 1.0; |
| 78 | + } |
| 79 | + |
| 80 | + $baseCurrency = ShopConfigExtension::get_site_currency(); |
| 81 | + $rates = self::config()->exchange_rates; |
| 82 | + |
| 83 | + // Build rate from $from to $to using the base currency as pivot |
| 84 | + $fromRate = $this->getRateToBase($from, $baseCurrency, $rates); |
| 85 | + $toRate = $this->getRateFromBase($to, $baseCurrency, $rates); |
| 86 | + |
| 87 | + return $fromRate * $toRate; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get the rate to convert from $currency to the base currency. |
| 92 | + * (i.e. how many base-currency units equal 1 unit of $currency) |
| 93 | + * |
| 94 | + * @param string $currency |
| 95 | + * @param string $baseCurrency |
| 96 | + * @param array<string, float> $rates |
| 97 | + */ |
| 98 | + private function getRateToBase(string $currency, string $baseCurrency, array $rates): float |
| 99 | + { |
| 100 | + if ($currency === $baseCurrency) { |
| 101 | + return 1.0; |
| 102 | + } |
| 103 | + |
| 104 | + if (isset($rates[$currency]) && $rates[$currency] > 0) { |
| 105 | + // rates[$currency] = base→foreign, so foreign→base = 1/$rates[$currency] |
| 106 | + return 1.0 / (float)$rates[$currency]; |
| 107 | + } |
| 108 | + |
| 109 | + return 1.0; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Get the rate to convert from the base currency to $currency. |
| 114 | + * (i.e. how many units of $currency equal 1 base-currency unit) |
| 115 | + * |
| 116 | + * @param string $currency |
| 117 | + * @param string $baseCurrency |
| 118 | + * @param array<string, float> $rates |
| 119 | + */ |
| 120 | + private function getRateFromBase(string $currency, string $baseCurrency, array $rates): float |
| 121 | + { |
| 122 | + if ($currency === $baseCurrency) { |
| 123 | + return 1.0; |
| 124 | + } |
| 125 | + |
| 126 | + if (isset($rates[$currency])) { |
| 127 | + return (float)$rates[$currency]; |
| 128 | + } |
| 129 | + |
| 130 | + return 1.0; |
| 131 | + } |
| 132 | +} |
0 commit comments