Passed
Push — master ( fe2f0d...e876ed )
by Orkhan
01:49
created

Currency::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Orkhanahmadov\Currencylayer;
4
5
use Carbon\CarbonImmutable;
6
use DateTimeImmutable;
7
8
class Currency
9
{
10
    /**
11
     * @var array
12
     */
13
    private $quotes;
14
    /**
15
     * @var string
16
     */
17
    private $source;
18
    /**
19
     * @var int
20
     */
21
    private $timestamp;
22
    /**
23
     * @var string|null
24
     */
25
    private $date;
26
27
    /**
28
     * Currency constructor.
29
     *
30
     * @param array $quotes
31
     * @param string $source
32
     * @param int $timestamp
33
     * @param string|null $date
34
     */
35
    public function __construct(array $quotes, string $source, int $timestamp, ?string $date = null)
36
    {
37
        $this->quotes = $quotes;
38
        $this->source = $source;
39
        $this->timestamp = $timestamp;
40
        $this->date = $date;
41
    }
42
43
    /**
44
     * @param string $name
45
     *
46
     * @return mixed
47
     */
48
    public function __get(string $name)
49
    {
50
        $key = $this->source . $name;
51
        if (! array_key_exists($key, $this->quotes)) {
52
            throw new \InvalidArgumentException($name . ' does not exist in API response. Did you requested it?');
53
        }
54
55
        return $this->quotes[$key];
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function getQuotes(): array
62
    {
63
        return $this->quotes;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getSource(): string
70
    {
71
        return $this->source;
72
    }
73
74
    /**
75
     * @return DateTimeImmutable
76
     * @throws \Exception
77
     */
78
    public function getTimestamp(): DateTimeImmutable
79
    {
80
        return new CarbonImmutable($this->timestamp);
81
    }
82
}
83