-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 #1 from umpirsky/feature/configurable-currencies
[WIP] Added exchange rates
- Loading branch information
Showing
29 changed files
with
927 additions
and
20 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,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); | ||
} | ||
} |
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,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); | ||
} |
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,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')); | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,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); | ||
} |
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
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,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 | ||
{ | ||
} |
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,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'; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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); | ||
} |
Oops, something went wrong.