ResultErrorLogger::formatErrorContext()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\PHPQueryBus\Middleware;
6
7
use Psr\Log;
8
use RemotelyLiving\PHPQueryBus\Interfaces;
9
use RemotelyLiving\PHPQueryBus\Traits;
10
11
final class ResultErrorLogger implements Log\LoggerAwareInterface
12
{
13
    use Traits\Logger;
14
15
    public function __invoke(object $query, callable $next): Interfaces\Result
16
    {
17
        /** @var \RemotelyLiving\PHPQueryBus\Interfaces\Result $result */
18
        $result = $next($query);
19
20
        if ($result->hasErrors()) {
21
            $this->getLogger()->error('Query bus result fetched with errors', $this->formatErrorContext($result));
22
        }
23
24
        return $result;
25
    }
26
27
    private function formatErrorContext(Interfaces\Result $result): array
28
    {
29
        $errorContext = [];
30
        foreach ($result->getErrors() as $error) {
31
            $errorContext[] = [
32
                'ExceptionClass' => get_class($error),
33
                'Message' => $error->getMessage(),
34
                'Line' => $error->getLine(),
35
                'File' => $error->getFile(),
36
                'Trace' => $error->getTraceAsString(),
37
            ];
38
        }
39
40
        return $errorContext;
41
    }
42
}
43