Passed
Push — main ( b86878...e52c85 )
by Pouya
05:24
created

Receive::call()   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
nc 1
nop 3
dl 0
loc 2
rs 10
c 0
b 0
f 0
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){
0 ignored issues
show
Unused Code introduced by
The parameter $blockchain is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

35
    public function __construct(/** @scrutinizer ignore-unused */ Blockchain $blockchain){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

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