Passed
Push — master ( 8884a6...674cbd )
by sarnado
02:38
created

SarnadoConverter::getFiatRates()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
3
4
namespace Sarnado\Converter;
5
6
7
use Sarnado\Converter\API\APIClient;
8
use Sarnado\Converter\Builders\CryptoCurrenciesCollectionBuilder;
9
use Sarnado\Converter\Builders\CryptoExchangesCollectionBuilder;
10
use Sarnado\Converter\Builders\CryptoRateObjectBuilder;
11
use Sarnado\Converter\Builders\CryptoRatesCollectionBuilder;
12
use Sarnado\Converter\Builders\FiatCurrenciesCollectionBuilder;
13
use Sarnado\Converter\Builders\FiatRateObjectBuilder;
14
use Sarnado\Converter\Builders\FiatRatesCollectionBuilder;
15
use Sarnado\Converter\Collections\CryptoCurrenciesCollection;
16
use Sarnado\Converter\Collections\CryptoExchangesCollection;
17
use Sarnado\Converter\Collections\FiatCurrenciesCollection;
18
use Sarnado\Converter\Objects\APIUsageObject;
19
20
/**
21
 * Class SarnadoConverter
22
 * @package Sarnado\Converter
23
 */
24
class SarnadoConverter
25
{
26
    /**
27
     * @var APIClient
28
     */
29
    private $apiClient;
30
31
    /**
32
     * SarnadoConverter constructor.
33
     * @param string $apiKey
34
     */
35 45
    public function __construct(string $apiKey)
36
    {
37 45
       $this->apiClient = new APIClient($apiKey);
38 45
    }
39
40
    /**
41
     * @return APIUsageObject
42
     * @throws Exceptions\BadAPIResponseException
43
     * @throws Exceptions\BadRequestException
44
     * @throws Exceptions\ConverterServerErrorException
45
     * @throws Exceptions\DefaultAPIException
46
     * @throws Exceptions\ReachedRateLimitException
47
     * @throws Exceptions\UnauthorizedException
48
     * @throws \GuzzleHttp\Exception\GuzzleException
49
     */
50 6
    public function getApiUsage(): APIUsageObject
51
    {
52 6
        $result = $this->apiClient->getAPIUsage()->getResult();
53 3
        return new APIUsageObject($result['current'], $result['available'], $result['refresh_after']);
54
    }
55
56
    /**
57
     * @return CryptoExchangesCollection
58
     * @throws Exceptions\BadAPIResponseException
59
     * @throws Exceptions\BadRequestException
60
     * @throws Exceptions\ConverterServerErrorException
61
     * @throws Exceptions\DefaultAPIException
62
     * @throws Exceptions\ReachedRateLimitException
63
     * @throws Exceptions\UnauthorizedException
64
     * @throws \GuzzleHttp\Exception\GuzzleException
65
     */
66 6
    public function getCryptoExchanges(): CryptoExchangesCollection
67
    {
68 6
        $result = $this->apiClient->getCryptoExchanges()->getResult();
69 3
        return CryptoExchangesCollectionBuilder::build($result);
70
    }
71
72
    /**
73
     * @param null $crypto
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $crypto is correct as it would always require null to be passed?
Loading history...
74
     * @param null $fiat
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fiat is correct as it would always require null to be passed?
Loading history...
75
     * @param null $exchange
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $exchange is correct as it would always require null to be passed?
Loading history...
76
     * @return Collections\CryptoRatesCollection|Objects\CryptoRateObject
77
     * @throws Exceptions\BadAPIResponseException
78
     * @throws Exceptions\BadRequestException
79
     * @throws Exceptions\ConverterServerErrorException
80
     * @throws Exceptions\DefaultAPIException
81
     * @throws Exceptions\ReachedRateLimitException
82
     * @throws Exceptions\UnauthorizedException
83
     * @throws \GuzzleHttp\Exception\GuzzleException
84
     */
85 12
    public function getCryptoRates($crypto = null, $fiat = null, $exchange = null)
86
    {
87 12
        $result = $this->apiClient->getCryptoRates($crypto, $fiat, $exchange)->getResult();
88 9
        if (!is_null($crypto) && !is_null($fiat))
0 ignored issues
show
introduced by
The condition is_null($crypto) is always true.
Loading history...
89
        {
90 6
            return CryptoRateObjectBuilder::build($result);
91
        }
92 3
        return CryptoRatesCollectionBuilder::build($result);
93
    }
94
95
    /**
96
     * @param null $from
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $from is correct as it would always require null to be passed?
Loading history...
97
     * @param null $to
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $to is correct as it would always require null to be passed?
Loading history...
98
     * @return Collections\FiatRatesCollection|Objects\FiatRateObject
99
     * @throws Exceptions\BadAPIResponseException
100
     * @throws Exceptions\BadRequestException
101
     * @throws Exceptions\ConverterServerErrorException
102
     * @throws Exceptions\DefaultAPIException
103
     * @throws Exceptions\ReachedRateLimitException
104
     * @throws Exceptions\UnauthorizedException
105
     * @throws \GuzzleHttp\Exception\GuzzleException
106
     */
107 9
    public function getFiatRates($from = null, $to = null)
108
    {
109 9
        $result = $this->apiClient->getFiatRates($from, $to)->getResult();
110 6
        if (!is_null($from) && !is_null($to))
0 ignored issues
show
introduced by
The condition is_null($from) is always true.
Loading history...
111
        {
112 3
            return FiatRateObjectBuilder::build($result);
113
        }
114 3
        return FiatRatesCollectionBuilder::build($result);
115
    }
116
117
    /**
118
     * @return CryptoCurrenciesCollection
119
     * @throws Exceptions\BadAPIResponseException
120
     * @throws Exceptions\BadRequestException
121
     * @throws Exceptions\ConverterServerErrorException
122
     * @throws Exceptions\DefaultAPIException
123
     * @throws Exceptions\ReachedRateLimitException
124
     * @throws Exceptions\UnauthorizedException
125
     * @throws \GuzzleHttp\Exception\GuzzleException
126
     */
127 6
    public function getCryptoCurrencies(): CryptoCurrenciesCollection
128
    {
129 6
        $result = $this->apiClient->getCryptoCurrencies()->getResult();
130 3
        return CryptoCurrenciesCollectionBuilder::build($result);
131
    }
132
133
134
    /**
135
     * @return FiatCurrenciesCollection
136
     * @throws Exceptions\BadAPIResponseException
137
     * @throws Exceptions\BadRequestException
138
     * @throws Exceptions\ConverterServerErrorException
139
     * @throws Exceptions\DefaultAPIException
140
     * @throws Exceptions\ReachedRateLimitException
141
     * @throws Exceptions\UnauthorizedException
142
     * @throws \GuzzleHttp\Exception\GuzzleException
143
     */
144 6
    public function getFiatCurrencies(): FiatCurrenciesCollection
145
    {
146 6
        $result = $this->apiClient->getFiatCurrencies()->getResult();
147 3
        return FiatCurrenciesCollectionBuilder::build($result);
148
    }
149
}
150