forked from ongr-archive/ContentBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michail Rybakov
committed
Dec 29, 2014
1 parent
694516c
commit aba4547
Showing
9 changed files
with
1,266 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ContentBundle\Core; | ||
|
||
/** | ||
* This interface defines structure for currency rates download driver. | ||
*/ | ||
interface CurrencyExchangeGetterInterface | ||
{ | ||
/** | ||
* Returns array of currency rates. For example: <code>['USD' => 1, 'EUR' => '1.678']</code>. | ||
* | ||
* @return array | ||
*/ | ||
public function getRates(); | ||
|
||
/** | ||
* Returns default currency name. | ||
* | ||
* @return string | ||
*/ | ||
public function getDefaultCurrencyName(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ContentBundle\Exception\Currency; | ||
|
||
/** | ||
* This exception is thrown when we try to retrieve currency rates while it's not loaded. | ||
*/ | ||
class RatesNotLoadedException extends \RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ContentBundle\Exception\Currency; | ||
|
||
/** | ||
* This exceptions is thrown when we request for currency that does not exists. | ||
*/ | ||
class UndefinedCurrencyException extends \UnexpectedValueException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ContentBundle\Service\Currency; | ||
|
||
use ONGR\ContentBundle\Exception\Currency\UndefinedCurrencyException; | ||
|
||
/** | ||
* This class handles currency rates download and exchange. | ||
*/ | ||
class CurrencyExchangeService | ||
{ | ||
/** | ||
* @var CurrencyRatesService | ||
*/ | ||
protected $rates = null; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $defaultCurrency; | ||
|
||
/** | ||
* @param CurrencyRatesService $rates | ||
* @param string $defaultCurrency | ||
*/ | ||
public function __construct(CurrencyRatesService $rates, $defaultCurrency) | ||
{ | ||
$this->rates = $rates; | ||
$this->defaultCurrency = $defaultCurrency; | ||
} | ||
|
||
/** | ||
* @param string $currency | ||
* | ||
* @return float | ||
* @throws UndefinedCurrencyException | ||
*/ | ||
public function getCurrencyRate($currency) | ||
{ | ||
$rates = $this->rates->getRates(); | ||
|
||
if (isset($rates[$currency])) { | ||
return $rates[$currency]; | ||
} | ||
|
||
throw new UndefinedCurrencyException('Currency ' . $currency . ' not found.'); | ||
} | ||
|
||
/** | ||
* @return array|null | ||
*/ | ||
public function getCurrencies() | ||
{ | ||
return $this->rates->getRates(); | ||
} | ||
|
||
/** | ||
* Rate calculation. | ||
* | ||
* @param float|int $amount | ||
* @param string $toCurrency | ||
* @param null $fromCurrency | ||
* | ||
* @return float | ||
*/ | ||
public function calculateRate($amount, $toCurrency, $fromCurrency = null) | ||
{ | ||
if (!isset($fromCurrency)) { | ||
$fromCurrency = $this->defaultCurrency; | ||
} | ||
|
||
if ($this->rates->getBaseCurrency() != $fromCurrency) { | ||
$amount = $amount / $this->getCurrencyRate($fromCurrency); | ||
} | ||
|
||
if ($toCurrency == $this->rates->getBaseCurrency()) { | ||
return $amount; | ||
} | ||
|
||
return $amount * $this->getCurrencyRate($toCurrency); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\ContentBundle\Service\Currency; | ||
|
||
use ONGR\ContentBundle\Core\CurrencyExchangeGetterInterface; | ||
use ONGR\ContentBundle\Exception\Currency\RatesNotLoadedException; | ||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Stash\Interfaces\ItemInterface; | ||
use Stash\Interfaces\PoolInterface; | ||
|
||
/** | ||
* This class provides currency rates. | ||
*/ | ||
class CurrencyRatesService implements LoggerAwareInterface | ||
{ | ||
/** | ||
* @var CurrencyExchangeGetterInterface | ||
*/ | ||
protected $driver; | ||
|
||
/** | ||
* @var PoolInterface | ||
*/ | ||
protected $pool; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $poorManLoad = true; | ||
|
||
/** | ||
* @var null|array | ||
*/ | ||
protected $rates = null; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
protected $logger; | ||
|
||
/** | ||
* @param CurrencyExchangeGetterInterface $driver Driver. | ||
* @param PoolInterface $pool Cache pool. | ||
* @param bool $poorManLoad Set to true if we want to load currencies on request. | ||
*/ | ||
public function __construct(CurrencyExchangeGetterInterface $driver, PoolInterface $pool, $poorManLoad = true) | ||
{ | ||
$this->driver = $driver; | ||
$this->pool = $pool; | ||
$this->poorManLoad = $poorManLoad; | ||
} | ||
|
||
/** | ||
* @return ItemInterface | ||
*/ | ||
protected function getCachePoolItem() | ||
{ | ||
return $this->pool->getItem('fox_currency'); | ||
} | ||
|
||
/** | ||
* This method returns exchange rates. | ||
* | ||
* @throws RatesNotLoadedException | ||
* @return array | ||
*/ | ||
public function getRates() | ||
{ | ||
if (isset($this->rates)) { | ||
return $this->rates; | ||
} | ||
|
||
/** @var ItemInterface $item */ | ||
$item = $this->getCachePoolItem(); | ||
|
||
$this->rates = $item->get(); | ||
if (isset($this->rates)) { | ||
return $this->rates; | ||
} | ||
|
||
if ($this->poorManLoad) { | ||
$this->logger && $this->logger->notice('Auto reloaded currency rates on CurrencyRatesService'); | ||
$this->reloadRates(); | ||
} else { | ||
throw new RatesNotLoadedException('Currency rates are not loaded and could not be loaded on demand'); | ||
} | ||
|
||
return $this->rates; | ||
} | ||
|
||
/** | ||
* Returns actual base currency name. | ||
* | ||
* @return string | ||
*/ | ||
public function getBaseCurrency() | ||
{ | ||
return $this->driver->getDefaultCurrencyName(); | ||
} | ||
|
||
/** | ||
* Reloads rates using given driver. | ||
*/ | ||
public function reloadRates() | ||
{ | ||
$this->rates = $this->driver->getRates(); | ||
$this->getCachePoolItem()->set($this->rates); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setLogger(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,8 +36,8 @@ protected function getDataArray() | |
'_id' => 3, | ||
'slug' => 'awsome', | ||
], | ||
] | ||
] | ||
], | ||
], | ||
]; | ||
} | ||
|
||
|
Oops, something went wrong.