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

Timeframe::__call()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
nc 3
nop 2
dl 0
loc 16
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Orkhanahmadov\Currencylayer\Data;
4
5
use Carbon\CarbonImmutable;
6
7
class Timeframe
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
     * Timeframe 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
     * @return string
43
     */
44
    public function getSource(): string
45
    {
46
        return $this->source;
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function getQuotes(): array
53
    {
54
        return $this->quotes;
55
    }
56
57
    /**
58
     * @return CarbonImmutable
59
     */
60
    public function getStartDate(): CarbonImmutable
61
    {
62
        return $this->startDate;
63
    }
64
65
    /**
66
     * @return CarbonImmutable
67
     */
68
    public function getEndDate(): CarbonImmutable
69
    {
70
        return $this->endDate;
71
    }
72
73
    /**
74
     * @param \DateTimeImmutable|string $date
75
     *
76
     * @return array
77
     *
78
     * @throws \Exception
79
     */
80
    public function for($date): array
81
    {
82
        $date = $date instanceof \DateTimeImmutable ?
83
            $date->format('Y-m-d') :
84
            (new CarbonImmutable($date))->format('Y-m-d');
85
86
        if (! isset($this->quotes[$date])) {
87
            throw new \InvalidArgumentException(
88
                "Quotes for {$date} is not available. Did you put it in request?"
89
            );
90
        }
91
92
        return $this->quotes[$date];
93
    }
94
95
    /**
96
     * @param string $name
97
     * @param $arguments
98
     *
99
     * @return float
100
     */
101
    public function __call(string $name, $arguments): float
102
    {
103
        if (! isset($arguments[0])) {
104
            throw new \InvalidArgumentException(
105
                "{$name} method doesn't exist."
106
            );
107
        }
108
109
        $key = $this->source . $name;
110
        if (! isset($this->quotes[$arguments[0]]) || ! isset($this->quotes[$arguments[0]][$key])) {
111
            throw new \InvalidArgumentException(
112
                "{$this->source} -> {$name} quotes for {$arguments[0]} is not available. Did you put it in request?"
113
            );
114
        }
115
116
        return $this->quotes[$arguments[0]][$key];
117
    }
118
}
119