Completed
Push — develop ( a05ff5...d1fb89 )
by Vladimir
03:27
created

HttpException::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

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