AbstractApi::setConfig()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 8
nop 1
1
<?php
2
/**
3
 * User: delboy1978uk
4
 * Date: 18/08/15
5
 * Time: 20:16
6
 */
7
8
namespace Del\Bitcoin\Api;
9
10
use GuzzleHttp\Client;
11
12
class AbstractApi
13
{
14
    /** @var array */
15
    private $config;
16
17
    /** @var Client */
18
    private  $client;
19
20
    /** @var string */
21
    private $host;
22
23
    public function __construct(array $config)
24
    {
25
        $this->setConfig($config);
26
    }
27
28
    /**
29
     * @param array $config
30
     * @return bool
31
     * @throws \Exception
32
     */
33
    public function setConfig(array $config)
34
    {
35
        $protocol = isset($config['protocol']) ? $config['protocol'] : 'http';
36
        $host = isset($config['host']) ? $config['host'] : '127.0.0.1';
37
        $port = isset($config['port']) ? $config['port'] : '8332';
38
        $this->config = $config;
39
        $this->host = $protocol . '://' . $host . ':' . $port;
40
        $this->client = new Client(['base_uri' => $this->host]);
41
        return $this;
42
    }
43
44
    /**
45
     * @return Client
46
     */
47
    private function getClient()
48
    {
49
        return $this->client;
50
    }
51
52
    /**
53
     * @param $uri
54
     * @param array $params
55
     * @return mixed
56
     */
57
    public function send($uri, $params = [])
58
    {
59
        /** @var \GuzzleHttp\Psr7\Response $response  */
60
        $response = $this->getClient()->post('/',[
61
            'auth' => [
62
                $this->config['username'],
63
                $this->config['password'],
64
            ],
65
            'headers' => [
66
                'Accept'     => 'application/json',
67
                'Content-Type' => 'application/json-rpc',
68
                'WWW-Authenticate' => 'Basic realm="jsonrpc"'
69
            ],
70
            'json' => [
71
                'method' => $uri,
72
                'params' => $params,
73
                'id' => 'phpbitcoin',
74
            ],
75
        ]);
76
        return $response->getBody();
77
    }
78
}