Skip to content

Commit

Permalink
Merge pull request #3 from Padam87/bundle3
Browse files Browse the repository at this point in the history
Exchange, currency pair support
  • Loading branch information
Padam87 authored Feb 18, 2021
2 parents b6a07ea + 5d76fd1 commit 375c8ef
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 5 deletions.
14 changes: 12 additions & 2 deletions DependencyInjection/Padam87MoneyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,21 @@ public function process(ContainerBuilder $container)
{
$config = $container->getParameter('padam87_money.config');

$container->getDefinition('doctrine.orm.default_metadata_driver')->addMethodCall(
$driver = $container->getDefinition('doctrine.orm.default_metadata_driver');

$driver->addMethodCall(
'addDriver',
[
$container->getDefinition('Padam87\MoneyBundle\Doctrine\Mapping\Driver\MoneyEmbeddedDriver'),
'Money'
'Money\Money'
]
);

$driver->addMethodCall(
'addDriver',
[
$container->getDefinition('Padam87\MoneyBundle\Doctrine\Mapping\Driver\CurrencyPairEmbeddedDriver'),
'Money\CurrencyPair'
]
);

Expand Down
59 changes: 59 additions & 0 deletions Doctrine/Mapping/Driver/CurrencyPairEmbeddedDriver.php
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;
}
}
4 changes: 2 additions & 2 deletions Doctrine/Mapping/Driver/MoneyEmbeddedDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Padam87\MoneyBundle\Doctrine\Mapping\Driver;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Money\Money;

class MoneyEmbeddedDriver implements MappingDriver
Expand Down
10 changes: 10 additions & 0 deletions Entity/ExchangeRateInterface.php
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;
}
48 changes: 48 additions & 0 deletions Exchange/DatabaseExchange.php
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();
}
}
11 changes: 11 additions & 0 deletions Repository/ExchangeRateRepositoryInterface.php
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;
}
16 changes: 16 additions & 0 deletions Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:
arguments:
$config: '%padam87_money.config%'

Padam87\MoneyBundle\Doctrine\Mapping\Driver\CurrencyPairEmbeddedDriver: ~

Padam87\MoneyBundle\Money\Formatter\NumberFormatterFactory: ~

padam87_money.number_formatter:
Expand All @@ -32,3 +34,17 @@ services:
Padam87\MoneyBundle\Form\CurrencyType:
arguments:
$config: '%padam87_money.config%'

Padam87\MoneyBundle\Exchange\DatabaseExchange:
arguments:
- '@Doctrine\Persistence\ManagerRegistry'

Money\Exchange\ReversedCurrenciesExchange:
arguments:
- '@Padam87\MoneyBundle\Exchange\DatabaseExchange'

Money\Converter:
class: Money\Converter
arguments:
- '@Money\Currencies\CurrencyList'
- '@Money\Exchange\ReversedCurrenciesExchange'
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Symfony bundle for https://github.com/moneyphp/money",
"type": "symfony-bundle",
"require": {
"php": "^7.0",
"php": "^7.4 || ^8.0",
"ext-bcmath": "*",
"symfony/framework-bundle": "^4.0 || ^5.0",
"moneyphp/money": "^3.1",
Expand Down

0 comments on commit 375c8ef

Please sign in to comment.