Test Failed
Pull Request — master (#13)
by
unknown
10:25
created

BadRequest::__construct()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 16
nop 1
dl 0
loc 23
ccs 0
cts 17
cp 0
crap 30
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Kapersoft\ShareFile\Exceptions;
4
5
use Exception;
6
use Psr\Http\Message\ResponseInterface;
7
8
/**
9
 * Class BadRequest.
10
 *
11
 * @author   Jan Willem Kaper <[email protected]>
12
 * @license  MIT (see License.txt)
13
 *
14
 * @link     http://github.com/kapersoft/sharefile-api
15
 */
16
class BadRequest extends Exception
17
{
18
    /**
19
     * The http error code supplied in the response.
20
     *
21
     * @var int|null
22
     */
23
    public $httpCode;
24
25
    /**
26
     * The error code supplied in the response.
27
     *
28
     * @var string|null
29
     */
30
    public $code;
31
32
    /**
33
     * The error message supplied in the response.
34
     *
35
     * @var string|null
36
     */
37
    public $message;
38
39
    /**
40
     * BadRequest constructor.
41
     *
42
     * @param ResponseInterface $response Guzzle response
43
     */
44
    public function __construct(ResponseInterface $response)
45
    {
46
        $this->httpCode = $response->getStatusCode();
47
48
        $body = json_decode($response->getBody(), true);
49
50
        if (isset($body['error'])) {
51
            $this->message = $body['error'];
52
        }
53
54
        if (isset($body['error_description'])) {
55
            $this->message = $body['error_description'];
56
        }
57
58
        if (isset($body['message']['value'])) {
59
            $this->message = $body['message']['value'];
60
        }
61
62
        if (isset($body['code'])) {
63
            $this->code = $body['code'];
64
        }
65
66
        parent::__construct('['.$this->code.'] '.$this->message, $this->httpCode);
67
    }
68
}
69