You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpdateCurrencyRates.php 806B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Listeners;
  3. use App\Events\DefaultCurrencyChanged;
  4. use App\Models\Setting\Currency;
  5. use App\Services\CurrencyService;
  6. class UpdateCurrencyRates
  7. {
  8. /**
  9. * Create the event listener.
  10. */
  11. public function __construct()
  12. {
  13. //
  14. }
  15. /**
  16. * Handle the event.
  17. */
  18. public function handle(DefaultCurrencyChanged $event): void
  19. {
  20. $currencyService = app(CurrencyService::class);
  21. $currencies = Currency::where('code', '!=', $event->currency->code)->get();
  22. foreach ($currencies as $currency) {
  23. $newRate = $currencyService->getCachedExchangeRate($event->currency->code, $currency->code);
  24. if ($newRate !== null) {
  25. $currency->update(['rate' => $newRate]);
  26. }
  27. }
  28. }
  29. }