Skip to content

Commit

Permalink
Quick class cleanup + type hints
Browse files Browse the repository at this point in the history
* Use PHP 8 type declarations (on params and class props)
* Remove unnecessary PHPDoc tags
* Add return types
  • Loading branch information
pH-7 authored Sep 3, 2024
1 parent 16f0fb8 commit e359a5b
Showing 1 changed file with 19 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @author Pierre-Henry Soria <[email protected]>
* @copyright (c) 2016-2019, Pierre-Henry Soria. All Rights Reserved.
* @copyright (c) 2016-2024, Pierre-Henry Soria. All Rights Reserved.
* @license MIT License; See LICENSE.md and COPYRIGHT.md in the root directory.
* @package PH7 / App / System / Module / Two-Factor Auth / Controller
*/
Expand All @@ -24,30 +24,21 @@ class MainController extends Controller
const WRONG_MODULE_ERROR_MESSAGE = 'Wrong "%s" module!';
const BACKUP_CODE_FILE_EXT = '.txt';

/** @var TwoFactorAuthModel */
private $o2FactorModel;
private TwoFactorAuthModel $o2FactorModel;
private Authenticator $oAuthenticator;

This comment has been minimized.

Copy link
@pH-7

pH-7 Sep 3, 2024

Author Member

Also, need to update RobThree's TwoFactorAuth to v3

RobThree/TwoFactorAuth#119


/** @var Authenticator */
private $oAuthenticator;

/** @var string */
private $sMod;

/** @var int */
private $iIsEnabled;

/** @var int */
private $iProfileId;
private string $sMod;
private int $iIsEnabled;
private int $iProfileId;

public function __construct()
{
parent::__construct();

$this->oAuthenticator = new Authenticator($this->registry->site_url);

}

public function verificationCode($sMod = '')
public function verificationCode(string $sMod = ''): void
{
$this->sMod = $sMod;
$this->checkMod();
Expand All @@ -56,7 +47,7 @@ public function verificationCode($sMod = '')
$this->output();
}

public function setup($sMod = '')
public function setup(string $sMod = ''): void
{
$this->sMod = $sMod;
$this->checkMod();
Expand Down Expand Up @@ -98,10 +89,8 @@ public function setup($sMod = '')
* Download the backup 2FA code (text file).
*
* @param string $sSecret The 2FA secret.
*
* @return void
*/
private function download($sSecret)
private function download(string $sSecret): void
{
$sFileName = '2FA-backup-code-' . $this->sMod . '-' . Url::clean($this->registry->site_name) . self::BACKUP_CODE_FILE_EXT;
header('Content-Disposition: attachment; filename=' . $sFileName);
Expand All @@ -112,10 +101,8 @@ private function download($sSecret)

/**
* @param string $sSecret The 2FA secret code.
*
* @return string
*/
private function getBackupCodeMessage($sSecret)
private function getBackupCodeMessage(string $sSecret): string
{
$sTxtMsg = t('BACKUP VERIFICATION CODE - %site_url% | %0%', $this->sMod) . "\r\n\r\n";
$sTxtMsg .= t('Code: %0%', $this->oAuthenticator->getCode($sSecret)) . "\r\n\r\n";
Expand All @@ -131,19 +118,17 @@ private function getBackupCodeMessage($sSecret)
/**
* Get Session Profile ID.
*
* @return int
*
* @throws PH7InvalidArgumentException Explanatory message if the specified module is wrong.
*/
private function getProfileId()
private function getProfileId(): int
{
switch ($this->sMod) {
case 'user':
return $this->session->get('member_id');
return (int)$this->session->get('member_id');
case 'affiliate':
return $this->session->get('affiliate_id');
return (int)$this->session->get('affiliate_id');
case PH7_ADMIN_MOD:
return $this->session->get('admin_id');
return (int)$this->session->get('admin_id');

default:
throw new PH7InvalidArgumentException(
Expand All @@ -154,10 +139,8 @@ private function getProfileId()

/**
* Turn on/off Two-Factor authentication.
*
* @return void
*/
private function update2FaStatus()
private function update2FaStatus(): void
{
$this->iIsEnabled = ($this->iIsEnabled === 1) ? 0 : 1; // Get the opposite value (if 1 so 0 | if 0 so 1)

Expand All @@ -170,22 +153,20 @@ private function update2FaStatus()
*
* @return string Unique Authenticator Name for the site.
*/
private function getAuthenticatorName()
private function getAuthenticatorName(): string
{
return str_replace('/', '-', UrlName::parse($this->registry->site_url)) . '-' . $this->sMod;
}

/**
* @param string $sSecret The 2FA secret code.
*
* @return bool
*/
private function isTwoFactorSet($sSecret)
private function isTwoFactorSet($sSecret): bool
{
return !empty($sSecret) && strlen($sSecret) > self::TWO_FACTOR_SECRET_STRING_LENGTH;
}

private function checkMod()
private function checkMod(): void
{
if (!$this->isModValid()) {
Header::redirect(
Expand All @@ -196,10 +177,7 @@ private function checkMod()
}
}

/**
* @return bool
*/
private function isModValid()
private function isModValid(): bool
{
$aValidMods = [
'user',
Expand Down

0 comments on commit e359a5b

Please sign in to comment.