Passed
Push — master ( a626ba...b864c0 )
by Zaahid
15:44 queued 12:03
created

Error   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 28
c 3
b 0
f 0
dl 0
loc 102
ccs 21
cts 21
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getClass() 0 3 1
A getMessage() 0 3 1
A isPsrLevelGreaterOrEqualTo() 0 5 1
A getObject() 0 3 1
A getPsrLevel() 0 3 1
A getException() 0 3 1
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
8
namespace ZBateson\MailMimeParser;
9
10
use InvalidArgumentException;
11
use Psr\Log\LogLevel;
12
use Throwable;
13
14
/**
15
 * Holds information about an error or notice that happened on a specific
16
 * object.
17
 *
18
 * @author Zaahid Bateson
19
 */
20
class Error
21
{
22
    /**
23
     * @var string The error message.
24
     */
25
    protected string $message;
26
27
    /**
28
     * @var string The PSR log level for this error.
29
     */
30
    protected string $psrLevel;
31
32
    /**
33
     * @var ErrorBag The object the error/notice occurred on.
34
     */
35
    protected ErrorBag $object;
36
37
    /**
38
     * @var ?Throwable An Exception object if one happened, or null if not
39
     */
40
    protected ?Throwable $exception;
41
42
    /**
43
     * @var array<string, int>
44
     */
45
    private array $levelMap = [
46
        LogLevel::EMERGENCY => 0,
47
        LogLevel::ALERT => 1,
48
        LogLevel::CRITICAL => 2,
49
        LogLevel::ERROR => 3,
50
        LogLevel::WARNING => 4,
51
        LogLevel::NOTICE => 5,
52
        LogLevel::INFO => 6,
53
        LogLevel::DEBUG => 7,
54
    ];
55
56
    /**
57
     *
58
     * @throws InvalidArgumentException if the passed $psrLogLevelAsErrorLevel
59
     *         is not a known PSR log level (see \Psr\Log\LogLevel)
60
     */
61 80
    public function __construct(string $message, string $psrLogLevelAsErrorLevel, ErrorBag $object, ?Throwable $exception = null)
62
    {
63 80
        if (!isset($this->levelMap[$psrLogLevelAsErrorLevel])) {
64 1
            throw new InvalidArgumentException($psrLogLevelAsErrorLevel . ' is not a known PSR Log Level');
65
        }
66 79
        $this->message = $message;
67 79
        $this->psrLevel = $psrLogLevelAsErrorLevel;
68 79
        $this->object = $object;
69 79
        $this->exception = $exception;
70
    }
71
72
    /**
73
     * Returns the error message.
74
     */
75 1
    public function getMessage() : string
76
    {
77 1
        return $this->message;
78
    }
79
80
    /**
81
     * Returns the PSR string log level for this error message.
82
     */
83 1
    public function getPsrLevel() : string
84
    {
85 1
        return $this->psrLevel;
86
    }
87
88
    /**
89
     * Returns the class type the error occurred on.
90
     */
91 1
    public function getClass() : string
92
    {
93 1
        return \get_class($this->object);
94
    }
95
96
    /**
97
     * Returns the object the error occurred on.
98
     */
99 1
    public function getObject() : ErrorBag
100
    {
101 1
        return $this->object;
102
    }
103
104
    /**
105
     * Returns the exception that occurred, if any, or null.
106
     */
107 1
    public function getException() : ?Throwable
108
    {
109 1
        return $this->exception;
110
    }
111
112
    /**
113
     * Returns true if the PSR log level for this error is equal to or greater
114
     * than the one passed, e.g. passing LogLevel::ERROR would return true for
115
     * LogLevel::ERROR and LogLevel::CRITICAL, ALERT and EMERGENCY.
116
     */
117 3
    public function isPsrLevelGreaterOrEqualTo(string $minLevel) : bool
118
    {
119 3
        $minIntLevel = $this->levelMap[$minLevel] ?? 1000;
120 3
        $thisLevel = $this->levelMap[$this->psrLevel];
121 3
        return ($minIntLevel >= $thisLevel);
122
    }
123
}
124