| Conditions | 8 |
| Paths | 17 |
| Total Lines | 62 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 101 | public function __construct($invitationId) { |
||
| 102 | parent::__construct(); |
||
| 103 | $this->invitationTokenString = $invitationId; |
||
| 104 | $this->databaseHandle = DBConnection::handle("INST"); |
||
| 105 | /* |
||
| 106 | * Finds invitation by its token attribute and loads all certificates generated using the token. |
||
| 107 | * Certificate details will always be empty, since code still needs to be adapted to return multiple certificates information. |
||
| 108 | */ |
||
| 109 | $invColumnNames = "`id`, `profile_id`, `silverbullet_user_id`, `token`, `quantity`, `expiry`"; |
||
| 110 | $invitationsResult = $this->databaseHandle->exec("SELECT $invColumnNames FROM `silverbullet_invitation` WHERE `token`=? ORDER BY `expiry` DESC", "s", $this->invitationTokenString); |
||
| 111 | $this->associatedCertificates = []; |
||
| 112 | if ($invitationsResult->num_rows == 0) { |
||
| 113 | $this->loggerInstance->debug(2, "Token $this->invitationTokenString not found in database or database query error!\n"); |
||
| 114 | $this->invitationTokenStatus = SilverbulletInvitation::SB_TOKENSTATUS_INVALID; |
||
| 115 | $this->identifier = 0; |
||
| 116 | $this->profile = 0; |
||
| 117 | $this->userId = 0; |
||
| 118 | $this->expiry = "2000-01-01 00:00:00"; |
||
| 119 | $this->activationsTotal = 0; |
||
| 120 | $this->activationsRemaining = 0; |
||
| 121 | return; |
||
| 122 | } |
||
| 123 | // if not returned, we found the token in the DB |
||
| 124 | // -> instantiate the class |
||
| 125 | // SELECT -> resource, no boolean |
||
| 126 | $invitationRow = mysqli_fetch_object(/** @scrutinizer ignore-type */ $invitationsResult); |
||
| 127 | $this->identifier = $invitationRow->id; |
||
| 128 | $this->profile = $invitationRow->profile_id; |
||
| 129 | $this->userId = $invitationRow->silverbullet_user_id; |
||
| 130 | $this->expiry = $invitationRow->expiry; |
||
| 131 | $this->activationsTotal = $invitationRow->quantity; |
||
| 132 | $certificatesResult = $this->databaseHandle->exec("SELECT `serial_number` FROM `silverbullet_certificate` WHERE `silverbullet_invitation_id` = ? ORDER BY `revocation_status`, `expiry` DESC", "i", $this->identifier); |
||
| 133 | $certificatesNumber = ($certificatesResult ? $certificatesResult->num_rows : 0); |
||
| 134 | $this->loggerInstance->debug(5, "At token validation level, " . $certificatesNumber . " certificates exist.\n"); |
||
| 135 | // SELECT -> resource, no boolean |
||
| 136 | while ($runner = mysqli_fetch_object(/** @scrutinizer ignore-type */ $certificatesResult)) { |
||
| 137 | $this->associatedCertificates[] = new \core\SilverbulletCertificate($runner->serial_number); |
||
| 138 | } |
||
| 139 | $this->activationsRemaining = (int) $this->activationsTotal - (int) $certificatesNumber; |
||
| 140 | switch ($certificatesNumber) { |
||
| 141 | case 0: |
||
| 142 | // find out if it has expired |
||
| 143 | $now = new \DateTime(); |
||
| 144 | $expiryObject = new \DateTime($this->expiry); |
||
| 145 | $delta = $now->diff($expiryObject); |
||
| 146 | if ($delta->invert == 1) { |
||
| 147 | $this->invitationTokenStatus = SilverbulletInvitation::SB_TOKENSTATUS_EXPIRED; |
||
| 148 | $this->activationsRemaining = 0; |
||
| 149 | break; |
||
| 150 | } |
||
| 151 | $this->invitationTokenStatus = SilverbulletInvitation::SB_TOKENSTATUS_VALID; |
||
| 152 | break; |
||
| 153 | case $invitationRow->quantity: |
||
| 154 | $this->invitationTokenStatus = SilverbulletInvitation::SB_TOKENSTATUS_REDEEMED; |
||
| 155 | break; |
||
| 156 | default: |
||
| 157 | assert($certificatesNumber > 0); // no negatives allowed |
||
| 158 | assert($certificatesNumber < $invitationRow->quantity || $invitationRow->quantity == 0); // not more than max quantity allowed (unless quantity is zero) |
||
| 159 | $this->invitationTokenStatus = SilverbulletInvitation::SB_TOKENSTATUS_PARTIALLY_REDEEMED; |
||
| 160 | } |
||
| 161 | |||
| 162 | $this->loggerInstance->debug(5, "Done creating invitation token state from DB.\n"); |
||
| 163 | } |
||
| 251 |