Invitation::getInvitations()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.6111
c 0
b 0
f 0
cc 5
nc 2
nop 1
crap 30
1
<?php
2
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
3
4
class Invitation extends Base {
5
  var $table = 'invitations';
6
7
  /**
8
   * Fetch invitations for one account
9
   * @param account_id int Account ID
10
   * @return mixed Array on success, bool on failure
11
   **/
12
  public function getInvitations($account_id) {
13
    $this->debug->append("STA " . __METHOD__, 4);
14
    $stmt = $this->mysqli->prepare("SELECT * FROM $this->table WHERE account_id = ?");
15
    if ($stmt && $stmt->bind_param('i', $account_id) && $stmt->execute() && $result = $stmt->get_result())
16
      return $result->fetch_all(MYSQLI_ASSOC);
17
    $this->sqlError('E0021');
18
  }
19
20
  /**
21
   * Count invitations sent by an account_id
22
   * @param account_id integer Account ID
23
   * @return mixes Integer on success, boolean on failure
24
   **/
25
  public function getCountInvitations($account_id) {
26
    $this->debug->append("STA " . __METHOD__, 4);
27
    $stmt = $this->mysqli->prepare("SELECT count(id) AS total FROM $this->table WHERE account_id = ?");
28
    if ($stmt && $stmt->bind_param('i', $account_id) && $stmt->execute() && $stmt->bind_result($total) && $stmt->fetch())
29
      return $total;
0 ignored issues
show
Bug introduced by
The variable $total does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
30
    $this->sqlError('E0021');
31
  }
32
33
  /**
34
   * Get a specific invitation by email address
35
   * Used to ensure no invitation was already sent
36
   * @param strEmail string Email address to check for
37
   * @return bool boolean true of ralse
38
   **/
39
  public function getByEmail($strEmail) {
40
    $this->debug->append("STA " . __METHOD__, 4);
41
    return $this->getSingle($strEmail, 'id', 'email', 's');
42
  }
43
44
  /**
45
   * Get a specific token by token ID
46
   * Used to match an invitation against a token
47
   * @param token_id integer Token ID stored in invitation
48
   * @return data mixed Invitation ID on success, false on error
49
   **/
50
  public function getByTokenId($token_id) {
51
    $this->debug->append("STA " . __METHOD__, 4);
52
    return $this->getSingle($token_id, 'id', 'token_id');
53
  }
54
55
  /**
56
   * Set an invitation as activated by the invitee
57
   * @param token_id integer Token to activate
58
   * @return bool boolean true or false
59
   **/
60
  public function setActivated($token_id) {
61
    if (!$iInvitationId = $this->getByTokenId($token_id)) {
62
      $this->setErrorMessage($this->getErrorMsg('E0030'));
63
      return false;
64
    }
65
    $field = array('name' => 'is_activated', 'type' => 'i', 'value' => 1);
66
    return $this->updateSingle($iInvitationId, $field);
0 ignored issues
show
Documentation introduced by
$field is of type array<string,string|inte...ng","value":"integer"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
  }
68
69
  /**
70
   * Insert a new invitation to the database
71
   * @param account_id integer Account ID to bind the invitation to
72
   * @param email string Email address the invite was sent to
73
   * @param token_id integer Token ID used during invitation
74
   * @return bool boolean True of false
75
   **/
76
  public function createInvitation($account_id, $email, $token_id) {
77
    $this->debug->append("STA " . __METHOD__, 4);
78
    $stmt = $this->mysqli->prepare("INSERT INTO $this->table ( account_id, email, token_id ) VALUES ( ?, ?, ?)");
79
    if ($stmt && $stmt->bind_param('isi', $account_id, $email, $token_id) && $stmt->execute())
80
      return true;
81
    $this->sqlError('E0022');
82
  }
83
84
  /**
85
   * Send an invitation out to a user
86
   * Uses the mail class to send mails
87
   * @param account_id integer Sending account ID
88
   * @param aData array Data array including mail information
89
   * @return bool boolean True or false
90
   **/
91
  public function sendInvitation($account_id, $aData) {
92
    $this->debug->append("STA " . __METHOD__, 4);
93
    // Check data input
94
    if (empty($aData['email']) || !filter_var($aData['email'], FILTER_VALIDATE_EMAIL)) {
95
      $this->setErrorMessage($this->getErrorMsg('E0023'));
96
      return false;
97
    }
98
    if (preg_match('/[^a-z_\.\!\?\-0-9 ]/i', $aData['message'])) {
99
      $this->setErrorMessage($this->getErrorMsg('E0024'));
100
      return false;
101
    }
102
    // Ensure this invitation does not exist yet nor do we have an account with that email
103
    if ($this->user->getEmail($aData['email'])) {
0 ignored issues
show
Bug introduced by
The property user 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...
104
      $this->setErrorMessage($this->getErrorMsg('E0025'));
105
      return false;
106
    }
107
    if ($this->getByEmail($aData['email'])) {
108
      $this->setErrorMessage($this->getErrorMsg('E0026'));
109
      return false;
110
    }
111 View Code Duplication
    if (!$aData['token'] = $this->token->createToken('invitation', $account_id)) {
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...
112
      $this->setErrorMessage($this->getErrorMsg('E0027', $this->token->getError()));
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...
113
      return false;
114
    }
115
    $aData['username'] = $this->user->getUserName($account_id);
116
    $aData['subject'] = 'Pending Invitation';
117
    $this->log->log("info", $this->user->getUserName($account_id)." sent an invitation");
118
    if ($this->mail->sendMail('invitations/body', $aData)) {
0 ignored issues
show
Bug introduced by
The property mail 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...
119
      $aToken = $this->token->getToken($aData['token'], 'invitation');
120
      if (!$this->createInvitation($account_id, $aData['email'], $aToken['id']))
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->createInvitation(...email'], $aToken['id']) of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
121
        return false;
122
      return true;
123
    } else {
124
      $this->log->log("warn", $this->user->getUserName($account_id)." sent an invitation but failed to send e-mail");
125
      $this->setErrorMessage($this->getErrorMsg('E0028'));
126
    }
127
    $this->setErrorMessage($this->getErrorMsg('E0029'));
128
    return false;
129
  }
130
}
131
132
// Instantiate class
133
$invitation = new invitation();
134
$invitation->setDebug($debug);
135
$invitation->setLog($log);
136
$invitation->setMysql($mysqli);
137
$invitation->setMail($mail);
138
$invitation->setUser($user);
139
$invitation->setToken($oToken);
140
$invitation->setConfig($config);
141
$invitation->setErrorCodes($aErrorCodes);
142