Completed
Push — master ( 9911e2...5a8d0e )
by Vladymyr
02:28
created

Api::buildQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Codenixsv\MessariApi\Api;
6
7
use Codenixsv\MessariApi\Message\ResponseTransformer;
8
use Codenixsv\MessariApi\MessariClient;
9
use Codenixsv\MessariApi\Exception\TransformResponseException;
10
11
/**
12
 * Class Api
13
 * @package Codenixsv\MessariApi\Api
14
 */
15
class Api
16
{
17
    /** @var MessariClient  */
18
    private $client;
19
20
    /** @var ResponseTransformer  */
21
    private $transformer;
22
23
    /**
24
     * Api constructor.
25
     * @param $client
26
     */
27 10
    public function __construct(MessariClient $client)
28
    {
29 10
        $this->client = $client;
30 10
        $this->transformer = new ResponseTransformer();
31 10
    }
32
33
    /**
34
     * @return MessariClient
35
     */
36 9
    public function getClient(): MessariClient
37
    {
38 9
        return $this->client;
39
    }
40
41
    /**
42
     * @return array
43
     * @throws TransformResponseException
44
     */
45 1
    public function getAllMarkets(): array
46
    {
47 1
        $response = $this->client->getBaseClient()->get('/markets');
48
49 1
        return $this->transformer->transform($response);
50
    }
51
52
    /**
53
     * @param bool $withMetrics
54
     * @param bool $withProfiles
55
     * @return array
56
     * @throws TransformResponseException
57
     */
58 4
    public function getAllAssets(bool $withMetrics = false, bool $withProfiles = false): array
59
    {
60 4
        $queryData = [];
61 4
        if (true === $withMetrics) {
62 2
            $queryData[] = 'with-metrics';
63
        }
64 4
        if (true === $withProfiles) {
65 2
            $queryData[] = 'with-profiles';
66
        }
67 4
        $response = $this->client->getBaseClient()->get('/assets' . $this->buildQuery($queryData));
68
69 4
        return $this->transformer->transform($response);
70
    }
71
72
    /**
73
     * @param string $symbol
74
     * @return array
75
     * @throws TransformResponseException
76
     */
77 1
    public function getProfileBySymbol(string $symbol): array
78
    {
79 1
        $response = $this->client->getBaseClient()->get('/assets/' . strtolower($symbol) . '/profile');
80
81 1
        return $this->transformer->transform($response);
82
    }
83
84
    /**
85
     * @param string $symbol
86
     * @return array
87
     * @throws TransformResponseException
88
     */
89 1
    public function getMetricsBySymbol(string $symbol): array
90
    {
91 1
        $response = $this->client->getBaseClient()->get('/assets/' . strtolower($symbol) . '/metrics');
92
93 1
        return $this->transformer->transform($response);
94
    }
95
96
    /**
97
     * @return array
98
     * @throws TransformResponseException
99
     */
100 1
    public function getNews(): array
101
    {
102 1
        $response = $this->client->getBaseClient()->get('/news');
103
104 1
        return $this->transformer->transform($response);
105
    }
106
107
    /**
108
     * @param string $symbol
109
     * @return array
110
     * @throws TransformResponseException
111
     */
112 1
    public function getNewsBySymbol(string $symbol): array
113
    {
114 1
        $response = $this->client->getBaseClient()->get('/news/' . strtolower($symbol));
115
116 1
        return $this->transformer->transform($response);
117
    }
118
119
    /**
120
     * @param array $queryData
121
     * @return string
122
     */
123 4
    private function buildQuery(array $queryData): string
124
    {
125 4
        if (empty($queryData)) {
126 1
            return '';
127
        }
128
129 3
        return '?' . implode('&', $queryData);
130
    }
131
}
132