Timeframe::source()   A
last analyzed

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
class Timeframe
6
{
7
    /**
8
     * @var string
9
     */
10
    private $source;
11
    /**
12
     * @var \DateTimeInterface
13
     */
14
    private $startDate;
15
    /**
16
     * @var \DateTimeInterface
17
     */
18
    private $endDate;
19
    /**
20
     * @var array
21
     */
22
    private $quotes;
23
24
    /**
25
     * Timeframe constructor.
26
     *
27
     * @param array $data
28
     *
29
     * @throws \Exception
30
     */
31
    public function __construct(array $data)
32
    {
33
        $this->source = $data['source'];
34
        $this->quotes = $data['quotes'];
35
        $this->startDate = new \DateTimeImmutable($data['start_date']);
36
        $this->endDate = new \DateTimeImmutable($data['end_date']);
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function source(): string
43
    {
44
        return $this->source;
45
    }
46
47
    /**
48
     * @return \DateTimeInterface
49
     */
50
    public function startDate(): \DateTimeInterface
51
    {
52
        return $this->startDate;
53
    }
54
55
    /**
56
     * @return \DateTimeInterface
57
     */
58
    public function endDate(): \DateTimeInterface
59
    {
60
        return $this->endDate;
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function allQuotes(): array
67
    {
68
        return $this->quotes;
69
    }
70
71
    /**
72
     * @param \DateTimeInterface|string $date
73
     *
74
     * @throws \Exception
75
     *
76
     * @return array
77
     */
78
    public function quotes($date): array
79
    {
80
        $date = $date instanceof \DateTimeInterface ?
81
            $date->format('Y-m-d') :
82
            (new \DateTimeImmutable($date))->format('Y-m-d');
83
84
        if (!isset($this->quotes[$date])) {
85
            throw new \InvalidArgumentException(
86
                "Quotes for {$date} is not available. Did you put it in request?"
87
            );
88
        }
89
90
        return $this->quotes[$date];
91
    }
92
93
    /**
94
     * @param string $name
95
     * @param $arguments
96
     *
97
     * @return float
98
     */
99
    public function __call(string $name, $arguments): float
100
    {
101
        if (isset($arguments[0])) {
102
            $date = $arguments[0] instanceof \DateTimeInterface ?
103
                $arguments[0]->format('Y-m-d') :
104
                $arguments[0];
105
        }
106
107
        $key = $this->source.$name;
108
        if (!isset($date) || !isset($this->quotes[$date]) || !isset($this->quotes[$date][$key])) {
109
            throw new \InvalidArgumentException(
110
                "{$name} currency or its argument is invalid. You sure you calling correct currency with correct date?"
111
            );
112
        }
113
114
        return $this->quotes[$date][$key];
115
    }
116
}
117