Passed
Push — master ( 31e5bc...a79a25 )
by Orkhan
01:45
created

Change::getEndDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 CarbonImmutable
19
     */
20
    private $startDate;
21
    /**
22
     * @var CarbonImmutable
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
     * @param string $currency
43
     *
44
     * @return float
45
     */
46
    public function startRate(string $currency): float
47
    {
48
        return $this->quotes[$this->findKey($currency)]['start_rate'];
49
    }
50
51
    /**
52
     * @param string $currency
53
     *
54
     * @return float
55
     */
56
    public function endRate(string $currency): float
57
    {
58
        return $this->quotes[$this->findKey($currency)]['end_rate'];
59
    }
60
61
    /**
62
     * @param string $currency
63
     *
64
     * @return float
65
     */
66
    public function change(string $currency): float
67
    {
68
        return $this->quotes[$this->findKey($currency)]['change'];
69
    }
70
71
    /**
72
     * @param string $currency
73
     *
74
     * @return float
75
     */
76
    public function changePercentage(string $currency): float
77
    {
78
        return $this->quotes[$this->findKey($currency)]['change_pct'];
79
    }
80
81
    /**
82
     * @param string $currency
83
     *
84
     * @return string
85
     */
86
    private function findKey(string $currency): string
87
    {
88
        $key = $this->source . $currency;
89
        if (! isset($this->quotes[$key])) {
90
            throw new \InvalidArgumentException(
91
                "{$currency} currencies is not available. Did you put it in request?"
92
            );
93
        }
94
95
        return $key;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getSource(): string
102
    {
103
        return $this->source;
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function getQuotes(): array
110
    {
111
        return $this->quotes;
112
    }
113
114
    /**
115
     * @return CarbonImmutable
116
     */
117
    public function getStartDate(): CarbonImmutable
118
    {
119
        return $this->startDate;
120
    }
121
122
    /**
123
     * @return CarbonImmutable
124
     */
125
    public function getEndDate(): CarbonImmutable
126
    {
127
        return $this->endDate;
128
    }
129
}
130