Completed
Push — master ( c44990...b4ec9a )
by Mikhail
01:07
created

BittrexClient   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 7
dl 0
loc 108
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setCredential() 0 5 1
A getKey() 0 4 1
A getSecret() 0 4 1
A public() 0 4 1
A market() 0 4 1
A account() 0 4 1
A createPublicClient() 0 4 1
A createPrivateClient() 0 10 3
A getPrivateClient() 0 4 2
A getPublicClient() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace R3bers\BittrexApi;
6
7
use GuzzleHttp\Client;
8
use GuzzleHttp\HandlerStack;
9
use R3bers\BittrexApi\Api\Account;
10
use R3bers\BittrexApi\Api\Market;
11
use R3bers\BittrexApi\Api\PublicApi;
12
use R3bers\BittrexApi\Exception\InvalidCredentialException;
13
use R3bers\BittrexApi\Middleware\Authentication;
14
15
class BittrexClient
16
{
17
    private const BASE_URI = 'https://api.bittrex.com';
18
19
    /** @var Client */
20
    private $publicClient;
21
22
    /** @var Client */
23
    private $privateClient;
24
25
    /** @var string */
26
    private $key = '';
27
28
    /** @var string  */
29
    private $secret = '';
30
31
    /**
32
     * @param string $key
33
     * @param string $secret
34
     */
35
    public function setCredential(string $key, string $secret): void
36
    {
37
        $this->key = $key;
38
        $this->secret = $secret;
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getKey(): string
45
    {
46
        return $this->key;
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getSecret(): string
53
    {
54
        return $this->secret;
55
    }
56
57
    /**
58
     * @return PublicApi
59
     */
60
    public function public(): PublicApi
61
    {
62
        return new PublicApi($this->getPublicClient());
63
    }
64
65
    /**
66
     * @return Market
67
     * @throws InvalidCredentialException
68
     */
69
    public function market(): Market
70
    {
71
        return new Market($this->getPrivateClient());
72
    }
73
74
    /**
75
     * @return Account
76
     * @throws InvalidCredentialException
77
     */
78
    public function account(): Account
79
    {
80
        return new Account($this->getPrivateClient());
81
    }
82
83
    /**
84
     * @return Client
85
     */
86
    private function createPublicClient(): Client
87
    {
88
        return new Client(['base_uri' => self::BASE_URI]);
89
    }
90
91
    /**
92
     * @return Client
93
     * @throws InvalidCredentialException
94
     */
95
    private function createPrivateClient(): Client
96
    {
97
        if (empty($this->key) || empty($this->secret)) {
98
            throw new InvalidCredentialException('Key and secret must be set for authenticated API');
99
        }
100
        $stack = HandlerStack::create();
101
        $stack->push(new Authentication($this->getKey(), $this->getSecret(), (string)time()));
102
103
        return new Client(['handler' => $stack, 'base_uri' => self::BASE_URI]);
104
    }
105
106
    /**
107
     * @return Client
108
     * @throws InvalidCredentialException
109
     */
110
    private function getPrivateClient(): Client
111
    {
112
        return $this->privateClient ?: $this->createPrivateClient();
113
    }
114
115
    /**
116
     * @return Client
117
     */
118
    private function getPublicClient(): Client
119
    {
120
        return $this->publicClient ?: $this->createPublicClient();
121
    }
122
}
123