1 | <?php |
||
2 | |||
3 | namespace Multicoin\Api; |
||
4 | |||
5 | use Http\Client\Common\Plugin\AuthenticationPlugin; |
||
6 | use Http\Client\Common\Plugin\DecoderPlugin; |
||
7 | use Http\Client\Common\Plugin\ErrorPlugin; |
||
8 | use Http\Client\Common\Plugin\HeaderSetPlugin; |
||
9 | use Http\Client\Common\Plugin\QueryDefaultsPlugin; |
||
10 | use Http\Client\Common\Plugin\RetryPlugin; |
||
11 | use Http\Message\Authentication\Bearer; |
||
12 | use Multicoin\Api\Service\ApiClient; |
||
13 | use Multicoin\Api\Traits\Address; |
||
14 | use Multicoin\Api\Traits\Currency; |
||
15 | use Multicoin\Api\Traits\Invoice; |
||
16 | use Multicoin\Api\Traits\Transaction; |
||
17 | use Multicoin\Api\Traits\User; |
||
18 | |||
19 | class Multicoin |
||
20 | { |
||
21 | use Address; |
||
22 | use Invoice; |
||
23 | use Transaction; |
||
24 | use User; |
||
25 | use Currency; |
||
26 | |||
27 | public $coin; |
||
28 | protected $client; |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
29 | |||
30 | protected $config; |
||
0 ignored issues
–
show
|
|||
31 | |||
32 | public function __construct(array $config = [], $client = null) |
||
33 | { |
||
34 | $this->config = $config; |
||
35 | $this->coin = $config['coin']; |
||
36 | $this->client = $client ?: $this->setClient(); |
||
37 | // $this->setAuth($this->config['key']); |
||
38 | // parent::__construct($this->setUrl($this->config['url'])); |
||
39 | } |
||
40 | |||
41 | public function buildQueryParam(array $default, array $param = []) |
||
42 | { |
||
43 | //$data = array_filter(array_merge($default, $param), 'strlen'); |
||
44 | $params = array_merge($default, $param); |
||
45 | |||
46 | return http_build_query($params); |
||
47 | } |
||
48 | |||
49 | public function buildUrl($url) |
||
50 | { |
||
51 | return '/'.trim($this->coin, '/').$url; |
||
52 | } |
||
53 | |||
54 | public function setAuth($apiKey = null) |
||
55 | { |
||
56 | if (null === $apiKey) { |
||
57 | $apiKey = config('multicoin.api_token'); |
||
58 | } |
||
59 | |||
60 | $authentication = new Bearer($apiKey); |
||
61 | $authenticationPlugin = new AuthenticationPlugin($authentication); |
||
62 | |||
63 | return $authenticationPlugin; |
||
64 | |||
65 | return [ |
||
66 | $authenticationPlugin, |
||
67 | ]; |
||
68 | } |
||
69 | |||
70 | public function setClient() |
||
71 | { |
||
72 | $plugins = $this->setPlugins($this->config['api_token']); |
||
73 | $baseUrl = $this->setUrl($this->config['url']); |
||
74 | |||
75 | return new ApiClient($baseUrl, $plugins); |
||
76 | } |
||
77 | |||
78 | public function setPlugins($apiKey = null) |
||
79 | { |
||
80 | $auth = $this->setAuth($apiKey); |
||
81 | $decoderPlugin = new DecoderPlugin(); |
||
82 | $headerSetPlugin = new HeaderSetPlugin([ |
||
83 | 'Accept' => 'application/json', |
||
84 | ]); |
||
85 | $queryDefaultsPlugin = new QueryDefaultsPlugin([ |
||
86 | 'currency' => 'btc', |
||
87 | ]); |
||
88 | |||
89 | return [ |
||
90 | $auth, |
||
91 | $decoderPlugin, |
||
92 | $headerSetPlugin, |
||
93 | $queryDefaultsPlugin, |
||
94 | new RetryPlugin(), |
||
95 | new ErrorPlugin(), |
||
96 | ]; |
||
97 | } |
||
98 | |||
99 | public function setUrl($url = null) |
||
100 | { |
||
101 | if (null === $url) { |
||
102 | return config('multicoin.url'); |
||
103 | } |
||
104 | |||
105 | return $url; |
||
106 | } |
||
107 | } |
||
108 |