Completed
Push — symfony3-wololo-packages ( fecf70...6bf04d )
by Kamil
28:46 queued 11:35
created

CurrencyConverter::convert()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 3
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\Currency\Converter;
13
14
use Sylius\Component\Currency\Model\ExchangeRateInterface;
15
use Sylius\Component\Currency\Repository\ExchangeRateRepositoryInterface;
16
17
/**
18
 * @author Paweł Jędrzejewski <[email protected]>
19
 */
20
final class CurrencyConverter implements CurrencyConverterInterface
21
{
22
    /**
23
     * @var ExchangeRateRepositoryInterface
24
     */
25
    private $exchangeRateRepository;
26
27
    /**
28
     * @var array
29
     */
30
    private $cache;
31
32
    /**
33
     * @param ExchangeRateRepositoryInterface $exchangeRateRepository
34
     */
35
    public function __construct(ExchangeRateRepositoryInterface $exchangeRateRepository)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $exchangeRateRepository exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
36
    {
37
        $this->exchangeRateRepository = $exchangeRateRepository;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function convert($amount, $sourceCurrencyCode, $targetCurrencyCode)
44
    {
45
        if ($sourceCurrencyCode === $targetCurrencyCode) {
46
            return $amount;
47
        }
48
49
        $exchangeRate = $this->getExchangeRate($sourceCurrencyCode, $targetCurrencyCode);
50
51
        if (null === $exchangeRate) {
52
            return $amount;
53
        }
54
55
        if ($exchangeRate->getSourceCurrency()->getCode() === $sourceCurrencyCode) {
56
            return (int) round($amount * $exchangeRate->getRatio());
57
        }
58
59
        return (int) round($amount / $exchangeRate->getRatio());
60
    }
61
62
    /**
63
     * @param string $sourceCode
64
     * @param string $targetCode
65
     *
66
     * @return ExchangeRateInterface
67
     */
68
    private function getExchangeRate($sourceCode, $targetCode)
69
    {
70
        $sourceTargetIndex = $this->createIndex($sourceCode, $targetCode);
71
72
        if (isset($this->cache[$sourceTargetIndex])) {
73
            return $this->cache[$sourceTargetIndex];
74
        }
75
76
        $targetSourceIndex = $this->createIndex($targetCode, $sourceCode);
77
78
        if (isset($this->cache[$targetSourceIndex])) {
79
            return $this->cache[$targetSourceIndex];
80
        }
81
82
        return $this->cache[$sourceTargetIndex] = $this->exchangeRateRepository->findOneWithCurrencyPair($sourceCode, $targetCode);
83
    }
84
85
    /**
86
     * @param $prefix
87
     * @param $suffix
88
     *
89
     * @return string
90
     */
91
    private function createIndex($prefix, $suffix)
92
    {
93
        return sprintf('%s-%s', $prefix, $suffix);
94
    }
95
}
96