HttpException::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * This file contains the HttpException
5
 *
6
 * @copyright 2015 Vladimir Jimenez
7
 * @license   https://github.com/allejo/PhpSoda/blob/master/LICENSE.md MIT
8
 */
9
10
namespace allejo\Socrata\Exceptions;
11
12
/**
13
 * An exception thrown if a cURL job returned an HTTP status of anything but 200
14
 *
15
 * @package allejo\Socrata\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
    public function __construct ($code, $response)
30
    {
31
        $json = json_decode($response);
32
33
        if (is_null($json))
34
        {
35
            $status        = ucfirst($json['code']);
36
            $message       = $json['message'];
37
            $this->message = sprintf("HTTP %s %d: %s", $status, $code, $message);
38
        }
39
        else
40
        {
41
            $this->message = $response;
42
        }
43
44
        $this->code = $code;
45
    }
46
}
47