Skip to content

Commit

Permalink
Merge pull request #1 from umpirsky/feature/configurable-currencies
Browse files Browse the repository at this point in the history
[WIP] Added exchange rates
  • Loading branch information
Paweł Jędrzejewski committed Jun 6, 2013
2 parents c2370f4 + 9f5ee89 commit 54806e8
Show file tree
Hide file tree
Showing 29 changed files with 927 additions and 20 deletions.
36 changes: 36 additions & 0 deletions Context/CurrencyContext.php
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\MoneyBundle\Context;

use Symfony\Component\HttpFoundation\Session\SessionInterface;

class CurrencyContext implements CurrencyContextInterface
{
protected $session;
protected $defaultCurrency;

public function __construct(SessionInterface $session, $defaultCurrency)
{
$this->session = $session;
$this->defaultCurrency = $defaultCurrency;
}

public function getCurrency()
{
return $this->session->get('currency', $this->defaultCurrency);
}

public function setCurrency($currency)
{
return $this->session->set('currency', $currency);
}
}
18 changes: 18 additions & 0 deletions Context/CurrencyContextInterface.php
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\MoneyBundle\Context;

interface CurrencyContextInterface
{
public function getCurrency();
public function setCurrency($currency);
}
25 changes: 25 additions & 0 deletions Controller/CurrencyController.php
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\MoneyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class CurrencyController extends Controller
{
public function changeAction(Request $request, $currency)
{
$this->get('sylius.currency_context')->setCurrency($currency);

return $this->redirect($request->headers->get('referer'));
}
}
33 changes: 33 additions & 0 deletions Converter/CurrencyConverter.php
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\MoneyBundle\Converter;

use Sylius\Bundle\ResourceBundle\Model\RepositoryInterface;

class CurrencyConverter implements CurrencyConverterInterface
{
protected $exchangeRateRepository;

public function __construct(RepositoryInterface $exchangeRateRepository)
{
$this->exchangeRateRepository = $exchangeRateRepository;
}

public function convert($value, $currency)
{
if (null === $exchangeRate = $this->exchangeRateRepository->findOneByCurrency($currency)) {
return $value;
}

return $value / $exchangeRate->getRate();
}
}
17 changes: 17 additions & 0 deletions Converter/CurrencyConverterInterface.php
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\MoneyBundle\Converter;

interface CurrencyConverterInterface
{
public function convert($value, $currency);
}
32 changes: 32 additions & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -35,11 +35,43 @@ public function getConfigTreeBuilder()

$rootNode
->children()
->scalarNode('driver')->isRequired()->cannotBeEmpty()->end()
->scalarNode('engine')->defaultValue('twig')->cannotBeEmpty()->end()
->scalarNode('locale')->defaultValue('en')->cannotBeEmpty()->end()
->scalarNode('currency')->defaultValue('EUR')->cannotBeEmpty()->end()
->end()
;

$this->addClassesSection($rootNode);

return $treeBuilder;
}

private function addClassesSection(ArrayNodeDefinition $node)
{
$node
->children()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
->arrayNode('exchange_rate')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->end()
->scalarNode('controller')->defaultValue('Sylius\Bundle\ResourceBundle\Controller\ResourceController')->end()
->scalarNode('repository')->end()
->scalarNode('form')->defaultValue('Sylius\Bundle\MoneyBundle\Form\Type\ExchangeRateType')->end()
->end()
->end()
->arrayNode('currency')
->addDefaultsIfNotSet()
->children()
->scalarNode('controller')->defaultValue('Sylius\Bundle\MoneyBundle\Controller\CurrencyController')->end()
->end()
->end()
->end()
->end()
->end()
;
}
}
32 changes: 31 additions & 1 deletion DependencyInjection/SyliusMoneyExtension.php
Expand Up @@ -11,6 +11,7 @@

namespace Sylius\Bundle\MoneyBundle\DependencyInjection;

use Sylius\Bundle\MoneyBundle\SyliusMoneyBundle;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -33,9 +34,38 @@ public function load(array $config, ContainerBuilder $container)
$config = $processor->processConfiguration(new Configuration(), $config);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');

$driver = $config['driver'];

if (!in_array($driver, SyliusMoneyBundle::getSupportedDrivers())) {
throw new \InvalidArgumentException(sprintf('Driver "%s" is unsupported for SyliusMoneyBundle', $driver));
}

$loader->load(sprintf('driver/%s.xml', $driver));

$container->setParameter('sylius.driver', $driver);
$container->setParameter('sylius.engine', $config['engine']);
$container->setParameter('sylius.money.locale', $config['locale']);
$container->setParameter('sylius.money.currency', $config['currency']);

$this->mapClassParameters($config['classes'], $container);

$loader->load('services.xml');
}

/**
* Remap class parameters.
*
* @param array $classes
* @param ContainerBuilder $container
*/
protected function mapClassParameters(array $classes, ContainerBuilder $container)
{
foreach ($classes as $model => $serviceClasses) {
foreach ($serviceClasses as $service => $class) {
$service = $service === 'form' ? 'form.type' : $service;
$container->setParameter(sprintf('sylius.%s.%s.class', $service, $model), $class);
}
}
}
}
18 changes: 18 additions & 0 deletions Entity/ExchangeRate.php
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\MoneyBundle\Entity;

use Sylius\Bundle\MoneyBundle\Model\ExchangeRate as BaseExchangeRate;

class ExchangeRate extends BaseExchangeRate
{
}
60 changes: 60 additions & 0 deletions Form/Type/ExchangeRateType.php
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\MoneyBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;

/**
* Sylius exchange rate type.
*
* @author Saša Stamenković <umpirsky@gmail.com>
*/
class ExchangeRateType extends AbstractType
{
protected $dataClass;

public function __construct($dataClass)
{
$this->dataClass = $dataClass;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('currency', 'text', array( // TODO: use currency type when we upgrade to 2.3
'label' => 'sylius.form.exchange_rate.currency'
))
->add('rate', 'text', array(
'label' => 'sylius.form.exchange_rate.rate'
))
;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setDefaults(array(
'data_class' => $this->dataClass
))
;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'sylius_exchange_rate';
}
}
68 changes: 68 additions & 0 deletions Model/ExchangeRate.php
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\MoneyBundle\Model;

use DateTime;

class ExchangeRate implements ExchangeRateInterface
{
protected $id;
protected $currency;
protected $rate;
protected $createdAt;
protected $updatedAt;

public function getId()
{
return $this->id;
}

public function getCurrency()
{
return $this->currency;
}

public function setCurrency($currency)
{
$this->currency = $currency;
}

public function getRate()
{
return $this->rate;
}

public function setRate($rate)
{
$this->rate = $rate;
}

public function getCreatedAt()
{
return $this->createdAt;
}

public function setCreatedAt(DateTime $createdAt)
{
$this->createdAt = $createdAt;
}

public function getUpdatedAt()
{
return $this->updatedAt;
}

public function setUpdatedAt(DateTime $updatedAt)
{
$this->updatedAt = $updatedAt;
}
}
22 changes: 22 additions & 0 deletions Model/ExchangeRateInterface.php
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\MoneyBundle\Model;

use Sylius\Bundle\ResourceBundle\Model\TimestampableInterface;

interface ExchangeRateInterface extends TimestampableInterface
{
public function getCurrency();
public function setCurrency($currency);
public function getRate();
public function setRate($rate);
}

0 comments on commit 54806e8

Please sign in to comment.