Failed Conditions
Push — master ( 1ef376...9977a5 )
by Arnold
03:50
created

Logging::log()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 8.8571
cc 5
eloc 7
nc 4
nop 1
crap 5
1
<?php
2
3
namespace Jasny\ErrorHandler;
4
5
use Psr\Log\LoggerInterface;
6
use Psr\Log\LogLevel;
7
use Psr\Log\NullLogger;
8
9
/**
10
 * Trait for logging errors and exceptions
11
 */
12
trait Logging
13
{
14
    /**
15
     * @var LoggerInterface
16
     */
17
    protected $logger;
18
    
19
    
20
    /**
21
     * Set the logger for logging errors
22
     * 
23
     * @param LoggerInterface $logger
24
     */
25 116
    public function setLogger(LoggerInterface $logger)
26
    {
27 116
        $this->logger = $logger;
28 116
    }
29
    
30
    /**
31
     * Set the logger for logging errors
32
     * 
33
     * @return LoggerInterface
34
     */
35 68
    public function getLogger()
36
    {
37 68
        if (!isset($this->logger)) {
38 6
            $this->logger = new NullLogger();
39 3
        }
40
        
41 68
        return $this->logger;
42
    }
43
    
44
    
45
    /**
46
     * Log an error or exception
47
     * 
48
     * @param \Exception|\Error $error
49
     */
50 66
    public function log($error)
51
    {
52 66
        if ($error instanceof \Error || $error instanceof \ErrorException) {
53 50
            return $this->logError($error);
54
        }
55
        
56 16
        if ($error instanceof \Exception) {
57 12
            return $this->logException($error);
58
        }
59
        
60 4
        $message = "Unable to log a " . (is_object($error) ? get_class($error) . ' ' : '') . gettype($error);
61 4
        $this->getLogger()->log(LogLevel::WARNING, $message);
62 4
    }
63
    
64
    /**
65
     * Log an error
66
     * 
67
     * @param \Error|\ErrorException $error
68
     */
69 50
    protected function logError($error)
70
    {
71 50
        $code = $error instanceof \ErrorException ? $error->getSeverity() : E_ERROR;
72 50
        $level = $this->getLogLevel($code);
0 ignored issues
show
Bug introduced by
It seems like getLogLevel() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
73
        
74 50
        $message = sprintf('%s: %s at %s line %s', $this->codeToString($code), $error->getMessage(),
0 ignored issues
show
Bug introduced by
It seems like codeToString() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
75 50
            $error->getFile(), $error->getLine());
76
77
        $context = [
78 50
            'error' => $error,
79 50
            'code' => $code,
80 50
            'message' => $error->getMessage(),
81 50
            'file' => $error->getFile(),
82 50
            'line' => $error->getLine()
83 25
        ];
84
85 50
        $this->getLogger()->log($level, $message, $context);
86 50
    }
87
    
88
    /**
89
     * Log an exception
90
     * 
91
     * @param \Exception $exception
92
     */
93 12
    protected function logException(\Exception $exception)
94
    {
95 12
        $level = $this->getLogLevel();
0 ignored issues
show
Bug introduced by
It seems like getLogLevel() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
96
        
97 12
        $message = sprintf('Uncaught Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(),
98 12
            $exception->getFile(), $exception->getLine());
99
        
100 12
        $context = compact('exception');
101
102 12
        $this->getLogger()->log($level, $message, $context);
103 12
    }
104
}
105