ClientException::getDescription()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2014 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Exception;
9
10
use Garden\Response;
11
12
/**
13
 * Represents a 400 series exception.
14
 */
15
class ClientException extends \Exception implements \JsonSerializable {
16
    protected $context;
17
18
    /**
19
     * Initialize an instance of the {@link ClientException} class.
20
     *
21
     * The 4xx class of status code is intended for cases in which the client seems to have erred.
22
     * When constructing a client exception you can pass additional information on the {@link $context} parameter
23
     * to aid in rendering.
24
     *
25
     * - Keys beginning with **HTTP_** will be added as headers.
26
     * - **description** will give the exception a longer description.
27
     *
28
     * @param string $message The error message.
29
     * @param int $code The http error code.
30
     * @param array $context An array of context variables that can be used to render a more detailed response.
31
     */
32 23
    public function __construct($message = '', $code = 400, array $context = []) {
33 23
        parent::__construct($message, $code);
34 23
        $this->context = $context;
35 23
    }
36
37
    /**
38
     * Gets the response headers that were set on the exception.
39
     *
40
     * @return array Returns the headers for this exception.
41
     */
42 23
    public function getHeaders() {
43 23
        $result = [];
44 23
        foreach ($this->context as $key => $value) {
45 23
            if (stripos($key, 'http_') === 0) {
46 7
                $key = Response::normalizeHeader(ltrim_substr($key, 'http_'));
47 7
                $result[$key] = $value;
48 7
            }
49 23
        }
50 23
        return $result;
51
    }
52
53
    /**
54
     * Gets a longer description for the exception.
55
     *
56
     * @return string Returns the description of the exception or an empty string if there isn't one.
57
     */
58 23
    public function getDescription() {
59 23
        return val('description', $this->context, '');
60
    }
61
62
    /**
63
     * Specify data which should be serialized to JSON.
64
     *
65
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
66
     * @return mixed data which can be serialized by <b>json_encode</b>,
67
     * which is a value of any type other than a resource.
68
     */
69 23
    public function jsonSerialize() {
70
        $result = [
71 23
            'message' => $this->getMessage(),
72 23
            'status' => $this->getCode()
73 23
        ];
74 23
        if ($this->getDescription()) {
75 4
            $result['description'] = $this->getDescription();
76 4
        }
77 23
        return $result;
78
    }
79
}
80