HttpException   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 139
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addHeader() 0 5 1
A addHeaders() 0 11 3
A getBody() 0 3 1
A setBody() 0 5 1
A getStatus() 0 3 1
A getHeaders() 0 3 1
A __construct() 0 11 3
1
<?php
2
3
namespace Pagantis\OrdersApiClient\Exception;
4
5
/**
6
 * Class HttpException
7
 * @package Pagantis\OrdersApiClient\Exception
8
 */
9
class HttpException extends \Exception
10
{
11
    /**
12
     * List of additional headers
13
     *
14
     * @var array
15
     */
16
    private $headers = array();
17
18
    /**
19
     * Body message
20
     *
21
     * @var string
22
     */
23
    private $body = '';
24
25
    /**
26
     * List of HTTP status codes USED
27
     *
28
     * @var array
29
     */
30
    private $status = array(
31
        400 => 'Bad Request',
32
        401 => 'Unauthorized',
33
        403 => 'Forbidden',
34
        404 => 'Not Found',
35
        405 => 'Method Not Allowed',
36
        406 => 'Not Acceptable',
37
        409 => 'Conflict',
38
        422 => 'Unprocessable Entity',
39
        500 => 'Internal Server Error',
40
        501 => 'Not Implemented',
41
        502 => 'Bad Gateway',
42
        503 => 'Service Unavailable',
43
    );
44
45
    const HTTP_BAD_REQUEST = 400;
46
    const HTTP_UNAUTHORIZED = 401;
47
    const HTTP_FORBIDDEN = 403;
48
    const HTTP_NOT_FOUND = 404;
49
    const HTTP_METHOD_NOT_ALLOWED = 405;
50
    const HTTP_NOT_ACCEPTABLE = 406;
51
    const HTTP_CONFLICT = 409;
52
    const HTTP_UNPROCESSABLE_ENTITY = 422;
53
    const HTTP_INTERNAL_SERVER_ERROR = 500;
54
    const HTTP_NOT_IMPLEMENTED = 501;
55
    const HTTP_BAD_GATEWAY = 502;
56
    const HTTP_SERVICE_UNAVAILABLE = 503;
57
58
    /**
59
     * @param int[optional]    $statusCode   If NULL will use 500 as default
60
     * @param string[optional] $statusPhrase If NULL will use the default status phrase
61
     * @param array[optional]  $headers      List of additional headers
62
     */
63
    public function __construct($statusCode = 500, $statusPhrase = null, array $headers = array())
64
    {
65
        if (null === $statusPhrase && isset($this->status[$statusCode])) {
66
            $statusPhrase = $this->status[$statusCode];
67
        }
68
        parent::__construct($statusPhrase, $statusCode);
69
70
        $header  = sprintf('HTTP/1.1 %d %s', $statusCode, $statusPhrase);
71
72
        $this->addHeader($header);
73
        $this->addHeaders($headers);
74
    }
75
76
    /**
77
     * Returns the list of additional headers
78
     *
79
     * @return array
80
     */
81
    public function getHeaders()
82
    {
83
        return $this->headers;
84
    }
85
86
    /**
87
     * @param string $header
88
     *
89
     * @return self
90
     */
91
    public function addHeader($header)
92
    {
93
        $this->headers[] = $header;
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param array $headers
100
     *
101
     * @return self
102
     */
103
    public function addHeaders(array $headers)
104
    {
105
        foreach ($headers as $key => $header) {
106
            if (!is_int($key)) {
107
                $header = $key.': '.$header;
108
            }
109
110
            $this->addHeader($header);
111
        }
112
113
        return $this;
114
    }
115
116
    /**
117
     * Return the body message.
118
     *
119
     * @return string
120
     */
121
    public function getBody()
122
    {
123
        return $this->body;
124
    }
125
126
    /**
127
     * Define a body message.
128
     *
129
     * @param string $body
130
     *
131
     * @return self
132
     */
133
    public function setBody($body)
134
    {
135
        $this->body = (string) $body;
136
137
        return $this;
138
    }
139
140
    /**
141
     * Return the valid status array
142
     *
143
     * @return array
144
     */
145
    public function getStatus()
146
    {
147
        return $this->status;
148
    }
149
}
150