-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored ContextAbstract and improved error handling (#23)
- Loading branch information
1 parent
0206528
commit a93f3fc
Showing
4 changed files
with
233 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace ShoppinPal\YapepCommon\Test\Integration; | ||
|
||
use YapepBase\Config; | ||
use YapepBase\ErrorHandler\ErrorHandlerHelper; | ||
use YapepBase\File\FileHandlerPhp; | ||
|
||
class ErrorLogHandler | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $ignoredErrors = []; | ||
|
||
|
||
public function ignoreError(int $errorCode, string $messagePrefix) | ||
{ | ||
$errorHandlerHelper = new ErrorHandlerHelper(); | ||
$message = '[' . $errorHandlerHelper->getPhpErrorLevelDescription($errorCode) . '(' . $errorCode . ')]: ' | ||
. $messagePrefix; | ||
|
||
$this->ignoredErrors[] = $message; | ||
} | ||
|
||
public function getLoggedErrors(): array | ||
{ | ||
$errorLogPath = Config::getInstance()->get('resource.log.error.path'); | ||
|
||
$fileHandlerPhp = new FileHandlerPhp(); | ||
|
||
if (!$fileHandlerPhp->checkIsPathExists($errorLogPath)) { | ||
return []; | ||
} | ||
|
||
$errorString = $fileHandlerPhp->getAsString($errorLogPath, 0); | ||
|
||
if (empty($errorString)) { | ||
return []; | ||
} | ||
$errors = []; | ||
foreach (explode(PHP_EOL, $errorString) as $error) { | ||
$errorMessage = json_decode($error, true)['message']; | ||
if (empty($errorMessage)) { | ||
continue; | ||
} | ||
|
||
if (!$this->isErrorIgnored($errorMessage)) { | ||
$errors[] = $errorMessage; | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
|
||
protected function isErrorIgnored(string $errorMessage): bool | ||
{ | ||
foreach ($this->ignoredErrors as $ignoredError) { | ||
if (mb_strpos($errorMessage, $ignoredError) === 0) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace ShoppinPal\YapepCommon\Test\Integration; | ||
|
||
use YapepBase\Config; | ||
use YapepBase\File\FileHandlerPhp; | ||
|
||
class LogHandler | ||
{ | ||
public function initLogs(array $resourceNames) | ||
{ | ||
$config = Config::getInstance(); | ||
foreach ($resourceNames as $resourceName) { | ||
$errorLogPath = $config->get('resource.log.' . $resourceName . '.path'); | ||
|
||
$fileHandler = new FileHandlerPhp(); | ||
|
||
if ($fileHandler->checkIsPathExists($errorLogPath)) { | ||
$fileHandler->remove($errorLogPath); | ||
} | ||
} | ||
} | ||
|
||
public function getLoggedEntries(string $logResourceName): array | ||
{ | ||
$logPath = Config::getInstance()->get('resource.log.' . $logResourceName . '.path'); | ||
$logEntries = (new FileHandlerPhp())->getAsString($logPath); | ||
|
||
if (empty($logEntries)) { | ||
return array(); | ||
} | ||
|
||
$loggedEntries = array(); | ||
foreach (explode(PHP_EOL, trim($logEntries, PHP_EOL)) as $logEntry) { | ||
$loggedEntries[] = json_decode($logEntry, true); | ||
} | ||
return $loggedEntries; | ||
} | ||
} |
Oops, something went wrong.