-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Padam87/bundle3
Exchange, currency pair support
- Loading branch information
Showing
8 changed files
with
159 additions
and
5 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
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,59 @@ | ||
<?php | ||
|
||
namespace Padam87\MoneyBundle\Doctrine\Mapping\Driver; | ||
|
||
use Doctrine\Persistence\Mapping\ClassMetadata; | ||
use Doctrine\Persistence\Mapping\Driver\MappingDriver; | ||
use Money\CurrencyPair; | ||
|
||
class CurrencyPairEmbeddedDriver implements MappingDriver | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function loadMetadataForClass($className, ClassMetadata $metadata) | ||
{ | ||
/* @var \Doctrine\ORM\Mapping\ClassMetadataInfo $metadata */ | ||
|
||
$metadata->isEmbeddedClass = true; | ||
|
||
$metadata->mapField( | ||
[ | ||
'fieldName' => 'baseCurrency', | ||
'type' => 'currency', | ||
] | ||
); | ||
|
||
$metadata->mapField( | ||
[ | ||
'fieldName' => 'counterCurrency', | ||
'type' => 'currency', | ||
] | ||
); | ||
|
||
$metadata->mapField( | ||
[ | ||
'fieldName' => 'conversionRatio', | ||
'type' => 'float', | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getAllClassNames() | ||
{ | ||
return [ | ||
CurrencyPair::class | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isTransient($className) | ||
{ | ||
return false; | ||
} | ||
} |
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
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,10 @@ | ||
<?php | ||
|
||
namespace Padam87\MoneyBundle\Entity; | ||
|
||
use Money\CurrencyPair; | ||
|
||
interface ExchangeRateInterface | ||
{ | ||
public function getCurrencyPair(): CurrencyPair; | ||
} |
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,48 @@ | ||
<?php | ||
|
||
namespace Padam87\MoneyBundle\Exchange; | ||
|
||
use Decimal\Decimal; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use Money\Currency; | ||
use Money\CurrencyPair; | ||
use App\Entity\ExchangeRate; | ||
use Money\Exception\UnresolvableCurrencyPairException; | ||
use Padam87\MoneyBundle\Entity\ExchangeRateInterface; | ||
use Padam87\MoneyBundle\Repository\ExchangeRateRepositoryInterface; | ||
|
||
class DatabaseExchange implements \Money\Exchange | ||
{ | ||
private $doctrine; | ||
|
||
public function __construct(ManagerRegistry $doctrine) | ||
{ | ||
$this->doctrine = $doctrine; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function quote(Currency $baseCurrency, Currency $counterCurrency) | ||
{ | ||
if ($baseCurrency->getCode() === $counterCurrency->getCode()) { | ||
return new CurrencyPair($baseCurrency, $counterCurrency, 1); | ||
} | ||
|
||
$repo = $this->doctrine->getRepository(ExchangeRateInterface::class); | ||
|
||
if (!$repo instanceof ExchangeRateRepositoryInterface) { | ||
throw new \LogicException( | ||
sprintf('"%s" must implement %s', $repo->getClassName(), ExchangeRateRepositoryInterface::class) | ||
); | ||
} | ||
|
||
if (null === $exchangeRate = $repo->getExchangeRate($baseCurrency, $counterCurrency)) { | ||
throw new UnresolvableCurrencyPairException( | ||
sprintf('%s - >%s ratio not found in the database.', $baseCurrency->getCode(), $counterCurrency->getCode()) | ||
); | ||
} | ||
|
||
return $exchangeRate->getCurrencyPair(); | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace Padam87\MoneyBundle\Repository; | ||
|
||
use Money\Currency; | ||
use Padam87\MoneyBundle\Entity\ExchangeRateInterface; | ||
|
||
interface ExchangeRateRepositoryInterface | ||
{ | ||
public function getExchangeRate(Currency $baseCurrency, Currency $counterCurrency): ?ExchangeRateInterface; | ||
} |
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
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