Completed
Branch master (56c2f5)
by
unknown
07:05
created

BlocktrailUnspentOutputFinder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 8.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 8
loc 96
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setPaginationLimit() 0 3 1
A getUTXOs() 0 17 4
B getUnspentOutputs() 8 38 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Blocktrail\SDK\Services;
4
5
use Blocktrail\SDK\BlocktrailSDK;
6
use Blocktrail\SDK\UnspentOutputFinder;
7
8
class BlocktrailUnspentOutputFinder extends UnspentOutputFinder {
9
10
    protected $client;
11
    protected $retryLimit;
12
    protected $sleepTime;
13
    protected $retries;
14
    protected $paginationLimit = 500;   //max results to retrieve at a time
15
16
    /**
17
     * @param        $apiKey
18
     * @param        $apiSecret
19
     * @param string $network
20
     * @param bool   $testnet
21
     * @param string $apiVersion
22
     * @param null   $apiEndpoint
23
     */
24
    public function __construct($apiKey, $apiSecret, $network = 'BTC', $testnet = false, $apiVersion = 'v1', $apiEndpoint = null) {
25
        $this->client = new BlocktrailSDK($apiKey, $apiSecret, $network, $testnet, $apiVersion, $apiEndpoint);
26
27
        $this->retryLimit = 5;
28
        $this->sleepTime = 20;
29
        $this->retries = 0;
30
    }
31
32
    /**
33
     * modify the default limit on how many utxo results are returned per page
34
     * @param $limit
35
     */
36
    public function setPaginationLimit($limit) {
37
        $this->paginationLimit = $limit;
38
    }
39
40
    public function getUTXOs(array $addresses) {
41
        $results = array();
42
43
        foreach ($addresses as $address) {
44
            if ($this->debug) {
45
                echo "\nchecking $address";
46
            }
47
            //get the utxos for this address
48
            $utxos = $this->getUnspentOutputs($address);
49
50
            if (count($utxos) > 0) {
51
                $results = array_merge($results, $utxos);
52
            }
53
        }
54
55
        return $results;
56
    }
57
58
    /**
59
     * gets unspent outputs for an address, returning and array of outputs with hash, index, value, and script pub hex
60
     *
61
     * @param $address
62
     * @return array        2d array of unspent outputs as ['hash' => $hash, 'index' => $index, 'value' => $value, 'script_hex' => $scriptHex]
63
     * @throws \Exception
64
     */
65
    protected function getUnspentOutputs($address) {
66
        //get unspent outputs for the address - required data: hash, index, value, and script hex
67
        $utxos = array();
68
        try {
69
            $page = 1;
70
            do {
71
                $results = $this->client->addressUnspentOutputs($address, $page, $this->paginationLimit);
72
                $utxos = array_merge($utxos, $results['data']);
73
                $page++;
74
            } while (count($results['data']) > 0);
75
76
        } catch (\Exception $e) {
77
            //if rate limit hit, sleep for a short while and try again
78
            if ($this->retries < $this->retryLimit) {
79
                $this->retries++;
80
                sleep($this->sleepTime);
81
82
                return $this->getUnspentOutputs($address);
83
            } else {
84
                throw $e;
85
            }
86
        }
87
88
        //reset retry count
89
        $this->retries = 0;
90
91
        //reduce the returned data into the values we're interested in
92 View Code Duplication
        $result = array_map(function ($utxo) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
            return array(
94
                'hash'       => $utxo['hash'],
95
                'index'      => $utxo['index'],
96
                'value'      => $utxo['value'],
97
                'script_hex' => $utxo['script_hex'],
98
            );
99
        }, $utxos);
100
101
        return $result;
102
    }
103
}
104