Skip to content

Commit 6054320

Browse files
author
Korshenko, Oleksii(okorshenko)
committed
Merge pull request #413 from magento-api/ext-public-pull-requests
[API] Public Pull Requests
2 parents 1f37b76 + cce3115 commit 6054320

File tree

11 files changed

+329
-44
lines changed

11 files changed

+329
-44
lines changed

app/code/Magento/Deploy/Model/Deployer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ protected function emulateApplicationLocale($locale, $area)
285285
$translator = $this->objectManager->get('Magento\Framework\TranslateInterface');
286286
$translator->setLocale($locale);
287287
$translator->loadData($area, true);
288+
/** @var \Magento\Framework\Locale\ResolverInterface $localeResolver */
289+
$localeResolver = $this->objectManager->get('Magento\Framework\Locale\ResolverInterface');
290+
$localeResolver->setLocale($locale);
288291
}
289292

290293
/**

app/code/Magento/Directory/Model/Currency/Import/AbstractImport.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function fetchRates()
9494
$data = [];
9595
$currencies = $this->_getCurrencyCodes();
9696
$defaultCurrencies = $this->_getDefaultCurrencyCodes();
97-
@set_time_limit(0);
97+
set_time_limit(0);
9898
foreach ($defaultCurrencies as $currencyFrom) {
9999
if (!isset($data[$currencyFrom])) {
100100
$data[$currencyFrom] = [];
@@ -111,6 +111,7 @@ public function fetchRates()
111111
}
112112
ksort($data[$currencyFrom]);
113113
}
114+
ini_restore('max_execution_time');
114115

115116
return $data;
116117
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Directory\Model\Currency\Import;
7+
8+
/**
9+
* Currency rate import model (From http://fixer.io/)
10+
*/
11+
class FixerIo extends \Magento\Directory\Model\Currency\Import\AbstractImport
12+
{
13+
/**
14+
* @var string
15+
*/
16+
const CURRENCY_CONVERTER_URL = 'http://api.fixer.io/latest?base={{CURRENCY_FROM}}&symbols={{CURRENCY_TO}}';
17+
18+
/**
19+
* Http Client Factory
20+
*
21+
* @var \Magento\Framework\HTTP\ZendClientFactory
22+
*/
23+
protected $httpClientFactory;
24+
25+
/**
26+
* Core scope config
27+
*
28+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
29+
*/
30+
private $scopeConfig;
31+
32+
/**
33+
* Initialize dependencies
34+
*
35+
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
36+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
37+
* @param \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
38+
*/
39+
public function __construct(
40+
\Magento\Directory\Model\CurrencyFactory $currencyFactory,
41+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
42+
\Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
43+
) {
44+
parent::__construct($currencyFactory);
45+
$this->scopeConfig = $scopeConfig;
46+
$this->httpClientFactory = $httpClientFactory;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function fetchRates()
53+
{
54+
$data = [];
55+
$currencies = $this->_getCurrencyCodes();
56+
$defaultCurrencies = $this->_getDefaultCurrencyCodes();
57+
58+
foreach ($defaultCurrencies as $currencyFrom) {
59+
if (!isset($data[$currencyFrom])) {
60+
$data[$currencyFrom] = [];
61+
}
62+
$data = $this->convertBatch($data, $currencyFrom, $currencies);
63+
ksort($data[$currencyFrom]);
64+
}
65+
return $data;
66+
}
67+
68+
/**
69+
* Return currencies convert rates in batch mode
70+
*
71+
* @param array $data
72+
* @param string $currencyFrom
73+
* @param array $currenciesTo
74+
* @return array
75+
*/
76+
private function convertBatch($data, $currencyFrom, $currenciesTo)
77+
{
78+
$currenciesStr = implode(',', $currenciesTo);
79+
$url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL);
80+
$url = str_replace('{{CURRENCY_TO}}', $currenciesStr, $url);
81+
82+
set_time_limit(0);
83+
$response = $this->getServiceResponse($url);
84+
ini_restore('max_execution_time');
85+
86+
foreach ($currenciesTo as $currencyTo) {
87+
if ($currencyFrom == $currencyTo) {
88+
$data[$currencyFrom][$currencyTo] = $this->_numberFormat(1);
89+
} else {
90+
if (empty($response['rates'][$currencyTo])) {
91+
$this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $currencyTo);
92+
$data[$currencyFrom][$currencyTo] = null;
93+
} else {
94+
$data[$currencyFrom][$currencyTo] = $this->_numberFormat(
95+
(double)$response['rates'][$currencyTo]
96+
);
97+
}
98+
}
99+
}
100+
return $data;
101+
}
102+
103+
/**
104+
* Get Fixer.io service response
105+
*
106+
* @param string $url
107+
* @param int $retry
108+
* @return array
109+
*/
110+
private function getServiceResponse($url, $retry = 0)
111+
{
112+
/** @var \Magento\Framework\HTTP\ZendClient $httpClient */
113+
$httpClient = $this->httpClientFactory->create();
114+
$response = [];
115+
116+
try {
117+
$jsonResponse = $httpClient->setUri(
118+
$url
119+
)->setConfig(
120+
[
121+
'timeout' => $this->scopeConfig->getValue(
122+
'currency/fixerio/timeout',
123+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
124+
),
125+
]
126+
)->request(
127+
'GET'
128+
)->getBody();
129+
130+
$response = json_decode($jsonResponse, true);
131+
} catch (\Exception $e) {
132+
if ($retry == 0) {
133+
$response = $this->getServiceResponse($url, 1);
134+
}
135+
}
136+
return $response;
137+
}
138+
139+
/**
140+
* {@inheritdoc}
141+
*/
142+
protected function _convert($currencyFrom, $currencyTo)
143+
{
144+
}
145+
}

