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

Markets::getAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Codenixsv\MessariApi\Api;
6
7
use Exception;
8
9
/**
10
 * Class Markets
11
 * @package Codenixsv\MessariApi\Api
12
 */
13
class Markets extends Api
14
{
15
    /**
16
     * @param int|null $page Page number, starts at 1. Increment to paginate through results (until
17
     *  result is empty array)
18
     * @param string|null $fields pare down the returned fields (comma , separated, drill down with a slash /)
19
     * @return array
20
     * @throws Exception
21
     */
22 2
    public function getAll(?int $page = null, ?string $fields = null): array
23
    {
24
        $params = array_filter(compact('fields', 'page'), function ($value) {
25 2
            return !is_null($value);
26 2
        });
27
28 2
        $query = http_build_query($params);
29 2
        $query = empty($query) ? $query : ('?' . $query);
30 2
        $response = $this->client->getBaseClient()->get('/markets' . $query);
31
32 2
        return $this->transformer->transform($response);
33
    }
34
35
    /**
36
     * Retrieve historical timeseries data for a market.
37
     *
38
     * @param string $assetKey This "key" can be the asset's ID (unique), slug (unique), or symbol (non-unique)
39
     * @param string $metricID The metricID is a unique identifier which describes the type of data returned by
40
     *  time-series endpoints.
41
     * @param string|null $start The "start" query parameter can be used to set the start date after which points
42
     *  are returned.
43
     * @param string|null $end The "end" query parameter can be used to set the end date after which no more points
44
     *  will be returned.
45
     * @param string|null $interval Defines what interval the resulting points will be returned in.
46
     * @param string|null $columns A comma separated list of strings that controls which columns will be returned and
47
     *  in what order.
48
     * @param string|null $order Order controls whether points in the response are returned in ascending or
49
     *  descending order.
50
     * @param string|null $format Specify format = csv to download data as CSV.
51
     * @return array
52
     * @throws Exception
53
     */
54 2
    public function getTimeseries(
55
        string $assetKey,
56
        string $metricID,
57
        ?string $start = null,
58
        ?string $end = null,
59
        ?string $interval = null,
60
        ?string $columns = null,
61
        ?string $order = null,
62
        ?string $format = null
63
    ): array {
64 2
        $query = $this->buildQueryGetTimeseries($start, $end, $interval, $columns, $order, $format);
65 2
        $query = empty($query) ? $query : ('?' . $query);
66 2
        $response = $this->client->getBaseClient()->get('/markets/' . strtolower($assetKey) . '/metrics/'
67 2
            . $metricID . '/time-series' . $query);
68
69 2
        return $this->transformer->transform($response);
70
    }
71
72
    /**
73
     * @param string|null $start
74
     * @param string|null $end
75
     * @param string|null $interval
76
     * @param string|null $columns
77
     * @param string|null $order
78
     * @param string|null $format
79
     * @return string
80
     */
81 2
    private function buildQueryGetTimeseries(
82
        ?string $start = null,
83
        ?string $end = null,
84
        ?string $interval = null,
85
        ?string $columns = null,
86
        ?string $order = null,
87
        ?string $format = null
88
    ): string {
89 2
        $data = compact('start', 'end', 'interval', 'columns', 'order', 'format');
90
91
        $params = array_filter($data, function ($value) {
92 2
            return !is_null($value);
93 2
        });
94
95 2
        return http_build_query($params);
96
    }
97
}
98