newscoop/plugin-NewscoopPaywallBundle

View on GitHub
Serializer/CurrencyConverterHandler.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

/**
 * @author Rafał Muszyński <rafal.muszynski@sourcefabric.org>
 * @copyright 2015 Sourcefabric z.ú.
 * @license http://www.gnu.org/licenses/gpl-3.0.txt
 */
namespace Newscoop\PaywallBundle\Serializer;

use JMS\Serializer\JsonSerializationVisitor;
use Newscoop\PaywallBundle\Currency\Context\CurrencyContextInterface;
use Newscoop\PaywallBundle\Entity\PriceableInterface;
use Sylius\Component\Currency\Converter\CurrencyConverterInterface;

/**
 * Currency converter handler. Handles currency conversion.
 */
class CurrencyConverterHandler
{
    private $converter;
    private $context;

    /**
     * Construct.
     *
     * @param CurrencyConverterInterface $converter
     * @param CurrencyContextInterface   $context
     */
    public function __construct(CurrencyConverterInterface $converter, CurrencyContextInterface $context)
    {
        $this->converter = $converter;
        $this->context = $context;
    }

    public function serializeToJson(JsonSerializationVisitor $visitor, PriceableInterface $subscription)
    {
        if (!$subscription) {
            return;
        }

        return $this->converter->convert($subscription->getPrice(), $this->context->getCurrency());
    }
}