app/code/Magento/Directory/Model/Currency/Import/Webservicex.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,32 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
// @codingStandardsIgnoreFile
7+
namespace Magento\Directory\Model\Currency\Import;
88

99
/**
1010
* Currency rate import model (From www.webservicex.net)
1111
*/
12-
namespace Magento\Directory\Model\Currency\Import;
13-
1412
class Webservicex extends \Magento\Directory\Model\Currency\Import\AbstractImport
1513
{
1614
/**
1715
* @var string
1816
*/
19-
const CURRENCY_CONVERTER_URL = 'http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency={{CURRENCY_FROM}}&ToCurrency={{CURRENCY_TO}}';
17+
const CURRENCY_CONVERTER_URL = 'http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?' .
18+
'FromCurrency={{CURRENCY_FROM}}&ToCurrency={{CURRENCY_TO}}';
2019

2120
/**
22-
* HTTP client
21+
* Http Client Factory
2322
*
24-
* @var \Magento\Framework\HTTP\ZendClient
23+
* @var \Magento\Framework\HTTP\ZendClientFactory
2524
*/
26-
protected $_httpClient;
25+
protected $httpClientFactory;
2726

2827
/**
29-
* Core store config
28+
* Core scope config
3029
*
3130
* @var \Magento\Framework\App\Config\ScopeConfigInterface
3231
*/
33-
protected $_scopeConfig;
32+
private $scopeConfig;
3433

3534
/**
3635
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
@@ -41,8 +40,7 @@ public function __construct(
4140
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
4241
) {
4342
parent::__construct($currencyFactory);
44-
$this->_scopeConfig = $scopeConfig;
45-
$this->_httpClient = new \Magento\Framework\HTTP\ZendClient();
43+
$this->scopeConfig = $scopeConfig;
4644
}
4745

4846
/**
@@ -55,13 +53,15 @@ protected function _convert($currencyFrom, $currencyTo, $retry = 0)
5553
{
5654
$url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL);
5755
$url = str_replace('{{CURRENCY_TO}}', $currencyTo, $url);
56+
/** @var \Magento\Framework\HTTP\ZendClient $httpClient */
57+
$httpClient = $this->getHttpClientFactory()->create();
5858

5959
try {
60-
$response = $this->_httpClient->setUri(
60+
$response = $httpClient->setUri(
6161
$url
6262
)->setConfig(
6363
[
64-
'timeout' => $this->_scopeConfig->getValue(
64+
'timeout' => $this->scopeConfig->getValue(
6565
'currency/webservicex/timeout',
6666
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
6767
),
@@ -84,4 +84,20 @@ protected function _convert($currencyFrom, $currencyTo, $retry = 0)
8484
}
8585
}
8686
}
87+
88+
/**
89+
* Get HttpClientFactory dependency
90+
*
91+
* @return \Magento\Framework\HTTP\ZendClientFactory
92+
*
93+
* @deprecated
94+
*/
95+
private function getHttpClientFactory()
96+
{
97+
if ($this->httpClientFactory === null) {
98+
$this->httpClientFactory = \Magento\Framework\App\ObjectManager::getInstance()
99+
->get('Magento\Framework\HTTP\ZendClientFactory');
100+
}
101+
return $this->httpClientFactory;
102+
}
87103
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Directory\Test\Unit\Model\Currency\Import;
8+
9+
class FixerIoTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @var \Magento\Directory\Model\Currency\Import\FixerIo
13+
*/
14+
private $model;
15+
16+
/**
17+
* @var \Magento\Directory\Model\CurrencyFactory|\PHPUnit_Framework_MockObject_MockObject
18+
*/
19+
private $currencyFactoryMock;
20+
21+
/**
22+
* @var \Magento\Framework\HTTP\ZendClientFactory|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
private $httpClientFactoryMock;
25+
26+
protected function setUp()
27+
{
28+
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
29+
30+
$this->currencyFactoryMock = $this->getMockBuilder('Magento\Directory\Model\CurrencyFactory')
31+
->disableOriginalConstructor()
32+
->setMethods(['create'])
33+
->getMock();
34+
$this->httpClientFactoryMock = $this->getMockBuilder('Magento\Framework\HTTP\ZendClientFactory')
35+
->disableOriginalConstructor()
36+
->setMethods(['create'])
37+
->getMock();
38+
$scopeMock = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface')
39+
->disableOriginalConstructor()
40+
->setMethods([])
41+
->getMock();
42+
43+
$this->model = $objectManagerHelper->getObject(
44+
'Magento\Directory\Model\Currency\Import\FixerIo',
45+
[
46+
'currencyFactory' => $this->currencyFactoryMock,
47+
'scopeConfig' => $scopeMock,
48+
'httpClientFactory' => $this->httpClientFactoryMock
49+
]
50+
);
51+
}
52+
53+
public function testFetchRates()
54+
{
55+
$currencyFromList = ['USD'];
56+
$currencyToList = ['EUR', 'UAH'];
57+
$responseBody = '{"base":"USD","date":"2015-10-07","rates":{"EUR":0.9022}}';
58+
$expectedCurrencyRateList = ['USD' => ['EUR' => 0.9022, 'UAH' => null]];
59+
$message = "We can't retrieve a rate from http://api.fixer.io/latest?base=USD&symbols=EUR,UAH for UAH.";
60+
61+
/** @var \Magento\Directory\Model\Currency|\PHPUnit_Framework_MockObject_MockObject $currencyMock */
62+
$currencyMock = $this->getMockBuilder('Magento\Directory\Model\Currency')
63+
->disableOriginalConstructor()
64+
->setMethods([])
65+
->getMock();
66+
/** @var \Magento\Framework\HTTP\ZendClient|\PHPUnit_Framework_MockObject_MockObject $currencyMock */
67+
$httpClientMock = $this->getMockBuilder('Magento\Framework\HTTP\ZendClient')
68+
->disableOriginalConstructor()
69+
->setMethods([])
70+
->getMock();
71+
/** @var \Zend_Http_Response|\PHPUnit_Framework_MockObject_MockObject $currencyMock */
72+
$httpResponseMock = $this->getMockBuilder('Zend_Http_Response')
73+
->disableOriginalConstructor()
74+
->setMethods([])
75+
->getMock();
76+
$this->currencyFactoryMock->expects($this->any())->method('create')->willReturn($currencyMock);
77+
$currencyMock->expects($this->once())->method('getConfigBaseCurrencies')->willReturn($currencyFromList);
78+
$currencyMock->expects($this->once())->method('getConfigAllowCurrencies')->willReturn($currencyToList);
79+
$this->httpClientFactoryMock->expects($this->any())->method('create')->willReturn($httpClientMock);
80+
$httpClientMock->expects($this->atLeastOnce())->method('setUri')->willReturnSelf();
81+
$httpClientMock->expects($this->atLeastOnce())->method('setConfig')->willReturnSelf();
82+
$httpClientMock->expects($this->atLeastOnce())->method('request')->willReturn($httpResponseMock);
83+
$httpResponseMock->expects($this->any())->method('getBody')->willReturn($responseBody);
84+
85+
$this->assertEquals($expectedCurrencyRateList, $this->model->fetchRates());
86+
$messages = $this->model->getMessages();
87+
$this->assertNotEmpty($messages);
88+
$this->assertTrue(is_array($messages));
89+
$this->assertEquals($message, (string)$messages[0]);
90+
}
91+
}

app/code/Magento/Directory/etc/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
<item name="label" xsi:type="string">Webservicex</item>
1414
<item name="class" xsi:type="string">Magento\Directory\Model\Currency\Import\Webservicex</item>
1515
</item>
16+
<item name="fixerio" xsi:type="array">
17+
<item name="label" xsi:type="string">Fixer.io</item>
18+
<item name="class" xsi:type="string">Magento\Directory\Model\Currency\Import\FixerIo</item>
19+
</item>
1620
</argument>
1721
</arguments>
1822
</type>

app/code/Magento/EncryptionKey/Controller/Adminhtml/Crypt/Key.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
87
namespace Magento\EncryptionKey\Controller\Adminhtml\Crypt;
98

109
/**
@@ -13,12 +12,9 @@
1312
abstract class Key extends \Magento\Backend\App\Action
1413
{
1514
/**
16-
* Check whether current administrator session allows this controller
15+
* Authorization level of a basic admin session
1716
*
18-
* @return bool
17+
* @see _isAllowed()
1918
*/
20-
protected function _isAllowed()
21-
{
22-
return $this->_authorization->isAllowed('Magento_EncryptionKey::crypt_key');
23-
}
19+
const ADMIN_RESOURCE = 'Magento_EncryptionKey::crypt_key';
2420
}

0 commit comments

Comments
 (0)