AbstractErrorProcessor   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 14
c 0
b 0
f 0
dl 0
loc 27
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A logException() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Processors;
6
7
use WebServCo\Framework\Helpers\ErrorMessageHelper;
8
use WebServCo\Framework\Interfaces\FileLoggerInterface;
9
10
/**
11
* A helper class for logging exceptions to a standard error(CLI) log file.
12
*/
13
abstract class AbstractErrorProcessor implements \WebServCo\Framework\Interfaces\ErrorProcessorInterface
14
{
15
    protected FileLoggerInterface $fileLogger;
16
17
    abstract public function report(\Throwable $exception, ?string $reference = null): bool;
18
19
    public function __construct()
20
    {
21
        $this->fileLogger = new \WebServCo\Framework\Log\FileLogger(
22
            \WebServCo\Framework\Helpers\PhpHelper::isCli() ? 'errorCLI' : 'error',
23
            \WebServCo\Framework\Environment\Config::string('APP_PATH_LOG'),
24
        );
25
    }
26
27
    public function logException(\Throwable $exception): void
28
    {
29
        $message = ErrorMessageHelper::format($exception);
30
        $this->fileLogger->error($message, ['message' => $message, 'trace' => $exception->getTrace()]);
31
        $previous = $exception->getPrevious();
32
        if (!$previous instanceof \Throwable) {
0 ignored issues
show
introduced by
$previous is always a sub-type of Throwable.
Loading history...
33
            return;
34
        }
35
        do {
36
            $message = ErrorMessageHelper::format($previous);
37
            $this->fileLogger->error($message, ['message' => $message, 'trace' => $previous->getTrace()]);
38
        // phpcs:ignore SlevomatCodingStandard.ControlStructures.AssignmentInCondition.AssignmentInCondition
39
        } while ($previous = $previous->getPrevious());
40
    }
41
}
42