src/Service/CurrencyService.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Repository\CurrencyExchangerateRepository;
  4. use Exception;
  5. use Sherlockode\ConfigurationBundle\Manager\ParameterManagerInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. class CurrencyService
  9. {
  10.     public function __construct(
  11.         private readonly CurrencyExchangerateRepository $currencyExchangerateRepository,
  12.         private readonly ParameterManagerInterface      $parameterManager,
  13.         private readonly ParameterService               $parameterService,
  14.         private readonly RequestStack                   $requestStack
  15.     ){}
  16.     public function getCurrencies(): array
  17.     {
  18.         $currencies = [];
  19.         $parameters $this->parameterManager->getAll();
  20.         foreach ($parameters as $key => $value) {
  21.             if (str_starts_with($key'Currency_')){
  22.                 $currencies [$value] = $value;
  23.             }
  24.         }
  25.         return $currencies;
  26.     }
  27.     public function getSessionCurrency() : string{
  28.         $session $this->requestStack->getSession();
  29.         return $session->get('userCurrency') ?? $this->parameterService->getReferenceCurrency();
  30.     }
  31.     /**
  32.      * @throws Exception
  33.      */
  34.     public function convert($amount$currencyFrom$currencyTo) : ?float
  35.     {
  36.         $exchangeRate $this->calculateExchangeRate($currencyFrom$currencyTo);
  37.         return ceil($amount $exchangeRate);
  38.     }
  39.     /**
  40.      * @throws Exception
  41.      */
  42.     public function calculateExchangeRate($currencyFrom$currencyTo) : float
  43.     {
  44.         if($currencyFrom == $currencyTo){
  45.             return 1.0;
  46.         }else
  47.         {
  48.             $currencyExchange $this->currencyExchangerateRepository->findOneBy(
  49.                 [
  50.                     'currencyFrom' => $currencyFrom,
  51.                     'currencyTo' => $currencyTo
  52.                 ]
  53.             );
  54.             if($currencyExchange!=null){
  55.                 return $currencyExchange->getExchangeRate();
  56.             }else{
  57.                 throw new Exception("Currency ExchangeRate Exception");
  58.                 return 1;
  59.             }
  60.         }
  61.     }
  62.     public function  getCurrenciesFromExchange(): array
  63.     {
  64.         $exchangeCurrencies $this->currencyExchangerateRepository->findAll();
  65.         $currenciesToValues array_map(function($currency) {
  66.             return $currency->getCurrencyTo();
  67.         }, $exchangeCurrencies);
  68.         $currencyArrayWithValues = [];
  69.         foreach ($currenciesToValues as $currency) {
  70.             $currencyArrayWithValues[$currency] = $currency;
  71.         }
  72.         return $currencyArrayWithValues;
  73.     }
  74.     public function getSaleCurrency(?UserInterface $user) : ?string
  75.     {
  76.         $customer = ($user?$user->getCustomer() : null);
  77.         if(is_null($user) || is_null($customer)){
  78. //            return $this->getSessionCurrency();
  79.             return $this->parameterService->getReferenceCurrency();
  80.         }
  81.         return $customer->getCurrency();
  82.     }
  83.     public function getISO4217Code(?string $currency) : ?int // 3 characters
  84.     {
  85.         return match ($currency) {
  86.             'TND' => 788,
  87.             'EUR' => 200,
  88.             'USD' => 100,
  89.             default => null,
  90.         };
  91.     }
  92. }