Converter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 2
cbo 2
dl 0
loc 69
ccs 19
cts 19
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 4 1
A get() 0 8 2
A clear() 0 4 1
A __construct() 0 4 1
A getSource() 0 4 1
A convert() 0 8 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