core/lib/Thelia/Action/Currency.php line 175

  1. <?php
  2. /*
  3.  * This file is part of the Thelia package.
  4.  * http://www.thelia.net
  5.  *
  6.  * (c) OpenStudio <info@thelia.net>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Thelia\Action;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Thelia\Core\Event\Currency\CurrencyCreateEvent;
  15. use Thelia\Core\Event\Currency\CurrencyDeleteEvent;
  16. use Thelia\Core\Event\Currency\CurrencyUpdateEvent;
  17. use Thelia\Core\Event\Currency\CurrencyUpdateRateEvent;
  18. use Thelia\Core\Event\TheliaEvents;
  19. use Thelia\Core\Event\UpdatePositionEvent;
  20. use Thelia\Core\Translation\Translator;
  21. use Thelia\CurrencyConverter\CurrencyConverter;
  22. use Thelia\CurrencyConverter\Exception\CurrencyNotFoundException;
  23. use Thelia\Log\Tlog;
  24. use Thelia\Math\Number;
  25. use Thelia\Model\Currency as CurrencyModel;
  26. use Thelia\Model\CurrencyQuery;
  27. class Currency extends BaseAction implements EventSubscriberInterface
  28. {
  29.     /** @var CurrencyConverter */
  30.     protected $currencyConverter;
  31.     public function __construct(CurrencyConverter $currencyConverter)
  32.     {
  33.         $this->currencyConverter $currencyConverter;
  34.     }
  35.     /**
  36.      * Create a new currencyuration entry.
  37.      */
  38.     public function create(CurrencyCreateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  39.     {
  40.         $currency = new CurrencyModel();
  41.         $isDefault CurrencyQuery::create()->count() === 0;
  42.         $currency
  43.             ->setLocale($event->getLocale())
  44.             ->setName($event->getCurrencyName())
  45.             ->setSymbol($event->getSymbol())
  46.             ->setFormat($event->getFormat())
  47.             ->setRate($event->getRate())
  48.             ->setCode(strtoupper($event->getCode()))
  49.             ->setByDefault($isDefault)
  50.             ->save()
  51.         ;
  52.         $event->setCurrency($currency);
  53.     }
  54.     /**
  55.      * Change a currency.
  56.      */
  57.     public function update(CurrencyUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  58.     {
  59.         if (null !== $currency CurrencyQuery::create()->findPk($event->getCurrencyId())) {
  60.             $currency
  61.                 ->setLocale($event->getLocale())
  62.                 ->setName($event->getCurrencyName())
  63.                 ->setSymbol($event->getSymbol())
  64.                 ->setFormat($event->getFormat())
  65.                 ->setRate($event->getRate())
  66.                 ->setCode(strtoupper($event->getCode()))
  67.                 ->save();
  68.             $event->setCurrency($currency);
  69.         }
  70.     }
  71.     /**
  72.      * Set the default currency.
  73.      */
  74.     public function setDefault(CurrencyUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  75.     {
  76.         if (null !== $currency CurrencyQuery::create()->findPk($event->getCurrencyId())) {
  77.             // Reset default status
  78.             CurrencyQuery::create()->filterByByDefault(true)->update(['ByDefault' => false]);
  79.             $currency
  80.                 ->setVisible($event->getVisible())
  81.                 ->setByDefault($event->getIsDefault())
  82.                 ->save()
  83.             ;
  84.             // Update rates when setting a new default currency
  85.             if ($event->getIsDefault()) {
  86.                 $updateRateEvent = new CurrencyUpdateRateEvent();
  87.                 $dispatcher->dispatch($updateRateEventTheliaEvents::CURRENCY_UPDATE_RATES);
  88.             }
  89.             $event->setCurrency($currency);
  90.         }
  91.     }
  92.     public function setVisible(CurrencyUpdateEvent $event): void
  93.     {
  94.         if (null !== $currency CurrencyQuery::create()->findPk($event->getCurrencyId())) {
  95.             if (!$currency->getByDefault()) {
  96.                 $currency->setVisible($event->getVisible())->save();
  97.             }
  98.         }
  99.     }
  100.     /**
  101.      * Delete a currencyuration entry.
  102.      */
  103.     public function delete(CurrencyDeleteEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  104.     {
  105.         if (null !== ($currency CurrencyQuery::create()->findPk($event->getCurrencyId()))) {
  106.             if ($currency->getByDefault()) {
  107.                 throw new \RuntimeException(
  108.                     Translator::getInstance()->trans('It is not allowed to delete the default currency')
  109.                 );
  110.             }
  111.             $currency
  112.                 ->delete()
  113.             ;
  114.             $event->setCurrency($currency);
  115.         }
  116.     }
  117.     public function updateRates(CurrencyUpdateRateEvent $event): void
  118.     {
  119.         if (null === $defaultCurrency CurrencyQuery::create()->findOneByByDefault(true)) {
  120.             throw new \RuntimeException('Unable to find a default currency, please define a default currency.');
  121.         }
  122.         $defaultCurrency->setRate(1)->save();
  123.         $currencies CurrencyQuery::create()->filterByByDefault(false);
  124.         $baseValue = new Number('1');
  125.         /** @var \Thelia\Model\Currency $currency */
  126.         foreach ($currencies as $currency) {
  127.             try {
  128.                 $rate $this->currencyConverter
  129.                     ->from($defaultCurrency->getCode())
  130.                     ->to($currency->getCode())
  131.                     ->convert($baseValue);
  132.                 $currency->setRate($rate->getNumber(-1))->save();
  133.             } catch (CurrencyNotFoundException $ex) {
  134.                 Tlog::getInstance()->addError(
  135.                     sprintf('Unable to find exchange rate for currency %s, ID %d'$currency->getCode(), $currency->getId())
  136.                 );
  137.                 $event->addUndefinedRate($currency->getId());
  138.             }
  139.         }
  140.     }
  141.     /**
  142.      * Changes position, selecting absolute ou relative change.
  143.      */
  144.     public function updatePosition(UpdatePositionEvent $eventstring $eventNameEventDispatcherInterface $dispatcher): void
  145.     {
  146.         $this->genericUpdatePosition(CurrencyQuery::create(), $event$dispatcher);
  147.     }
  148.     /**
  149.      * {@inheritDoc}
  150.      */
  151.     public static function getSubscribedEvents()
  152.     {
  153.         return [
  154.             TheliaEvents::CURRENCY_CREATE => ['create'128],
  155.             TheliaEvents::CURRENCY_UPDATE => ['update'128],
  156.             TheliaEvents::CURRENCY_DELETE => ['delete'128],
  157.             TheliaEvents::CURRENCY_SET_DEFAULT => ['setDefault'128],
  158.             TheliaEvents::CURRENCY_SET_VISIBLE => ['setVisible'128],
  159.             TheliaEvents::CURRENCY_UPDATE_RATES => ['updateRates'128],
  160.             TheliaEvents::CURRENCY_UPDATE_POSITION => ['updatePosition'128],
  161.         ];
  162.     }
  163. }