Passed
Push — master ( e876ed...96eedf )
by Orkhan
01:40
created

Currency   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 80
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getQuotes() 0 3 1
A __get() 0 8 2
A getTimestamp() 0 3 1
A getSource() 0 3 1
A getDate() 0 3 1
A __construct() 0 6 2
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 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->quotes = $data['quotes'];
37
        $this->source = $data['source'];
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 mixed
46
     */
47
    public function __get(string $name)
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 requested it?');
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