Skip to content

30600 GraphQL: list of available currencies #33376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions app/code/Magento/Directory/Api/Data/AvailableCurrencyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Directory\Api\Data;

/**
* Available Currency interface.
*
* @api
* @since 100.0.2
*/
interface AvailableCurrencyInterface extends \Magento\Framework\Api\ExtensibleDataInterface
{
/**
* Get the currency code
*
* @return string
*/
public function getCode();

/**
* Set the currency code
*
* @param string $code
* @return $this
*/
public function setCode($code);

/**
* Get the currency value
*
* @return string
*/
public function getValue();

/**
* Set the currency value
*
* @param string $value
* @return $this
*/
public function setValue($value);

/**
* Get the currency name
*
* @return string
*/
public function getName();

/**
* Set the currency name
*
* @param string $name
* @return $this
*/
public function setName($name);

/**
* Get the currency symbol
*
* @return string
*/
public function getSymbol();

/**
* Set the currency symbol
*
* @param $symbol
* @return $this
*/
public function setSymbol($symbol);
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ public function getAvailableCurrencyCodes();
*/
public function setAvailableCurrencyCodes(array $codes = null);

/**
* Get the list of allowed currencies for the store.
*
* @return \Magento\Directory\Api\Data\AvailableCurrencyInterface[]
*/
public function getAvailableCurrencies();

/**
* Set the list of allowed currencies for the store.
*
* @param \Magento\Directory\Api\Data\AvailableCurrencyInterface[] $availableCurrencies
* @return $this
*/
public function setAvailableCurrencies(array $availableCurrencies = null);

/**
* Get the list of exchange rate information for the store.
*
Expand Down
36 changes: 34 additions & 2 deletions app/code/Magento/Directory/Model/CurrencyInformationAcquirer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,44 @@ class CurrencyInformationAcquirer implements \Magento\Directory\Api\CurrencyInfo
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;


/**
* @var \Magento\Directory\Model\Data\AvailableCurrencyFactory
*/
protected $availableCurrencyFactory;

/**
* @var \Magento\Framework\Locale\CurrencyInterface
*/
protected $localeCurrency;

/**
* @param \Magento\Directory\Model\Data\CurrencyInformationFactory $currencyInformationFactory
* @param \Magento\Directory\Model\Data\ExchangeRateFactory $exchangeRateFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\Locale\CurrencyInterface $localeCurrency
* @param \Magento\Directory\Model\Data\AvailableCurrencyFactory $availableCurrencyFactory
*/
public function __construct(
\Magento\Directory\Model\Data\CurrencyInformationFactory $currencyInformationFactory,
\Magento\Directory\Model\Data\ExchangeRateFactory $exchangeRateFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Locale\CurrencyInterface $localeCurrency,
\Magento\Directory\Model\Data\AvailableCurrencyFactory $availableCurrencyFactory
) {
$this->currencyInformationFactory = $currencyInformationFactory;
$this->exchangeRateFactory = $exchangeRateFactory;
$this->storeManager = $storeManager;
$this->localeCurrency = $localeCurrency;
$this->availableCurrencyFactory = $availableCurrencyFactory;
}

/**
* {@inheritdoc}
*/
public function getCurrencyInfo()
{
/** @var \Magento\Directory\Model\Data\CurrencyInformation $currencyInfo */
$currencyInfo = $this->currencyInformationFactory->create();

/** @var \Magento\Store\Model\Store $store */
Expand All @@ -59,13 +76,28 @@ public function getCurrencyInfo()
$currencyInfo->setAvailableCurrencyCodes($store->getAvailableCurrencyCodes(true));

$exchangeRates = [];
$availableCurrencies = [];
foreach ($store->getAvailableCurrencyCodes(true) as $currencyCode) {
$currency = $this->localeCurrency->getCurrency($currencyCode);

if ($currency instanceof \Magento\Framework\Currency) {
/** @var \Magento\Directory\Model\Data\AvailableCurrency $availableCurrency */
$availableCurrency = $this->availableCurrencyFactory->create();
$availableCurrency->setSymbol($currency->getSymbol());
$availableCurrency->setName($currency->getName());
$availableCurrency->setValue($currency->getValue());
$availableCurrency->setCode($currencyCode);
$availableCurrencies[] = $availableCurrency;
}

/** @var \Magento\Directory\Model\Data\ExchangeRate $exchangeRate */
$exchangeRate = $this->exchangeRateFactory->create();
$exchangeRate->setRate($store->getBaseCurrency()->getRate($currencyCode));
$exchangeRate->setCurrencyTo($currencyCode);
$exchangeRates[] = $exchangeRate;
}
$currencyInfo->setExchangeRates($exchangeRates);
$currencyInfo->setAvailableCurrencies($availableCurrencies);

return $currencyInfo;
}
Expand Down
106 changes: 106 additions & 0 deletions app/code/Magento/Directory/Model/Data/AvailableCurrency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Data Model implementing the Address interface
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Directory\Model\Data;

/**
* Class Available Currency
*
* @codeCoverageIgnore
*/
class AvailableCurrency extends \Magento\Framework\Api\AbstractExtensibleObject implements
\Magento\Directory\Api\Data\AvailableCurrencyInterface
{
const KEY_CODE = 'code';
const KEY_NAME = 'name';
const KEY_VALUE = 'value';
const KEY_SYMBOL = 'symbol';

/**
* @inheritDoc
*/
public function getCode()
{
return $this->_get(self::KEY_CODE);
}

/**
* @inheritDoc
*/
public function setCode($code)
{
return $this->setData(self::KEY_CODE, $code);
}

/**
* @inheritDoc
*/
public function getValue()
{
return $this->_get(self::KEY_VALUE);
}

/**
* @inheritDoc
*/
public function setValue($value)
{
return $this->setData(self::KEY_VALUE, $value);
}

/**
* @inheritDoc
*/
public function getName()
{
return $this->_get(self::KEY_NAME);
}

/**
* @inheritDoc
*/
public function setName($name)
{
return $this->setData(self::KEY_NAME, $name);
}

/**
* @inheritDoc
*/
public function getSymbol()
{
return $this->_get(self::KEY_SYMBOL);
}

/**
* @inheritDoc
*/
public function setSymbol($symbol)
{
return $this->setData(self::KEY_SYMBOL, $symbol);
}

/**
* @inheritDoc
*/
public function getExtensionAttributes()
{
return $this->_getExtensionAttributes();
}

/**
* @inheritDoc
*/
public function setExtensionAttributes(
\Magento\Directory\Api\Data\ExchangeRateExtensionInterface $extensionAttributes
) {
return $this->_setExtensionAttributes($extensionAttributes);
}
}
17 changes: 17 additions & 0 deletions app/code/Magento/Directory/Model/Data/CurrencyInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CurrencyInformation extends \Magento\Framework\Api\AbstractExtensibleObjec
const KEY_DEFAULT_DISPLAY_CURRENCY_CODE = 'default_display_currency_code';
const KEY_DEFAULT_DISPLAY_CURRENCY_SYMBOL = 'default_display_currency_symbol';
const KEY_AVAILABLE_CURRENCY_CODES = 'available_currency_codes';
const KEY_AVAILABLE_CURRENCIES = 'available_currencies';
const KEY_EXCHANGE_RATES = 'exchange_rates';

/**
Expand Down Expand Up @@ -102,6 +103,22 @@ public function setAvailableCurrencyCodes(array $codes = null)
return $this->setData(self::KEY_AVAILABLE_CURRENCY_CODES, $codes);
}

/**
* @inheritDoc
*/
public function getAvailableCurrencies()
{
return $this->_get(self::KEY_AVAILABLE_CURRENCIES);
}

/**
* @inheritDoc
*/
public function setAvailableCurrencies(array $availableCurrencies = null)
{
return $this->setData(self::KEY_AVAILABLE_CURRENCIES, $availableCurrencies);
}

/**
* @inheritDoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

use Magento\Directory\Model\Currency;
use Magento\Directory\Model\CurrencyInformationAcquirer;
use Magento\Directory\Model\Data\AvailableCurrency;
use Magento\Directory\Model\Data\AvailableCurrencyFactory;
use Magento\Directory\Model\Data\CurrencyInformation;
use Magento\Directory\Model\Data\CurrencyInformationFactory;
use Magento\Directory\Model\Data\ExchangeRate;
Expand Down Expand Up @@ -37,6 +39,11 @@ class CurrencyInformationAcquirerTest extends TestCase
*/
protected $exchangeRateFactory;

/**
* @var MockObject
*/
protected $availableCurrencyFactory;

/**
* @var MockObject
*/
Expand Down Expand Up @@ -64,6 +71,11 @@ protected function setUp(): void
->onlyMethods(['create'])
->getMock();

$this->availableCurrencyFactory = $this->getMockBuilder(AvailableCurrencyFactory::class)
->disableOriginalConstructor()
->onlyMethods(['create'])
->getMock();

$this->storeManager = $this->getMockBuilder(StoreManager::class)
->disableOriginalConstructor()
->onlyMethods(['getStore'])
Expand All @@ -74,6 +86,7 @@ protected function setUp(): void
[
'currencyInformationFactory' => $this->currencyInformationFactory,
'exchangeRateFactory' => $this->exchangeRateFactory,
'availableCurrencyFactory' => $this->availableCurrencyFactory,
'storeManager' => $this->storeManager,
]
);
Expand All @@ -93,6 +106,15 @@ public function testGetCurrencyInfo()
$exchangeRate->expects($this->any())->method('load')->willReturnSelf();
$this->exchangeRateFactory->expects($this->any())->method('create')->willReturn($exchangeRate);

