Completed
Push — remove-content-bundle ( 201341...8d07b3 )
by Kamil
52:29 queued 32:39
created

ChannelAndCurrencyBasedCalculator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 47
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A calculate() 0 20 4
A getType() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Core\Pricing;
13
14
use Sylius\Component\Channel\Context\ChannelContextInterface;
15
use Sylius\Component\Currency\Context\CurrencyContextInterface;
16
use Sylius\Component\Currency\Converter\CurrencyConverterInterface;
17
use Sylius\Component\Pricing\Calculator\CalculatorInterface;
18
use Sylius\Component\Pricing\Model\PriceableInterface;
19
20
/**
21
 * @author Mateusz Zalewski <[email protected]>
22
 */
23
class ChannelAndCurrencyBasedCalculator implements CalculatorInterface
24
{
25
    /**
26
     * @var CurrencyConverterInterface
27
     */
28
    private $currencyConverter;
29
30
    /**
31
     * @param CurrencyConverterInterface $currencyConverter
32
     */
33
    public function __construct(CurrencyConverterInterface $currencyConverter)
34
    {
35
        $this->currencyConverter = $currencyConverter;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function calculate(PriceableInterface $subject, array $configuration, array $context = [])
42
    {
43
        if (!isset($context['channel']) || !isset($context['currency'])) {
44
            throw new \InvalidArgumentException('You should configure currency and channel to determine a price.');
45
        }
46
47
        $pricingConfiguration = $subject->getPricingConfiguration();
48
49
        $channel = $context['channel'];
50
        $currencyCode = $context['currency'];
51
52
        if (!isset($pricingConfiguration[$channel->getCode()][$currencyCode])) {
53
            return $subject->getPrice();
54
        }
55
56
        return $this->currencyConverter->convertToBase(
57
            $pricingConfiguration[$channel->getCode()][$currencyCode],
58
            $currencyCode
59
        );
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getType()
66
    {
67
        return Calculators::CHANNEL_AND_CURRENCY_BASED;
68
    }
69
}
70