CoinMarketCap   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getTicker() 0 8 1
A doRequest() 0 18 2
A getCacheKey() 0 6 1
A getTickerBySymbol() 0 15 3
A getListings() 0 5 1
A getTickerById() 0 5 1
1
<?php
2
3
namespace Bineks\Coinmarketcap\Services;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Facades\Cache;
7
8
/**
9
 * Class CoinMarketCap.
10
 */
11
class CoinMarketCap
12
{
13
    /** @var string */
14
    const SERVICE_URL = 'https://api.coinmarketcap.com/v2/';
15
16
    /**
17
     * Send request to CoinMarketCap.com.
18
     *
19
     * @param string $method
20
     * @param string $url
21
     * @param array  $query
22
     *
23
     * @return null|mixed
24
     */
25
    public function doRequest(string $method, string $url, array $query = [])
26
    {
27
        return Cache::remember(
28
            $this->getCacheKey($method, $url, $query),
29
            Carbon::now()->addSeconds(config('services.coinmarketcap.cache.timeout', 5)),
30
            function () use ($method, $url, $query) {
31
                $client = new \GuzzleHttp\Client([
32
                    'base_uri'  => self::SERVICE_URL,
33
                    'protocols' => 'https',
34
                ]);
35
36
                $response = $client->request($method, $url, [
37
                    'http_errors' => false,
38
                    'query'       => $query,
39
                ]);
40
41
                if ($response->getStatusCode() == 200) {
42
                    return json_decode($response->getBody()->getContents(), true);
43
                }
44
            });
45
    }
46
47
    /**
48
     * This method gets cryptocurrency ticker data in order of rank.
49
     * The maximum number of results per call is 100.
50
     * Pagination is possible by using the start and limit parameters.
51
     *
52
     * @param int $start
53
     * @param int $limit
54
     *
55
     * @throws \GuzzleHttp\Exception\GuzzleException
56
     *
57
     * @return null|array
58
     */
59
    public function getTicker(int $start = 1, int $limit = 100): ?array
60
    {
61
        $response = $this->doRequest('GET', 'ticker', [
62
            'start' => $start,
63
            'limit' => $limit,
64
        ]);
65
66
        return array_get($response, 'data');
67
    }
68
69
    /**
70
     * This  method gets cryptocurrency ticker data.
71
     *
72
     * @param int $id
73
     *
74
     * @throws \GuzzleHttp\Exception\GuzzleException
75
     *
76
     * @return null|array
77
     */
78
    public function getTickerById(int $id): ?array
79
    {
80
        $response = $this->doRequest('GET', "ticker/{$id}");
81
82
        return array_get($response, 'data');
83
    }
84
85
    /**
86
     * This methods gets cryptocurrency ticker data.
87
     * example "ETH".
88
     *
89
     * @param string $symbol
90
     *
91
     * @throws \GuzzleHttp\Exception\GuzzleException
92
     *
93
     * @return null|array
94
     */
95
    public function getTickerBySymbol(string $symbol): ?array
96
    {
97
        $listings = $this->getListings();
98
        if (!empty($listings)) {
99
            $item = collect($listings)->firstWhere('symbol', $symbol);
100
101
            if (!empty($item)) {
102
                $id = array_get($item, 'id');
103
                $response = $this->doRequest('GET', "ticker/{$id}");
104
105
                return array_get($response, 'data');
106
            }
107
        }
108
109
        return null;
110
    }
111
112
    /**
113
     * This methods gets all active cryptocurrency listings in one call.
114
     * Use the "id" field on the Ticker endpoint to query more information on a specific cryptocurrency.
115
     *
116
     * @throws \GuzzleHttp\Exception\GuzzleException
117
     *
118
     * @return null|array
119
     */
120
    public function getListings(): ?array
121
    {
122
        $response = $this->doRequest('GET', 'listings');
123
124
        return array_get($response, 'data');
125
    }
126
127
    /**
128
     * Generate Cache key.
129
     *
130
     * @param string $method
131
     * @param string $url
132
     * @param array  $query
133
     *
134
     * @return string
135
     */
136
    protected function getCacheKey(string $method, string $url, array $query = []): string
137
    {
138
        $query = json_encode($query);
139
        $prefix = config('services.coinmarketcap.cache.prefix', 'coinmarkeycap');
140
141
        return "{$prefix}:{$url}:{$method}:{$query}";
142
    }
143
}
144