Passed
Push — master ( 46ed75...ca2387 )
by Zaahid
03:33
created

Error::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 10
cc 2
nc 2
nop 4
crap 2
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 Throwable;
11
use InvalidArgumentException;
12
use Psr\Log\LogLevel;
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
    private array $levelMap = [
43
        LogLevel::EMERGENCY => 0,
44
        LogLevel::ALERT => 1,
45
        LogLevel::CRITICAL => 2,
46
        LogLevel::ERROR => 3,
47
        LogLevel::WARNING => 4,
48
        LogLevel::NOTICE => 5,
49
        LogLevel::INFO => 6,
50
        LogLevel::DEBUG => 7,
51
    ];
52
53
    /**
54
     *
55
     * @throws InvalidArgumentException if the passed $psrLogLevelAsErrorLevel
56
     *         is not a known PSR log level (see \Psr\Log\LogLevel)
57
     */
58 79
    public function __construct(string $message, string $psrLogLevelAsErrorLevel, ErrorBag $object, ?Throwable $exception = null)
59
    {
60 79
        if (!isset($this->levelMap[$psrLogLevelAsErrorLevel])) {
61 1
            throw new InvalidArgumentException($psrLogLevelAsErrorLevel . ' is not a known PSR Log Level');
62
        }
63 78
        $this->message = $message;
64 78
        $this->psrLevel = $psrLogLevelAsErrorLevel;
65 78
        $this->object = $object;
66 78
        $this->exception = $exception;
67
    }
68
69
    /**
70
     * Returns the error message.
71
     */
72 1
    public function getMessage() : string
73
    {
74 1
        return $this->message;
75
    }
76
77
    /**
78
     * Returns the PSR string log level for this error message.
79
     */
80 1
    public function getPsrLevel() : string
81
    {
82 1
        return $this->psrLevel;
83
    }
84
85
    /**
86
     * Returns the class type the error occurred on.
87
     */
88 1
    public function getClass() : string
89
    {
90 1
        return get_class($this->object);
91
    }
92
93
    /**
94
     * Returns the object the error occurred on.
95
     */
96 1
    public function getObject() : ErrorBag
97
    {
98 1
        return $this->object;
99
    }
100
101
    /**
102
     * Returns the exception that occurred, if any, or null.
103
     */
104
    public function getException() : ?Throwable
105
    {
106
        return $this->exception;
107
    }
108
109
    /**
110
     * Returns true if the PSR log level for this error is equal to or greater
111
     * than the one passed, e.g. passing LogLevel::ERROR would return true for
112
     * LogLevel::ERROR and LogLevel::CRITICAL, ALERT and EMERGENCY.
113
     */
114 2
    public function isPsrLevelGreaterOrEqualTo(string $minLevel) : bool
115
    {
116 2
        $minIntLevel = $this->levelMap[$minLevel] ?? 1000;
117 2
        $thisLevel = $this->levelMap[$this->psrLevel];
118 2
        return ($minIntLevel >= $thisLevel);
119
    }
120
}
121