Passed
Push — master ( b5e6ba...9ab7d8 )
by Radu
09:39 queued 02:23
created

AbstractErrorProcessor::logException()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 3
nc 2
nop 1
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\LoggerInterface;
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 LoggerInterface $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