Passed
Push — master ( b38d1f...17bee1 )
by Orkhan
01:23
created

Change::changeAmount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Orkhanahmadov\Currencylayer\Data;
4
5
use Carbon\CarbonImmutable;
6
7
class Change
8
{
9
    /**
10
     * @var string
11
     */
12
    private $source;
13
    /**
14
     * @var array
15
     */
16
    private $quotes;
17
    /**
18
     * @var \DateTimeImmutable
19
     */
20
    private $startDate;
21
    /**
22
     * @var \DateTimeImmutable
23
     */
24
    private $endDate;
25
26
    /**
27
     * Change constructor.
28
     *
29
     * @param array $data
30
     *
31
     * @throws \Exception
32
     */
33
    public function __construct(array $data)
34
    {
35
        $this->source = $data['source'];
36
        $this->quotes = $data['quotes'];
37
        $this->startDate = new CarbonImmutable($data['start_date']);
38
        $this->endDate = new CarbonImmutable($data['end_date']);
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getSource(): string
45
    {
46
        return $this->source;
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function getQuotes(): array
53
    {
54
        return $this->quotes;
55
    }
56
57
    /**
58
     * @return \DateTimeImmutable
59
     */
60
    public function getStartDate(): \DateTimeImmutable
61
    {
62
        return $this->startDate;
63
    }
64
65
    /**
66
     * @return \DateTimeImmutable
67
     */
68
    public function getEndDate(): \DateTimeImmutable
69
    {
70
        return $this->endDate;
71
    }
72
73
    /**
74
     * @param string $currency
75
     *
76
     * @return float
77
     */
78
    public function startRate(string $currency): float
79
    {
80
        return $this->quotes[$this->findKey($currency)]['start_rate'];
81
    }
82
83
    /**
84
     * @param string $currency
85
     *
86
     * @return float
87
     */
88
    public function endRate(string $currency): float
89
    {
90
        return $this->quotes[$this->findKey($currency)]['end_rate'];
91
    }
92
93
    /**
94
     * @param string $currency
95
     *
96
     * @return float
97
     */
98
    public function changeAmount(string $currency): float
99
    {
100
        return $this->quotes[$this->findKey($currency)]['change'];
101
    }
102
103
    /**
104
     * @param string $currency
105
     *
106
     * @return float
107
     */
108
    public function changePercentage(string $currency): float
109
    {
110
        return $this->quotes[$this->findKey($currency)]['change_pct'];
111
    }
112
113
    /**
114
     * @param string $currency
115
     *
116
     * @return string
117
     */
118
    private function findKey(string $currency): string
119
    {
120
        $key = $this->source.$currency;
121
        if (!isset($this->quotes[$key])) {
122
            throw new \InvalidArgumentException(
123
                "{$currency} currency is not available. Did you put it in request?"
124
            );
125
        }
126
127
        return $key;
128
    }
129
}
130