Passed
Branch main (0e1999)
by Pouya
04:43 queued 01:52
created

Receive::Get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
4
namespace Appino\Blockchain\Classes;
5
6
7
use Appino\Blockchain\Blockchain;
8
use Appino\Blockchain\Interfaces\Notification;
9
use Appino\Blockchain\Interfaces\Operation;
10
use Appino\Blockchain\Objects\LogResponse;
11
use Appino\Blockchain\Objects\NotificationResponse;
12
use Appino\Blockchain\Objects\ReceiveResponse;
13
use GuzzleHttp\Client;
14
15
class Receive{
16
17
    /**
18
     * @var array
19
     */
20
    private $params;
21
    const URL = 'https://api.blockchain.info/v2/receive';
22
23
24
    /**
25
     * Receive constructor.
26
     */
27
28
    public function __construct(){
29
        $this->client = new Client(['base_uri'=>self::URL]);
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
        $this->params = [
31
            'key'=>data_get('api_code',null)
32
        ];
33
    }
34
35
    private function Get($uri,$params=array()){
36
        return $this->client->get($uri,['query'=>$params])->getBody()->getContents();
37
    }
38
39
    private function Post($uri,$params=array()){
40
        return $this->client->post($uri,['form_params'=>$params])->getBody()->getContents();
41
    }
42
43
    /**
44
     * Generates a receive address.
45
     *
46
     * @param string $xpub The public key.
47
     * @param string $callback The callback URL.
48
     * @param int $gap_limit How many unused addresses are allowed.
49
     * @return ReceiveResponse
50
     */
51
    public function Generate($xpub, $callback, $gap_limit = 20){
52
        $params = [
53
            'xpub' => $xpub,
54
            'callback' => $callback,
55
            'gap_limit' => $gap_limit
56
        ];
57
        $params = array_merge($this->params, $params);
58
        $response = json_decode($this->Get('',['query'=>$params]),true);
59
        return new ReceiveResponse($response);
60
    }
61
62
    /**
63
     * Get the xpub gap
64
     *
65
     * @param $xpub string
66
     * @return integer
67
     * @throws \GuzzleHttp\Exception\GuzzleException
68
     */
69
    public function AddressGap($xpub){
70
        $params = array_merge(['xpub'=>$xpub],$this->params);
71
        $response = json_decode($this->Get('checkgap',['query'=>$params]),true);
72
        return $response['gap'];
73
    }
74
75
    /**
76
     * This method monitors an address of your choice for received and / or spent payments.
77
     * You will be sent an HTTP notification immediately when a transaction is made,
78
     * and subsequently when it reaches the number of confirmations specified in the request.
79
     *
80
     * @param string $address
81
     * @param string $callback
82
     * @param Notification|string $on
83
     * @param int $confs
84
     * @param Operation|string $op
85
     * @return NotificationResponse
86
     */
87
88
    public function BalanceNotification($address, $callback, $on = Notification::KEEP, $confs = 3, $op = Operation::ALL){
89
        $params = [
90
            'address' => $address,
91
            'callback' => $callback,
92
            'onNotification' => $on,
93
            'confs' => $confs,
94
            'op' => $op
95
        ];
96
        $params = array_merge($this->params, $params);
97
        $response = json_decode($this->Post('balance_update',['form_params'=>$params]),true);
98
        return new NotificationResponse($response);
99
    }
100
101
    /**
102
     * This method delete the balance notification
103
     *
104
     * @param $id int
105
     * @return bool
106
     * @throws \GuzzleHttp\Exception\GuzzleException
107
     */
108
109
    public function DeleteBalanceNotification($id){
110
       $response = json_decode($this->client->delete('balance_update/'.$id, ['query'=>$this->params])->getBody()->getContents(),true);
111
       return $response['deleted'];
112
    }
113
114
    /**
115
     * This method allows you to request callbacks when a new block of a specified height and confirmation number is added to the blockchain.
116
     *
117
     * @param $callback string
118
     * @param string $on
119
     * @param int $confs
120
     * @param int|null $height
121
     * @return NotificationResponse
122
     * @throws \GuzzleHttp\Exception\GuzzleException
123
     */
124
125
    public function BlockNotification($callback, $on = Notification::KEEP, $confs = 1, $height = null){
126
        $params = [
127
            'callback' => $callback,
128
            'onNotification' => $on,
129
            'confs' => $confs,
130
        ];
131
        if(!is_null($height))
132
            $params['height'] = $height;
133
        $params = array_merge($this->params, $params);
134
        $response = json_decode($this->Post('block_notification',['form_params'=>$params]),true);
135
        return new NotificationResponse($response);
136
    }
137
138
    /**
139
     * This method delete block notification
140
     *
141
     * @param $id int
142
     * @return bool
143
     * @throws \GuzzleHttp\Exception\GuzzleException
144
     */
145
146
    public function DeleteBlockNotification($id){
147
        $response = json_decode($this->client->delete('block_notification/'.$id,['query'=>$this->params])->getBody()->getContents(),true);
148
        return $response['deleted'];
149
    }
150
151
    /**
152
     * See logs related to callback attempts.
153
     *
154
     * @param $callback string
155
     * @return array<LogResponse>
156
     * @throws \GuzzleHttp\Exception\GuzzleException
157
     */
158
159
    public function CallbackLogs($callback){
160
        $params = array_merge(['callback'=>$callback],$this->params);
161
        $logs = json_decode($this->Get('callback_log',['query'=>$params]),true);
162
        $response = array();
163
        foreach ($logs as $log){
164
            $response[] = new LogResponse($log);
165
        }
166
        return $response;
167
    }
168
169
}
170