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

HttpException::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 10
cp 0.9
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
crap 3.009
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