HttpException   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A getHttpStatusCode() 0 3 1
A getUserMessage() 0 3 1
1
<?php
2
3
namespace TraderInteractive;
4
5
use Exception;
6
7
/**
8
 * Exception to throw when an http status code should be included.
9
 *
10
 * Defaults to a 500 error.
11
 */
12
final class HttpException extends Exception
13
{
14
    private $httpStatusCode;
15
    private $userMessage;
16
17
    /**
18
     * Constructs
19
     *
20
     * @param string $message @see Exception::__construct()
21
     * @param int $httpStatusCode a valid http status code
22
     * @param int $code @see Exception::__construct()
23
     * @param Exception $previous @see Exception::__construct()
24
     * @param string|null $userMessage a nicer message to display to the user sans sensitive details
25
     */
26
    public function __construct(
27
        string $message = 'Application Error',
28
        int $httpStatusCode = 500,
29
        int $code = 0,
30
        Exception $previous = null,
31
        string $userMessage = null
32
    ) {
33
        parent::__construct($message, $code, $previous);
34
35
        $this->httpStatusCode = $httpStatusCode;
36
37
        if ($userMessage !== null) {
38
            $this->userMessage = $userMessage;
39
        } else {
40
            $this->userMessage = $message;
41
        }
42
    }
43
44
    /**
45
     * Getter for $httpStatusCode
46
     *
47
     * @return int the http status code
48
     */
49
    public function getHttpStatusCode() : int
50
    {
51
        return $this->httpStatusCode;
52
    }
53
54
    /**
55
     * Getter for $userMessage
56
     *
57
     * @return string the user message
58
     */
59
    public function getUserMessage() : string
60
    {
61
        return $this->userMessage;
62
    }
63
}
64