Binance   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 122
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getHistoricalPrices() 0 2 1
A sellOrder() 0 19 1
A buyOrder() 0 18 1
A getBalance() 0 20 3
A getPrice() 0 9 1
A __construct() 0 3 1
A getName() 0 3 1
1
<?php
2
3
4
namespace Mokka\Exchange\Market;
5
6
use GuzzleHttp\Client;
7
use Mokka\Action\BuyAction;
8
use Mokka\Action\SellAction;
9
use Mokka\Exchange\ExchangeInterface;
10
11
class Binance implements ExchangeInterface
12
{
13
14
    /**
15
     * @var
16
     */
17
    private static $apiService;
18
19
    public function __construct($config)
20
    {
21
        self::$apiService = new BinanceApiService($config['api_key'], $config['api_secret']);
22
    }
23
24
    /**
25
     * Get symbol price from source
26
     * @param $symbol
27
     * @return float
28
     */
29
    public function getPrice($symbol) : float
30
    {
31
        $client = new Client();
32
33
        $response = $client->get("https://api.binance.com/api/v3/ticker/price?symbol={$symbol}");
34
35
        $response = json_decode($response->getBody()->getContents(), TRUE);
36
37
        return (float) $response['price'];
38
    }
39
40
    /**
41
     * Get historical prices
42
     * @param $symbol
43
     * @return array
44
     */
45
    public function getHistoricalPrices($symbol): array
46
    {
47
        // TODO: Implement getHistoricalPrices() method.
48
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return array. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
49
50
    /**
51
     * Put buy order
52
     * @param BuyAction $action
53
     * @return mixed|\Psr\Http\Message\ResponseInterface
54
     */
55
    public function buyOrder(BuyAction $action)
56
    {
57
58
        $getServerTime = self::$apiService->getServerTime()->getBody()->getContents();
59
        $getServerTime = json_decode($getServerTime, true);
60
61
        $params = [
62
            'symbol' => $action->getSymbol(),
63
            'price' => $action->getActionPrice(),
64
            'quantity' => round($action->getQuantity(), 2),
65
            'side'  => 'BUY',
66
            'type'  => 'LIMIT',
67
            'timeInForce' => 'GTC',
68
            'recvWindow' => 5000,
69
            'timestamp' => $getServerTime['serverTime']
70
        ];
71
72
        return self::$apiService->postOrderTest($params);
73
    }
74
75
    /**
76
     * Put sell order
77
     * @param SellAction $action
78
     * @return mixed
79
     */
80
    public function sellOrder(SellAction $action)
81
    {
82
83
        $getServerTime = self::$apiService->getServerTime()->getBody()->getContents();
84
        $getServerTime = json_decode($getServerTime, true);
85
86
        $params = [
87
            'symbol' => $action->getSymbol(),
88
            'price' => $action->getActionPrice(),
89
            'quantity' => $action->getQuantity(),
90
            'side'  => 'SELL',
91
            'type'  => 'LIMIT',
92
            'timeInForce' => 'GTC',
93
            'recvWindow' => 10000000,
94
            'timestamp' => $getServerTime['serverTime']
95
96
        ];
97
98
        return self::$apiService->postOrderTest($params);
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getName()
105
    {
106
        return 'binance';
107
    }
108
109
110
    /**
111
     * @return mixed
112
     */
113
    public function getBalance()
114
    {
115
        //get server time. dont use user local machine time
116
        $client = new Client();
117
        $response = $client->get("https://api.binance.com/api/v1/time");
118
        $response = json_decode($response->getBody()->getContents(), TRUE);
119
120
        $account = self::$apiService->getAccount(['timestamp' => $response['serverTime']]);
121
        $account = json_decode($account->getBody()->getContents(), TRUE);
122
123
        $balance = null;
124
        //catch BTC balance
125
        foreach ($account['balances'] as $item) {
126
            if ($item['asset'] == 'BTC') {
127
                $balance = $item['free'];
128
                break;
129
            }
130
        }
131
132
        return $balance;
133
134
    }
135
}