Payout   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 58
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isPayoutActive() 0 6 6
B createPayout() 0 24 8
A setProcessed() 0 6 4
1
<?php
2
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
3
4
class Payout Extends Base {
5
  protected $table = 'payouts';
6
7
  /**
8
   * Check if the user has an active payout request already
9
   * @param account_id int Account ID
10
   * @return boolean bool True of False
11
   **/
12
  public function isPayoutActive($account_id) {
13
    $stmt = $this->mysqli->prepare("SELECT id FROM $this->table WHERE completed = 0 AND account_id = ? LIMIT 1");
14
    if ($stmt && $stmt->bind_param('i', $account_id) && $stmt->execute( )&& $stmt->store_result() && $stmt->num_rows > 0)
15
      return true;
16
    return $this->sqlError('E0048');
17
  }
18
19
  /**
20
   * Insert a new payout request
21
   * @param account_id int Account ID
22
   * @param strToken string Token to confirm
23
   * @return data mixed Inserted ID or false
24
   **/
25
  public function createPayout($account_id=NULL, $strToken) {
26
    $stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id) VALUES (?)");
27
    if ($stmt && $stmt->bind_param('i', $account_id) && $stmt->execute()) {
28
      $insert_id = $stmt->insert_id;
29
      // twofactor - consume the token if it is enabled and valid
30
      if ($this->config['twofactor']['enabled'] && $this->config['twofactor']['options']['withdraw']) {
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31
        $tValid = $this->token->isTokenValid($account_id, $strToken, 7);
0 ignored issues
show
Bug introduced by
The property token does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
        if ($tValid) {
33
          $delete = $this->token->deleteToken($strToken);
34
          if (!$delete) {
35
            $this->log->log("info", "User $account_id requested manual payout but failed to delete payout token");
36
            $this->setErrorMessage('Unable to delete token');
37
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Payout::createPayout of type data.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
38
          }
39
        } else {
40
          $this->log->log("info", "User $account_id requested manual payout using an invalid payout token");
41
          $this->setErrorMessage('Invalid token');
42
          return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Payout::createPayout of type data.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
43
        }
44
      }
45
      return $insert_id;
46
    }
47
    return $this->sqlError('E0049');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->sqlError('E0049'); (boolean) is incompatible with the return type documented by Payout::createPayout of type data.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
48
  }
49
50
  /**
51
   * Mark a payout as processed
52
   * @param id int Payout ID
53
   * @return boolean bool True or False
54
   **/
55
  public function setProcessed($id) {
56
    $stmt = $this->mysqli->prepare("UPDATE $this->table SET completed = 1 WHERE id = ? LIMIT 1");
57
    if ($stmt && $stmt->bind_param('i', $id) && $stmt->execute())
58
      return true;
59
    return $this->sqlError('E0051');
60
  }
61
}
62
63
$oPayout = new Payout();
64
$oPayout->setDebug($debug);
65
$oPayout->setLog($log);
66
$oPayout->setMysql($mysqli);
67
$oPayout->setConfig($config);
68
$oPayout->setToken($oToken);
69
$oPayout->setErrorCodes($aErrorCodes);
70