/** @var AvailableCurrency $availableCurrency */
$availableCurrency = $this->getMockBuilder(AvailableCurrency::class)
->addMethods(['load'])
->disableOriginalConstructor()
->getMock();

$availableCurrency->expects($this->any())->method('load')->willReturnSelf();
$this->availableCurrencyFactory->expects($this->any())->method('create')->willReturn($availableCurrency);

/** @var CurrencyInformation $currencyInformation */
$currencyInformation = $this->getMockBuilder(CurrencyInformation::class)
->addMethods(['load'])
Expand Down Expand Up @@ -132,5 +154,11 @@ public function testGetCurrencyInfo()
$this->assertEquals([$exchangeRate], $result->getExchangeRates());
$this->assertEquals('0.80', $result->getExchangeRates()[0]->getRate());
$this->assertEquals('AUD', $result->getExchangeRates()[0]->getCurrencyTo());
$this->assertIsArray($result->getAvailableCurrencies());
$this->assertEquals([$availableCurrency], $result->getAvailableCurrencies());
$this->assertEquals('AUD', $result->getAvailableCurrencies()[0]->getCode());
$this->assertEquals('Australian Dollar', $result->getAvailableCurrencies()[0]->getName());
$this->assertEquals('A$', $result->getAvailableCurrencies()[0]->getSymbol());
$this->assertEquals('0', $result->getAvailableCurrencies()[0]->getValue());
}
}
8 changes: 8 additions & 0 deletions app/code/Magento/DirectoryGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ type Currency {
default_display_currecy_symbol: String @deprecated(reason: "Symbol was missed. Use `default_display_currency_symbol`.")
default_display_currency_symbol: String
available_currency_codes: [String]
available_currencies: [CurrencyOutput] @doc(description: "The list of allowed currencies for the store.")
exchange_rates: [ExchangeRate]
}

type CurrencyOutput {
code: String
value: String
name: String
symbol: String
}

type ExchangeRate {
currency_to: String
rate: Float
Expand Down
Loading