Converter::getSource()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace CL\CurrencyConvert;
4
5
use SebastianBergmann\Money\Currency;
6
use SebastianBergmann\Money\Money;
7
use LogicException;
8
9
/**
10
 * @author    Ivan Kerin <[email protected]>
11
 * @copyright 2014, Clippings Ltd.
12
 * @license   http://spdx.org/licenses/BSD-3-Clause
13
 */
14
class Converter
15
{
16
    /**
17
     * @var Converter
18
     */
19
    private static $instance;
20
21
    /**
22
     * Create a new Converter object that you can later get through "::get()"
23
     * @param  SourceInterface $source
24
     */
25 1
    public static function initialize(SourceInterface $source)
26
    {
27 1
        self::$instance = new static($source);
28 1
    }
29
30
    /**
31
     * @return Converter
32
     */
33 2
    public static function get()
34
    {
35 2
        if (! self::$instance) {
36 1
            throw new LogicException('Converter not initialized, call Converter::initialize(source)');
37
        }
38
39 1
        return self::$instance;
40
    }
41
42
    /**
43
     * Clear the object, returned through "::get"
44
     */
45 1
    public static function clear()
46
    {
47 1
        self::$instance = null;
48 1
    }
49
50
    /**
51
     * @var SourceInterface
52
     */
53
    private $source;
54
55 1
    public function __construct(SourceInterface $source)
56
    {
57 1
        $this->source = $source;
58 1
    }
59
60
    /**
61
     * @return SourceInterface
62
     */
63 1
    public function getSource()
64
    {
65 1
        return $this->source;
66
    }
67
68
    /**
69
     * @param  Money    $money
70
     * @param  Currency $to
71
     * @param  int      $roundingMode
72
     * @return Money
73
     */
74 1
    public function convert(Money $money, Currency $to, $roundingMode = PHP_ROUND_HALF_UP)
75
    {
76 1
        $new = new Money($money->getAmount(), $to);
77
78 1
        $conversionRate = $this->source->getRateBetween($money->getCurrency(), $to);
79
80 1
        return $new->multiply($conversionRate, $roundingMode);
81
    }
82
}
83