Passed
Push — master ( 96eedf...95a867 )
by Orkhan
01:50
created

Quotes::getTimestamp()   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 0
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
use DateTimeImmutable;
7
8
class Quotes
9
{
10
    /**
11
     * @var string
12
     */
13
    private $source;
14
    /**
15
     * @var array
16
     */
17
    private $quotes;
18
    /**
19
     * @var int
20
     */
21
    private $timestamp;
22
    /**
23
     * @var CarbonImmutable|null
24
     */
25
    private $date;
26
27
    /**
28
     * Currency constructor.
29
     *
30
     * @param array $data
31
     *
32
     * @throws \Exception
33
     */
34
    public function __construct(array $data)
35
    {
36
        $this->source = $data['source'];
37
        $this->quotes = $data['quotes'];
38
        $this->timestamp = $data['timestamp'];
39
        $this->date = isset($data['date']) ? new CarbonImmutable($data['date']) : null;
40
    }
41
42
    /**
43
     * @param string $name
44
     *
45
     * @return float
46
     */
47
    public function __get(string $name): float
48
    {
49
        $key = $this->source . $name;
50
        if (! array_key_exists($key, $this->quotes)) {
51
            throw new \InvalidArgumentException("{$name} does not exist in API response. Did you put it in request?");
52
        }
53
54
        return $this->quotes[$key];
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function getQuotes(): array
61
    {
62
        return $this->quotes;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getSource(): string
69
    {
70
        return $this->source;
71
    }
72
73
    /**
74
     * @return DateTimeImmutable
75
     * @throws \Exception
76
     */
77
    public function getTimestamp(): DateTimeImmutable
78
    {
79
        return new CarbonImmutable($this->timestamp);
80
    }
81
82
    /**
83
     * @return CarbonImmutable|null
84
     */
85
    public function getDate(): ?CarbonImmutable
86
    {
87
        return $this->date;
88
    }
89
}
90