Passed
Push — master ( 5a8d0e...941bae )
by Vladymyr
01:28
created

Assets::getTimeseries()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 8
dl 0
loc 16
ccs 6
cts 6
cp 1
crap 2
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Codenixsv\MessariApi\Api;
6
7
use Exception;
8
9
/**
10
 * Class Assets
11
 * @package Codenixsv\MessariApi\Api
12
 */
13
class Assets extends Api
14
{
15
    /**
16
     * Get the paginated list of all assets and their metrics and profiles.
17
     *
18
     * @param bool $withMetrics existence of this query param filters assets to those with quantitative data
19
     * @param bool $withProfiles existence of this query param filters assets to those with qualitative data
20
     * @param string|null $fields pare down the returned fields (comma , separated, drill down with a slash /)
21
     * @param string|null $sort default sort is "marketcap desc", but the only valid value for this query
22
     *  param is "id" which translates to "id asc", which is useful for a stable sort while paginating
23
     * @param int|null $page Page number, starts at 1. Increment to paginate through
24
     *  results (until result is empty array)
25
     * @param int|null $limit default is 20, max is 500
26
     * @return array
27
     * @throws Exception
28
     */
29 2
    public function getAll(
30
        bool $withMetrics = false,
31
        bool $withProfiles = false,
32
        ?string $fields = null,
33
        ?string $sort = null,
34
        ?int  $page = null,
35
        ?int $limit = null
36
    ): array {
37 2
        $query = $this->buildQueryGetAll($withMetrics, $withProfiles, $fields, $sort, $page, $limit);
38 2
        $endpoint = empty($query) ? '/assets' : '/assets?' . $query;
39 2
        $response = $this->client->getBaseClient()->get($endpoint);
40
41 2
        return $this->transformer->transform($response);
42
    }
43
44
    /**
45
     * Get basic metadata for an asset.
46
     *
47
     * @param string $assetKey This "key" can be the asset's ID (unique), slug (unique), or symbol (non-unique)
48
     * @param string|null $fields pare down the returned fields (comma , separated, drill down with a slash /)
49
     * @return array
50
     * @throws Exception
51
     */
52 1
    public function get(string $assetKey, ?string $fields = null): array
53
    {
54 1
        $query = is_null($fields) ? '' : '?' . http_build_query(compact($fields));
55 1
        $response = $this->client->getBaseClient()->get('/assets/' . strtolower($assetKey) . $query);
56
57 1
        return $this->transformer->transform($response);
58
    }
59
60
    /**
61
     * Get all of our qualitative information for an asset.
62
     *
63
     * @param string $assetKey This "key" can be the asset's ID (unique), slug (unique), or symbol (non-unique)
64
     * @param string|null $fields pare down the returned fields (comma , separated, drill down with a slash /)
65
     * @return array
66
     * @throws Exception
67
     */
68 1
    public function getProfile(string $assetKey, ?string $fields = null): array
69
    {
70 1
        $query = is_null($fields) ? '' : '?' . http_build_query(compact($fields));
71 1
        $response = $this->client->getBaseClient()->get('/assets/' . strtolower($assetKey) . '/profile' . $query);
72
73 1
        return $this->transformer->transform($response);
74
    }
75
76
    /**
77
     * Get all of our quantitative metrics for an asset.
78
     *
79
     * @param string $assetKey This "key" can be the asset's ID (unique), slug (unique), or symbol (non-unique)
80
     * @param string|null $fields pare down the returned fields (comma , separated, drill down with a slash /)
81
     * @return array
82
     * @throws Exception
83
     */
84 1
    public function getMetrics(string $assetKey, ?string $fields = null): array
85
    {
86 1
        $query = is_null($fields) ? '' : '?' . http_build_query(compact($fields));
87 1
        $response = $this->client->getBaseClient()->get('/assets/' . strtolower($assetKey) . '/metrics' . $query);
88
89 1
        return $this->transformer->transform($response);
90
    }
91
92
    /**
93
     * Get the latest market data for an asset. This data is also included in the metrics endpoint, but if all you need
94
     *  is market-data, use this.
95
     *
96
     * @param string $assetKey This "key" can be the asset's ID (unique), slug (unique), or symbol (non-unique)
97
     * @param string|null $fields pare down the returned fields (comma , separated, drill down with a slash /)
98
     * @return array
99
     * @throws Exception
100
     */
101 1
    public function getMarketData(string $assetKey, ?string $fields = null): array
102
    {
103 1
        $query = is_null($fields) ? '' : '?' . http_build_query(compact($fields));
104 1
        $response = $this->client->getBaseClient()->get('/assets/' . strtolower($assetKey) . '/metrics'
105 1
            . '/market-data' . $query);
106
107 1
        return $this->transformer->transform($response);
108
    }
109
110
    /**
111
     * Retrieve historical timeseries data for an asset.
112
     *
113
     * @param string $assetKey This "key" can be the asset's ID (unique), slug (unique), or symbol (non-unique)
114
     * @param string $metricID The metricID is a unique identifier which describes the type of data returned by
115
     *  time-series endpoints.
116
     * @param string|null $start The "start" query parameter can be used to set the start date after which points
117
     *  are returned.
118
     * @param string|null $end The "end" query parameter can be used to set the end date after which no more points
119
     *  will be returned.
120
     * @param string|null $interval Defines what interval the resulting points will be returned in.
121
     * @param string|null $columns A comma separated list of strings that controls which columns will be returned and
122
     *  in what order.
123
     * @param string|null $order Order controls whether points in the response are returned in ascending or
124
     *  descending order.
125
     * @param string|null $format Specify format = csv to download data as CSV.
126
     * @return array
127
     * @throws Exception
128
     */
129 2
    public function getTimeseries(
130
        string $assetKey,
131
        string $metricID,
132
        ?string $start = null,
133
        ?string $end = null,
134
        ?string $interval = null,
135
        ?string $columns = null,
136
        ?string $order = null,
137
        ?string $format = null
138
    ): array {
139 2
        $query = $this->buildQueryGetTimeseries($start, $end, $interval, $columns, $order, $format);
140 2
        $query = empty($query) ? $query : ('?' . $query);
141 2
        $response = $this->client->getBaseClient()->get('/assets/' . strtolower($assetKey) . '/metrics/'
142 2
        . $metricID . '/time-series' . $query);
143
144 2
        return $this->transformer->transform($response);
145
    }
146
147
    /**
148
     * @param bool $withMetrics
149
     * @param bool $withProfiles
150
     * @param string|null $fields
151
     * @param string|null $sort
152
     * @param int|null $page
153
     * @param int|null $limit
154
     * @return string
155
     */
156 2
    private function buildQueryGetAll(
157
        bool $withMetrics = false,
158
        bool $withProfiles = false,
159
        ?string $fields = null,
160
        ?string $sort = null,
161
        ?int  $page = null,
162
        ?int $limit = null
163
    ): string {
164 2
        $data = compact('fields', 'sort', 'page', 'limit');
165 2
        if (true === $withMetrics) {
166 1
            $data['with-metrics'] = '';
167
        }
168 2
        if (true === $withProfiles) {
169 1
            $data['with-profiles'] = '';
170
        }
171
172
        $params = array_filter($data, function ($value) {
173 2
            return !is_null($value);
174 2
        });
175
176 2
        return http_build_query($params);
177
    }
178
179
    /**
180
     * @param string|null $start
181
     * @param string|null $end
182
     * @param string|null $interval
183
     * @param string|null $columns
184
     * @param string|null $order
185
     * @param string|null $format
186
     * @return string
187
     */
188 2
    private function buildQueryGetTimeseries(
189
        ?string $start = null,
190
        ?string $end = null,
191
        ?string $interval = null,
192
        ?string $columns = null,
193
        ?string $order = null,
194
        ?string $format = null
195
    ): string {
196 2
        $data = compact('start', 'end', 'interval', 'columns', 'order', 'format');
197
198
        $params = array_filter($data, function ($value) {
199 2
            return !is_null($value);
200 2
        });
201
202 2
        return http_build_query($params);
203
    }
204
}
205