|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Directory\Test\Fixture; |
| 9 | + |
| 10 | +use Magento\Directory\Model\Currency; |
| 11 | +use Magento\Framework\App\ResourceConnection; |
| 12 | +use Magento\Framework\DataObject; |
| 13 | +use Magento\Framework\Locale\FormatInterface; |
| 14 | +use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface; |
| 15 | + |
| 16 | +/** |
| 17 | + * Data fixture for currency rate. |
| 18 | + */ |
| 19 | +class CurrencyRate implements RevertibleDataFixtureInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var FormatInterface |
| 23 | + */ |
| 24 | + private $format; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var Currency |
| 28 | + */ |
| 29 | + private $currency; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ResourceConnection |
| 33 | + */ |
| 34 | + private $resourceConnection; |
| 35 | + |
| 36 | + public function __construct( |
| 37 | + FormatInterface $format, |
| 38 | + Currency $currency, |
| 39 | + ResourceConnection $resourceConnection |
| 40 | + ) { |
| 41 | + $this->format = $format; |
| 42 | + $this->currency = $currency; |
| 43 | + $this->resourceConnection = $resourceConnection; |
| 44 | + } |
| 45 | + |
| 46 | + public function apply(array $data = []): ?DataObject |
| 47 | + { |
| 48 | + if (is_array($data)) { |
| 49 | + foreach ($data as $currencyCode => $rate) { |
| 50 | + foreach ($rate as $currencyTo => $value) { |
| 51 | + $value = abs((float) $this->format->getNumber($value)); |
| 52 | + $data[$currencyCode][$currencyTo] = $value; |
| 53 | + } |
| 54 | + } |
| 55 | + $this->currency->saveRates($data); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public function revert(DataObject $data): void |
| 60 | + { |
| 61 | + $connection = $this->resourceConnection->getConnection(); |
| 62 | + $currencyTable = $this->resourceConnection->getTableName('directory_currency_rate'); |
| 63 | + |
| 64 | + foreach ($data as $currencyCode => $rate) { |
| 65 | + foreach ($rate as $currencyTo) { |
| 66 | + $connection->delete( |
| 67 | + $currencyTable, |
| 68 | + [ |
| 69 | + 'currency_from = ?' => $currencyCode, |
| 70 | + 'currency_to = ?' => $currencyTo, |
| 71 | + ] |
| 72 | + ); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments