Receive::BlockNotification()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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