Skip to content

Commit

Permalink
Enhancement: Adding types & logging in catch blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
pH-7 committed Dec 18, 2022
1 parent 5aa4f0f commit 5351d56
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Clickatell\ClickatellException;
use Clickatell\Rest as Client;
use PH7\Framework\Error\Logger;

class ClickatellProvider extends SmsProvider implements SmsProvidable
{
Expand Down Expand Up @@ -42,6 +43,8 @@ public function send($sPhoneNumber, $sTextMessage)

return false;
} catch (ClickatellException $oExcept) {
(new Logger())->msg('Clickatell error while sending SMS: ' . $oExcept->getMessage());

return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php
/**
* @author Pierre-Henry Soria <hello@ph7builder.com>
* @copyright (c) 2019, Pierre-Henry Soria. All Rights Reserved.
* @copyright (c) 2019-2023, Pierre-Henry Soria. All Rights Reserved.
* @license MIT License; See LICENSE.md and COPYRIGHT.md in the root directory.
* @package PH7 / App / System / Module / SMS Verification / Inc / Class
*/

namespace PH7;

use PH7\Framework\Error\Logger;
use Twilio\Rest\Client;
use Twilio\Exceptions\TwilioException;

class TwilioProvider extends SmsProvider implements SmsProvidable
{
Expand All @@ -19,14 +21,20 @@ public function send($sPhoneNumber, $sTextMessage)
{
$oClient = new Client($this->sApiId, $this->sApiToken);

$oMessage = $oClient->messages->create(
$sPhoneNumber,
[
'from' => $this->sSenderNumber,
'body' => $sTextMessage
]
);
try {
$oMessage = $oClient->messages->create(
$sPhoneNumber,
[
'from' => $this->sSenderNumber,
'body' => $sTextMessage
]
);

return strlen($oMessage->sid) > 1;
return strlen($oMessage->sid) > 1;
} catch (TwilioException $oExcept) {
(new Logger())->msg('Twilio error while sending SMS: ' . $oExcept->getMessage());

return false;
}
}
}
10 changes: 3 additions & 7 deletions _protected/framework/Error/Debug.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ private function __construct()

/**
* Gets Information (message, code, file, line, trace) of an Exception.
*
* @param Exception $oE
*
* @return string
*/
public static function getInfoExcept(Exception $oE)
public static function getInfoExcept(Exception $oE): string
{
$sDebug = $oE->getMessage();
$sDebug .= '<br />';
Expand All @@ -53,7 +49,7 @@ public static function getInfoExcept(Exception $oE)
*
* @return bool Returns true if the development mode is enabled else returns false.
*/
public static function is()
public static function is(): bool
{
return Config::getInstance()->values['mode']['environment'] === Environment::DEVELOPMENT_MODE;
}
Expand All @@ -73,7 +69,7 @@ private function __clone()
/**
* Alias for Debug::is()
*/
function isDebug()
function isDebug(): bool
{
return Debug::is();
}
Expand Down
31 changes: 5 additions & 26 deletions _protected/framework/Error/LoggerExcept.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ public function __construct()

/**
* Write to the logfile.
*
* @param Exception $oExcept
*
* @return void
*/
public function except(Exception $oExcept)
public function except(Exception $oExcept): void
{
// Time: Set the log date/time.
// IP: The IP address of the client.
Expand Down Expand Up @@ -100,12 +96,7 @@ public function except(Exception $oExcept)
}
}

/**
* @param string $sContents
*
* @return void
*/
private function fileHandler($sContents)
private function fileHandler(string $sContents): void
{
$sFullFile = $this->sDir . static::EXCEPT_DIR . $this->sFileName . '.json';
$sFullGzipFile = $this->sDir . static::EXCEPT_DIR . static::GZIP_DIR . $this->sFileName . '.gz';
Expand All @@ -123,25 +114,15 @@ private function fileHandler($sContents)
}
}

/**
* @param string $sContents
*
* @return void
*/
private function sqlHandler($sContents)
private function sqlHandler(string $sContents): void
{
$sSql = 'INSERT INTO' . Db::prefix(DbTableName::LOG_ERROR) . 'SET logError = :line';
$rStmt = Db::getInstance()->prepare($sSql);
$rStmt->execute([':line' => $sContents]);
Db::free($rStmt);
}

/**
* @param string $sContents
*
* @return void
*/
private function emailHandler($sContents)
private function emailHandler(string $sContents): void
{
$aInfo = [
'to' => $this->config->values['logging']['bug_report_email'],
Expand All @@ -159,10 +140,8 @@ private function emailHandler($sContents)
* If the log file already exists and is larger than 5 Mb, then returns TRUE, FALSE otherwise.
*
* @param string $sFullFile Log file path.
*
* @return bool
*/
private function isGzipEligible($sFullFile)
private function isGzipEligible(string $sFullFile): bool
{
return is_file($sFullFile) && filesize($sFullFile) >= static::MAX_UNCOMPRESSED_SIZE * 1024 * 1024;
}
Expand Down

0 comments on commit 5351d56

Please sign in to comment.