Completed
Push — development ( d1d4a0...e79b55 )
by Sebastian
02:47
created

BitcoinWrapper::is_testnet()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
crap 12
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
    if (!(parent::getwalletinfo()['walletname']))
0 ignored issues
show
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...
28
      return $this->memcache->setCache(__FUNCTION__, parent::getinfo(), 30);
29
    else
30
      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...
31
  }
32
33
  public function is_testnet() {
34
    $this->oDebug->append("STA " . __METHOD__, 4);
35
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
36
    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...
37
      return $this->memcache->setCache(__FUNCTION__, parent::is_testnet(), 30);
38
    else
39
      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...
40
  }
41
  
42
  public function getmininginfo() {
43
    $this->oDebug->append("STA " . __METHOD__, 4);
44
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
45
    return $this->memcache->setCache(__FUNCTION__, parent::getmininginfo(), 30);
46
  }
47
48
  public function getblockcount() {
49
    $this->oDebug->append("STA " . __METHOD__, 4);
50
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
51
    return $this->memcache->setCache(__FUNCTION__, parent::getblockcount(), 30);
52
  }
53
  // Wrapper method to get the real main account balance
54
  public function getrealbalance() {
55
    $this->oDebug->append("STA " . __METHOD__, 4);
56
    $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...
57
    $dBalance = 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...
58
    // Account checks
59
    if (count($aAccounts) == 1) {
60
      // We only have a single account so getbalance will be fine
61
      return $dBalance;
62
    } else {
63
      $dMainBalance = $aAccounts[''];
64
      return $dMainBalance;
65
    }
66
  }
67
  public function getdifficulty() {
68
    $this->oDebug->append("STA " . __METHOD__, 4);
69
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
70
    $data = parent::getdifficulty();
71
    // Check for PoS/PoW coins
72
    if (is_array($data) && array_key_exists('proof-of-work', $data))
73
      $data = $data['proof-of-work'];
74
    return $this->memcache->setCache(__FUNCTION__, $data, 30);
75
  }
76
  public function getestimatedtime($iCurrentPoolHashrate) {
77
    $this->oDebug->append("STA " . __METHOD__, 4);
78
    if ($iCurrentPoolHashrate == 0) return 0;
79
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
80
    $dDifficulty = $this->getdifficulty();
81
    return $this->memcache->setCache(__FUNCTION__, $dDifficulty * pow(2,32) / $iCurrentPoolHashrate, 30);
82
  }
83
  public function getblockchaindownload() {
84
    $aPeerInfo = $this->getpeerinfo();
85
    $aInfo = $this->getinfo();
86
    $iStartingHeight = 0;
87
    foreach ($aPeerInfo as $aPeerData) {
88
      if ($iStartingHeight < $aPeerData['startingheight']) $iStartingHeight = $aPeerData['startingheight'];
89
    }
90
    if ($iStartingHeight > $aInfo['blocks']) {
91
      return number_format(round($aInfo['blocks'] / $iStartingHeight * 100, 2), 2);
92
    } else {
93
      return false;
94
    }
95
  }
96
  public function getnetworkhashps() {
97
    $this->oDebug->append("STA " . __METHOD__, 4);
98
    if ($data = $this->memcache->get(__FUNCTION__)) return $data;
99
    try {
100
      $dNetworkHashrate = $this->getmininginfo();
101
      if (is_array($dNetworkHashrate)) {
102
        if (array_key_exists('networkhashps', $dNetworkHashrate)) {
103
          $dNetworkHashrate = $dNetworkHashrate['networkhashps'];
104
        } else if (array_key_exists('networkmhps', $dNetworkHashrate)) {
105
          $dNetworkHashrate = $dNetworkHashrate['networkmhps'] * 1000 * 1000;
106
        } else if (array_key_exists('networkghps', $dNetworkHashrate)) {
107
          $dNetworkHashrate = $dNetworkHashrate['networkghps'] * 1000 * 1000 * 1000;
108
        } else if (array_key_exists('hashespersec', $dNetworkHashrate)) {
109
          $dNetworkHashrate = $dNetworkHashrate['hashespersec'];
110
        } else if (array_key_exists('netmhashps', $dNetworkHashrate)) {
111
          $dNetworkHashrate = $dNetworkHashrate['netmhashps'] * 1000 * 1000;
112
        } else {
113
          // Unsupported implementation
114
          $dNetworkHashrate = 0;
115
        }
116
      }
117
    } catch (Exception $e) {
118
      // getmininginfo does not exist, cache for an hour
119
      return $this->memcache->setCache(__FUNCTION__, 0, 3600);
120
    }
121
    return $this->memcache->setCache(__FUNCTION__, $dNetworkHashrate, 30);
122
  }
123
}
124
125
// Load this wrapper
126
$bitcoin = new BitcoinWrapper($config['wallet']['type'], $config['wallet']['username'], $config['wallet']['password'], $config['wallet']['host'], $config['DEBUG'], $debug, $memcache);
127