ChainsoConnector::apiCall()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelCryptoStats\Connectors;
4
5
use Exception;
6
7
class ChainsoConnector extends AbstractConnector
8
{
9
    public $supported_currencies = ['BTC', 'LTC', 'DOGE', 'ZEC', 'DOGE'];
10
    protected $api_url_prefix = 'https://chain.so//api/v2/';
11
    protected $api_description = 'https://chain.so/api';
12
    protected $block_link_prefix = 'https://chain.so/address/';
13
14
    public function validateAddress(string $address): bool
15
    {
16
        if ($address) {
17
            $url = $this->api_url_prefix.'is_address_valid/'.$this->currency.'/'.$address;
18
            $response = $this->apiCall($url);
19
20
            if (isset($response['is_valid'])) {
21
                return $response['is_valid'];
22
            }
23
        }
24
25
        throw new Exception('Wallet address can not be empty!');
26
    }
27
28
    public function getBalance(string $address): float
29
    {
30
        if ($address) {
31
            $url = $this->api_url_prefix.'get_address_balance/'.$this->currency.'/'.$address;
32
            $response = $this->apiCall($url);
33
34
            if (isset($response['confirmed_balance'])) {
35
                return $this->roundBalance($response['confirmed_balance']);
36
            }
37
        }
38
39
        throw new Exception('Wallet address can not be empty!');
40
    }
41
42
    public function getBlockExplorerLink(string $address): string
43
    {
44
        if ($address) {
45
            return $this->block_link_prefix.$this->currency.'/'.$address;
46
        }
47
48
        throw new Exception('Wallet address can not be empty!');
49
    }
50
51
    protected function apiCall(string $url)
52
    {
53
        $response = $this->sendApiRequest($url);
54
        if ($response && isset($response['data']) && isset($response['status']) && $response['status'] == 'success') {
55
            return $response['data'];
56
        }
57
58
        throw new Exception('Output data is not correct. Check the API description - '.$this->api_description.'!');
59
    }
60
}
61