Completed
Push — master ( fa6eea...660d7d )
by BENOIT
01:21
created

ExchangeRate::swapCurrencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace BenTools\Currency\Model;
4
5
final class ExchangeRate implements ExchangeRateInterface
6
{
7
    /**
8
     * @var CurrencyInterface
9
     */
10
    private $sourceCurrency;
11
12
    /**
13
     * @var CurrencyInterface
14
     */
15
    private $targetCurrency;
16
17
    /**
18
     * @var float
19
     */
20
    private $ratio;
21
22
    /**
23
     * ExchangeRate constructor.
24
     * @param CurrencyInterface $sourceCurrency
25
     * @param CurrencyInterface $targetCurrency
26
     * @param float             $ratio
27
     */
28
    public function __construct(
29
        CurrencyInterface $sourceCurrency,
30
        CurrencyInterface $targetCurrency,
31
        float $ratio
32
    ) {
33
        $this->sourceCurrency = $sourceCurrency;
34
        $this->targetCurrency = $targetCurrency;
35
        if ($ratio <= 0) {
36
            throw new \InvalidArgumentException("Ratio must be a positive float.");
37
        }
38
        $this->ratio = $ratio;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function getRatio(): float
45
    {
46
        return $this->ratio;
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function getSourceCurrency(): CurrencyInterface
53
    {
54
        return $this->sourceCurrency;
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60
    public function getTargetCurrency(): CurrencyInterface
61
    {
62
        return $this->targetCurrency;
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68
    public function swapCurrencies(): ExchangeRateInterface
69
    {
70
        $clone = clone $this;
71
        $clone->sourceCurrency = $this->targetCurrency;
72
        $clone->targetCurrency = $this->sourceCurrency;
73
        $clone->ratio = 1 / $this->ratio;
74
        return $clone;
75
    }
76
}
77