FiatRateObject::convert()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Sarnado\Converter\Objects;
4
5
use Sarnado\Converter\Checkers\TypeValidator;
6
use Sarnado\Converter\Helpers\ResultNumberFormatter;
7
8
/**
9
 * Class FiatRateObject
10
 */
11
class FiatRateObject
12
{
13
    /**
14
     * @var string
15
     */
16
    private $from;
17
    /**
18
     * @var string
19
     */
20
    private $to;
21
    /**
22
     * @var float|int
23
     */
24
    private $rate;
25
    /**
26
     * @var string
27
     */
28
    private $time;
29
30
    /**
31
     * FiatRateObject constructor.
32
     * @param string $from
33
     * @param string $to
34
     * @param float|int $rate
35
     * @param string $time
36
     */
37 81
    public function __construct(string $from, string $to, $rate, string $time)
38
    {
39 81
        $this->setFrom($from);
40 81
        $this->setTo($to);
41 81
        $this->setRate($rate);
42 81
        $this->setTime($time);
43 81
    }
44
45
    /**
46
     * @return string
47
     */
48 12
    public function getFrom(): string
49
    {
50 12
        return $this->from;
51
    }
52
53
    /**
54
     * @param string $from
55
     */
56 81
    public function setFrom(string $from)
57
    {
58 81
        $this->from = $from;
59 81
    }
60
61
    /**
62
     * @return string
63
     */
64 21
    public function getTo(): string
65
    {
66 21
        return $this->to;
67
    }
68
69
    /**
70
     * @param string $to
71
     */
72 81
    public function setTo(string $to)
73
    {
74 81
        $this->to = $to;
75 81
    }
76
77
    /**
78
     * @return float|int
79
     */
80 6
    public function getRate()
81
    {
82 6
        return $this->rate;
83
    }
84
85
    /**
86
     * @param float|int $rate
87
     */
88 81
    public function setRate($rate)
89
    {
90 81
        TypeValidator::isNum($rate);
91
92 81
        $this->rate = $rate;
93 81
    }
94
95
    /**
96
     * @return string
97
     */
98 3
    public function getTime(): string
99
    {
100 3
        return $this->time;
101
    }
102
103
    /**
104
     * @param string $time
105
     */
106 81
    public function setTime(string $time)
107
    {
108 81
        $this->time = $time;
109 81
    }
110
111
    /**
112
     * @param $amount
113
     * @return ConverterResultObject
114
     */
115 42
    public function convert($amount): ConverterResultObject
116
    {
117 42
        TypeValidator::isNum($amount);
118
119 18
        $amount = ResultNumberFormatter::format($amount);
120
121 18
        $rate = ResultNumberFormatter::format($this->rate);
122
123 18
        $res = bcmul($amount, $rate, ResultNumberFormatter::DEFAULT_SCALE);
124
125 18
        return new ConverterResultObject(ResultNumberFormatter::formatFiat($res), $this->to);
126
    }
127
}
128