BitcoinWrapper   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 36
lcom 0
cbo 1
dl 0
loc 124
ccs 0
cts 106
cp 0
rs 9.52
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getinfo() 0 10 3
A is_testnet() 0 8 3
A getmininginfo() 0 5 2
A getblockcount() 0 5 2
A getrealbalance() 0 19 4
A getdifficulty() 0 9 4
A getestimatedtime() 0 7 3
A getblockchaindownload() 0 13 4
B getnetworkhashps() 0 27 9
1
<?php
2
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
3
4
/**
5
 * We use a wrapper class around BitcoinClient to add
6
 * some basic caching functionality and some debugging
7
 **/
8
class BitcoinWrapper extends BitcoinClient {
9
  public function __construct($type, $username, $password, $host, $debug_level, $debug_object, $memcache) {
10
    $this->type = $type;
11
    $this->username = $username;
12
    $this->password = $password;
13
    $this->host = $host;
14
    // $this->debug is already used
15
    $this->oDebug = $debug_object;
16
    $this->memcache = $memcache;
17
    $debug_level > 0 ? $debug_level = true : $debug_level = false;
18
    return parent::__construct($this->type, $this->username, $this->password, $this->host, '', $debug_level);
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
19
  }
20
  /**
21
   * Wrap variouns methods to add caching
22
   **/
23
  // Caching this, used for each can_connect call
24
  public function getinfo() {
25
    $this->oDebug->append("STA " . __METHOD__, 4);
26
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
27
    try {
28
      return $this->memcache->setCache(__FUNCTION__, parent::getnetworkinfo()+parent::getmininginfo()+parent::getwalletinfo(), 30);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getnetworkinfo() instead of getinfo()). Are you sure this is correct? If so, you might want to change this to $this->getnetworkinfo().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getmininginfo() instead of getinfo()). Are you sure this is correct? If so, you might want to change this to $this->getmininginfo().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getwalletinfo() instead of getinfo()). Are you sure this is correct? If so, you might want to change this to $this->getwalletinfo().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
29
    } catch (Exception $e) {
30
      $this->oDebug->append("DEPRECATED : RPC version < 0.16, fallback to `getinfo` RPC call", 2);
31
      return $this->memcache->setCache(__FUNCTION__, parent::getinfo(), 30);
32
    }
33
  }
34
35
  public function is_testnet() {
36
    $this->oDebug->append("STA " . __METHOD__, 4);
37
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
38
    if (!(parent::getblockchaininfo()))
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getblockchaininfo() instead of is_testnet()). Are you sure this is correct? If so, you might want to change this to $this->getblockchaininfo().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
39
      return $this->memcache->setCache(__FUNCTION__, parent::is_testnet(), 30);
40
    else
41
      return $this->memcache->setCache(__FUNCTION__, parent::getblockchaininfo()['chain'] == 'test', 30);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getblockchaininfo() instead of is_testnet()). Are you sure this is correct? If so, you might want to change this to $this->getblockchaininfo().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
42
  }
43
44
  public function getmininginfo() {
45
    $this->oDebug->append("STA " . __METHOD__, 4);
46
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
47
    return $this->memcache->setCache(__FUNCTION__, parent::getmininginfo(), 30);
48
  }
49
50
  public function getblockcount() {
51
    $this->oDebug->append("STA " . __METHOD__, 4);
52
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
53
    return $this->memcache->setCache(__FUNCTION__, parent::getblockcount(), 30);
54
  }
55
  // Wrapper method to get the real main account balance
56
  public function getrealbalance() {
57
    $this->oDebug->append("STA " . __METHOD__, 4);
58
    $aAccounts = [];
59
60
    try {
61
      $aAccounts = parent::listaccounts();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (listaccounts() instead of getrealbalance()). Are you sure this is correct? If so, you might want to change this to $this->listaccounts().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
62
    } catch (Exception $e) {
63
      if ($e->getCode() == 404)
64
        $aAccounts = array( '*' => parent::getbalance("*") );
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getbalance() instead of getrealbalance()). Are you sure this is correct? If so, you might want to change this to $this->getbalance().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
65
    }
