ThrowableLogger::logThrowable()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Polder Knowledge / log-module (https://polderknowledge.com)
4
 *
5
 * @link https://github.com/polderknowledge/log-module for the canonical source repository
6
 * @copyright Copyright (c) 2016-2017 Polder Knowledge (https://polderknowledge.com)
7
 * @license https://github.com/polderknowledge/log-module/blob/master/LICENSE.md MIT
8
 */
9
10
namespace PolderKnowledge\LogModule\TaskService;
11
12
use Throwable;
13
use Psr\Log\LoggerInterface;
14
15
/**
16
 * A helper class that logs a throwable object to a logger.
17
 */
18
final class ThrowableLogger
19
{
20
    /**
21
     * @var LoggerInterface
22
     */
23
    private $logger;
24
25
    /**
26
     * Initializes a new instance of this class.
27
     *
28
     * @param LoggerInterface $logger
29
     */
30 25
    public function __construct(LoggerInterface $logger)
31
    {
32 25
        $this->logger = $logger;
33 25
    }
34
35
    /**
36
     * Logs a throwable object.
37
     *
38
     * @param Throwable $throwable The throwable object to log.
39
     * @return void
40
     */
41 12
    public function logThrowable(Throwable $throwable)
42
    {
43 12
        while ($throwable) {
44 12
            $this->logger->error($throwable->getMessage(), [
45 12
                $throwable->getCode(),
46 12
                $throwable->getFile(),
47 12
                $throwable->getLine(),
48 12
                $throwable->getTrace()
49
            ]);
50
51 12
            $throwable = $throwable->getPrevious();
52
        }
53 12
    }
54
}
55