Completed
Push — master ( 4a1992...3a4f99 )
by Vladimir
06:33
created

HttpException   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 29
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 3
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/PhpPulse/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\DaPulse\Exceptions;
9
10
/**
11
 * An exception thrown if a cURL job returned an HTTP status of anything but 200
12
 *
13
 * @package allejo\DaPulse\Exceptions
14
 * @since   0.1.0
15
 */
16
class HttpException extends \Exception
17
{
18
    /**
19
     * Create an exception
20
     *
21
     * @param string $code      The HTTP code returned
22
     * @param string $response  A JSON formatted string containing information regarding the HTTP error or a string
23
     *                          simply containing stating the error.
24
     *
25
     * @since 0.1.0
26
     */
27 1
    public function __construct ($code, $response)
28
    {
29 1
        $json = json_decode($response, true);
30
31 1
        if (!is_null($json))
32 1
        {
33 1
            $message = (isset($json["message"])) ? $json["message"] : $json["error"];
34
35 1
            $this->message = sprintf("HTTP %d: %s", $code, $message);
36 1
        }
37
        else
38
        {
39
            $this->message = $response;
40
        }
41
42 1
        $this->code = $code;
43 1
    }
44
}
45