src/Twig/CurrencyExtension.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\Entity\Gos\Country;
  4. use App\Entity\Gos\CountrySettings;
  5. use App\Utils\Region\CountryService;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\Security\Core\Security;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Twig\Extension\AbstractExtension;
  12. use Twig\TwigFilter;
  13. class CurrencyExtension extends AbstractExtension
  14. {
  15.     private EntityManagerInterface $em;
  16.     private ?Request $request;
  17.     private ?UserInterface $user;
  18.     private CountryService $countryService;
  19.     public function __construct(
  20.         EntityManagerInterface $em,
  21.         RequestStack $requestStack,
  22.         CountryService $countryService,
  23.         Security $security
  24.     ) {
  25.         $this->em       $em;
  26.         $this->request  $requestStack->getCurrentRequest();
  27.         $this->user     $security->getUser();
  28.         $this->countryService $countryService;
  29.     }
  30.     public function getFilters(): array
  31.     {
  32.         return array(
  33.             new TwigFilter('addCurrency', array($this'addCurrency'))
  34.         );
  35.     }
  36.     public function addCurrency(string $priceCountry $countryParam null): string
  37.     {
  38.         if (!isset($countryParam))
  39.             $country $this->countryService->getCountry($this->request$this->user);
  40.         else
  41.             $country $countryParam;
  42.         $currency "$";
  43.         $locale 'pl';
  44.         try {
  45.             $locale $this->request->getSession()->get('userLocale''pl');
  46.         }catch (\Exception $e){}
  47.         if (in_array(isset($countryParam) ? strtolower($countryParam->getAlpha2()) : $locale, ['pl''en''de''ru']))
  48.         {
  49.             $currency $this->getCurrencyBasedOnLocale(isset($countryParam) ? strtolower($countryParam->getAlpha2()) : $locale);
  50.         }
  51.         elseif ($country)
  52.         {
  53.             $countrySettings $this->em->getRepository(CountrySettings::class)->findOneByCountry($country);
  54.             if ($countrySettings)
  55.             {
  56.                 if ($countrySettings->getSignAfterPrice())
  57.                     return $price ' ' $countrySettings->getSignAfterPrice();
  58.                 else
  59.                     return $countrySettings->getSignBeforePrice() . $price;
  60.             }
  61.         }
  62.         return $price ' ' $currency;
  63.     }
  64.     private function getCurrencyBasedOnLocale(string $country): string
  65.     {
  66.         switch ($country)
  67.         {
  68.             case 'pl':
  69.                 return 'zł';
  70.             case 'ru':
  71.                 return '₽';
  72.             case 'en':
  73.                 return '£';
  74.             case 'de':
  75.             default:
  76.                 return '€';
  77.         }
  78.     }
  79. }