Completed
Pull Request — master (#11)
by ARCANEDEV
08:25
created

Rate   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 162
ccs 32
cts 32
cp 1
rs 10
wmc 11
lcom 1
cbo 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A from() 0 4 1
A getFromCurrency() 0 4 1
A to() 0 4 1
A getToCurrency() 0 4 1
A ratio() 0 4 1
A make() 0 4 1
A convert() 0 4 1
A reverse() 0 8 1
A toArray() 0 8 1
A toJson() 0 4 1
1
<?php namespace Arcanedev\Currencies\Entities;
2
3
use Arcanedev\Currencies\Contracts\Entities\Rate as RateContract;
4
use Illuminate\Contracts\Support\Arrayable;
5
use Illuminate\Contracts\Support\Jsonable;
6
7
/**
8
 * Class     Rate
9
 *
10
 * @package  Arcanedev\Currencies\Entities
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class Rate implements RateContract, Arrayable, Jsonable
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * @var string
21
     */
22
    protected $from;
23
24
    /**
25
     * @var string
26
     */
27
    protected $to;
28
29
    /**
30
     * @var double
31
     */
32
    protected $ratio;
33
34
    /* ------------------------------------------------------------------------------------------------
35
     |  Constructor
36
     | ------------------------------------------------------------------------------------------------
37
     */
38
    /**
39
     * Rate constructor.
40
     *
41
     * @param  string  $from
42
     * @param  string  $to
43
     * @param  double  $ratio
44
     */
45 120
    public function __construct($from, $to, $ratio)
46
    {
47 120
        $this->from  = $from;
48 120
        $this->to    = $to;
49 120
        $this->ratio = $ratio;
50 120
    }
51
52
    /* ------------------------------------------------------------------------------------------------
53
     |  Getters & Setters
54
     | ------------------------------------------------------------------------------------------------
55
     */
56
    /**
57
     * Get the `from` currency iso.
58
     *
59
     * @return string
60
     */
61 84
    public function from()
62
    {
63 84
        return $this->from;
64
    }
65
66
    /**
67
     * Get the `from` currency.
68
     *
69
     * @return \Arcanedev\Currencies\Entities\Currency
70
     */
71 12
    public function getFromCurrency()
72
    {
73 12
        return currency_manager()->findOrFail($this->from());
74
    }
75
76
    /**
77
     * Get the `to` currency iso.
78
     *
79
     * @return string
80
     */
81 96
    public function to()
82
    {
83 96
        return $this->to;
84
    }
85
86
    /**
87
     * Get the `to` currency.
88
     *
89
     * @return \Arcanedev\Currencies\Entities\Currency
90
     */
91 12
    public function getToCurrency()
92
    {
93 12
        return currency_manager()->findOrFail($this->to());
94
    }
95
96
    /**
97
     * Get the rate ratio.
98
     *
99
     * @return float
100
     */
101 84
    public function ratio()
102
    {
103 84
        return $this->ratio;
104
    }
105
106
    /* ------------------------------------------------------------------------------------------------
107
     |  Main Functions
108
     | ------------------------------------------------------------------------------------------------
109
     */
110
    /**
111
     * Make a rate instance.
112
     *
113
     * @param  string  $from
114
     * @param  string  $to
115
     * @param  double  $ratio
116
     *
117
     * @return self
118
     */
119 108
    public static function make($from, $to, $ratio)
120
    {
121 108
        return new self($from, $to, $ratio);
122
    }
123
124
    /**
125
     * Convert the amount.
126
     *
127
     * @param  double|int  $amount
128
     *
129
     * @return double|int
130
     */
131 12
    public function convert($amount)
132
    {
133 12
        return $amount * $this->ratio();
134
    }
135
136
    /**
137
     * Reverse the rate.
138
     *
139
     * @return self
140
     */
141 12
    public function reverse()
142
    {
143 12
        return self::make(
144 12
            $this->to(),
145 12
            $this->from(),
146 12
            round(1 / $this->ratio(), 6)
147 9
        );
148
    }
149
150
    /**
151
     * Get the instance as an array.
152
     *
153
     * @return array
154
     */
155 24
    public function toArray()
156
    {
157
        return [
158 24
            'from'  => $this->from(),
159 24
            'to'    => $this->to(),
160 24
            'ratio' => $this->ratio(),
161 18
        ];
162
    }
163
164
    /**
165
     * Convert the object to its JSON representation.
166
     *
167
     * @param  int $options
168
     * @return string
169
     */
170 12
    public function toJson($options = 0)
171
    {
172 12
        return json_encode($this->toArray(), $options);
173
    }
174
}
175