Passed
Push — main ( ac53a4...aee26d )
by Pouya
06:40 queued 03:43
created

Blockchain::Market()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Appino\Blockchain\Classes;
4
5
use Appino\Blockchain\Classes\Create;
6
use Appino\Blockchain\Classes\Receive;
7
use Appino\Blockchain\Classes\Wallet;
8
use Appino\Blockchain\Exception\ApiError;
9
use Appino\Blockchain\Exception\Error;
10
use Appino\Blockchain\Exception\HttpError;
11
use Appino\Blockchain\Exception\ParameterError;
12
use GuzzleHttp\Client;
13
use GuzzleHttp\Exception\GuzzleException;
14
use Illuminate\Contracts\Container\Container;
15
16
class Blockchain{
17
18
    protected $client;
19
    protected $params;
20
    public $HD = false;
21
    const GET = 'GET';
22
    const POST = 'POST';
23
24
    public function __construct($config){
25
        $this->client = new Client(['base_uri'=>data_get($config,'base_uri')]);
26
        $this->params['api_code'] = data_get($config,'api_code');
27
        $this->params['key'] = data_get($config,'api_code');
28
    }
29
30
    /**
31
     * @return Create
32
     */
33
    public function Create(){
34
        return new Create($this);
35
    }
36
37
    /**
38
     * @return Wallet
39
     */
40
    public function Wallet(){
41
        return new Wallet($this);
42
    }
43
44
    /**
45
     * @return Receive
46
     */
47
    public function Receive(){
48
        return new Receive($this);
49
    }
50
51
    /**
52
     * @return Market
53
     */
54
    public function Market(){
55
        return new Market($this);
56
    }
57
58
    /**
59
     * @param $base_uri string Default : http://localhost:3000
60
     */
61
62
    public function newBaseUri($base_uri){
63
        $this->client = new Client(['base_uri'=>$base_uri]);
64
    }
65
66
    /**
67
     * @param $method
68
     * @param $url
69
     * @param $params
70
     * @return array
71
     * @throws HttpError
72
     */
73
74
    public function Request($method, $url, $params){
75
        $params = array_merge($params, $this->params);
76
        switch ($method){
77
            case 'GET':
78
            case 'DELETE':
79
                $options = array('query'=>$params);
80
                break;
81
            case 'POST':
82
                $options = array(
83
                    'form_params'=>$params,
84
                    'headers'=>[
85
                        'Content-Type'=>'application/x-www-form-urlencoded',
86
                    ]
87
                );
88
                break;
89
            default:
90
                throw new ParameterError("request method not set");
91
        }
92
        try {
93
            $response = $this->client->request($method, $url, $options);
94
            $json = json_decode($response->getBody()->getContents(),true);
95
            if(is_null($json)) {
96
                // this is possibly a from btc request with a comma separation
97
                $json = json_decode(str_replace(',', '', $response));
98
                if (is_null($json))
99
                    throw new Error("Unable to decode JSON response from Blockchain: " . $response->getBody()->getContents());
100
            }
101
            if(array_key_exists('error', $json)) {
102
                throw new ApiError($json['error']);
103
            }
104
            return $json;
105
        } catch (GuzzleException $e) {
106
            echo $e->getMessage();
107
        }
108
    }
109
110
}
111