diff --git a/DependencyInjection/Padam87MoneyExtension.php b/DependencyInjection/Padam87MoneyExtension.php index a238939..98ddb4d 100644 --- a/DependencyInjection/Padam87MoneyExtension.php +++ b/DependencyInjection/Padam87MoneyExtension.php @@ -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' ] ); diff --git a/Doctrine/Mapping/Driver/CurrencyPairEmbeddedDriver.php b/Doctrine/Mapping/Driver/CurrencyPairEmbeddedDriver.php new file mode 100644 index 0000000..7c495c3 --- /dev/null +++ b/Doctrine/Mapping/Driver/CurrencyPairEmbeddedDriver.php @@ -0,0 +1,59 @@ +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; + } +} diff --git a/Doctrine/Mapping/Driver/MoneyEmbeddedDriver.php b/Doctrine/Mapping/Driver/MoneyEmbeddedDriver.php index 85062cf..02b89df 100644 --- a/Doctrine/Mapping/Driver/MoneyEmbeddedDriver.php +++ b/Doctrine/Mapping/Driver/MoneyEmbeddedDriver.php @@ -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 diff --git a/Entity/ExchangeRateInterface.php b/Entity/ExchangeRateInterface.php new file mode 100644 index 0000000..6f08fac --- /dev/null +++ b/Entity/ExchangeRateInterface.php @@ -0,0 +1,10 @@ +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(); + } +} diff --git a/Repository/ExchangeRateRepositoryInterface.php b/Repository/ExchangeRateRepositoryInterface.php new file mode 100644 index 0000000..ef8c67d --- /dev/null +++ b/Repository/ExchangeRateRepositoryInterface.php @@ -0,0 +1,11 @@ +