66
67
    // Account checks
68
    if (count($aAccounts) == 1) {
69
      // We only have a single account so getbalance will be fine
70
      return parent::getbalance("*");
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getbalance() instead of getrealbalance()). Are you sure this is correct? If so, you might want to change this to $this->getbalance().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
71
    } else {
72
      return $aAccounts[0];
73
    }
74
  }
75
  public function getdifficulty() {
76
    $this->oDebug->append("STA " . __METHOD__, 4);
77
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
78
    $data = parent::getdifficulty();
79
    // Check for PoS/PoW coins
80
    if (is_array($data) && array_key_exists('proof-of-work', $data))
81
      $data = $data['proof-of-work'];
82
    return $this->memcache->setCache(__FUNCTION__, $data, 30);
83
  }
84
  public function getestimatedtime($iCurrentPoolHashrate) {
85
    $this->oDebug->append("STA " . __METHOD__, 4);
86
    if ($iCurrentPoolHashrate == 0) return 0;
87
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
88
    $dDifficulty = $this->getdifficulty();
89
    return $this->memcache->setCache(__FUNCTION__, $dDifficulty * pow(2,32) / $iCurrentPoolHashrate, 30);
90
  }
91
  public function getblockchaindownload() {
92
    $aPeerInfo = $this->getpeerinfo();
93
    $aInfo = $this->getinfo();
94
    $iStartingHeight = 0;
95
    foreach ($aPeerInfo as $aPeerData) {
96
      if ($iStartingHeight < $aPeerData['startingheight']) $iStartingHeight = $aPeerData['startingheight'];
97
    }
98
    if ($iStartingHeight > $aInfo['blocks']) {
99
      return number_format(round($aInfo['blocks'] / $iStartingHeight * 100, 2), 2);
100
    } else {
101
      return false;
102
    }
103
  }
104
  public function getnetworkhashps() {
105
    $this->oDebug->append("STA " . __METHOD__, 4);
106
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
107
    try {
108
      $dNetworkHashrate = $this->getmininginfo();
109
      if (is_array($dNetworkHashrate)) {
110
        if (array_key_exists('networkhashps', $dNetworkHashrate)) {
111
          $dNetworkHashrate = $dNetworkHashrate['networkhashps'];
112
        } else if (array_key_exists('networkmhps', $dNetworkHashrate)) {
113
          $dNetworkHashrate = $dNetworkHashrate['networkmhps'] * 1000 * 1000;
114
        } else if (array_key_exists('networkghps', $dNetworkHashrate)) {
115
          $dNetworkHashrate = $dNetworkHashrate['networkghps'] * 1000 * 1000 * 1000;
116
        } else if (array_key_exists('hashespersec', $dNetworkHashrate)) {
117
          $dNetworkHashrate = $dNetworkHashrate['hashespersec'];
118
        } else if (array_key_exists('netmhashps', $dNetworkHashrate)) {
119
          $dNetworkHashrate = $dNetworkHashrate['netmhashps'] * 1000 * 1000;
120
        } else {
121
          // Unsupported implementation
122
          $dNetworkHashrate = 0;
123
        }
124
      }
125
    } catch (Exception $e) {
126
      // getmininginfo does not exist, cache for an hour
127
      return $this->memcache->setCache(__FUNCTION__, 0, 3600);
128
    }
129
    return $this->memcache->setCache(__FUNCTION__, $dNetworkHashrate, 30);
130
  }
131
}
132
133
// Load this wrapper
134
$bitcoin = new BitcoinWrapper($config['wallet']['type'], $config['wallet']['username'], $config['wallet']['password'], $config['wallet']['host'], $config['DEBUG'], $debug, $memcache);
135