123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Directory\Model;
- use Magento\Framework\App\ScopeInterface;
- use Magento\Store\Model\StoreManagerInterface;
- use Psr\Log\LoggerInterface as Logger;
- use Magento\Store\Model\Store;
- /**
- * Class PriceCurrency model for convert and format price value
- */
- class PriceCurrency implements \Magento\Framework\Pricing\PriceCurrencyInterface
- {
- /**
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $storeManager;
- /**
- * @var CurrencyFactory
- */
- protected $currencyFactory;
- /**
- * @var Logger
- */
- protected $logger;
- /**
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param CurrencyFactory $currencyFactory
- * @param Logger $logger
- */
- public function __construct(
- StoreManagerInterface $storeManager,
- CurrencyFactory $currencyFactory,
- Logger $logger
- ) {
- $this->storeManager = $storeManager;
- $this->currencyFactory = $currencyFactory;
- $this->logger = $logger;
- }
- /**
- * @inheritdoc
- */
- public function convert($amount, $scope = null, $currency = null)
- {
- $currentCurrency = $this->getCurrency($scope, $currency);
- return $this->getStore($scope)
- ->getBaseCurrency()
- ->convert($amount, $currentCurrency);
- }
- /**
- * @inheritdoc
- */
- public function convertAndRound($amount, $scope = null, $currency = null, $precision = self::DEFAULT_PRECISION)
- {
- return $this->roundPrice($this->convert($amount, $scope, $currency), $precision);
- }
- /**
- * @inheritdoc
- */
- public function format(
- $amount,
- $includeContainer = true,
- $precision = self::DEFAULT_PRECISION,
- $scope = null,
- $currency = null
- ) {
- return $this->getCurrency($scope, $currency)
- ->formatPrecision($amount, $precision, [], $includeContainer);
- }
- /**
- * @inheritdoc
- */
- public function convertAndFormat(
- $amount,
- $includeContainer = true,
- $precision = self::DEFAULT_PRECISION,
- $scope = null,
- $currency = null
- ) {
- $amount = $this->convert($amount, $scope, $currency);
- return $this->format($amount, $includeContainer, $precision, $scope, $currency);
- }
- /**
- * @inheritdoc
- */
- public function getCurrency($scope = null, $currency = null)
- {
- if ($currency instanceof Currency) {
- $currentCurrency = $currency;
- } elseif (is_string($currency)) {
- $currency = $this->currencyFactory->create()
- ->load($currency);
- $baseCurrency = $this->getStore($scope)
- ->getBaseCurrency();
- $currentCurrency = $baseCurrency->getRate($currency) ? $currency : $baseCurrency;
- } else {
- $currentCurrency = $this->getStore($scope)
- ->getCurrentCurrency();
- }
- return $currentCurrency;
- }
- /**
- * Get currrency symbol
- *
- * @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope
- * @param \Magento\Framework\Model\AbstractModel|string|null $currency
- * @return string
- */
- public function getCurrencySymbol($scope = null, $currency = null)
- {
- return $this->getCurrency($scope, $currency)->getCurrencySymbol();
- }
- /**
- * Get store model
- *
- * @param null|string|bool|int|ScopeInterface $scope
- * @return Store
- */
- protected function getStore($scope = null)
- {
- try {
- if (!$scope instanceof Store) {
- $scope = $this->storeManager->getStore($scope);
- }
- } catch (\Exception $e) {
- $this->logger->critical($e);
- $scope = $this->storeManager->getStore();
- }
- return $scope;
- }
- /**
- * @inheritdoc
- */
- public function round($price)
- {
- return round($price, 2);
- }
- /**
- * Round price with precision
- *
- * @param float $price
- * @param int $precision
- * @return float
- */
- public function roundPrice($price, $precision = self::DEFAULT_PRECISION)
- {
- return round($price, $precision);
- }
- }
|