Completed
Push — master ( e80003...273b48 )
by Tobias
21:42
created

HttpClientException   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 53.85%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 82
ccs 14
cts 26
cp 0.5385
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A badRequest() 0 4 1
A unauthorized() 0 4 1
A requestFailed() 0 4 1
A notFound() 0 4 1
A getResponse() 0 4 1
A getResponseBody() 0 4 1
A getResponseCode() 0 4 1
1
<?php
2
3
/*
4
 * Copyright (C) 2013-2016 Mailgun
5
 *
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace Mailgun\Exception;
11
12
use Mailgun\Exception;
13
use Psr\Http\Message\ResponseInterface;
14
15
/**
16
 * @author Tobias Nyholm <[email protected]>
17
 */
18
final class HttpClientException extends \RuntimeException implements Exception
19
{
20
    /**
21
     * @var ResponseInterface|null
22
     */
23
    private $response;
24
25
    /**
26
     * @var array
27
     */
28
    private $responseBody;
29
30
    /**
31
     * @var int
32
     */
33
    private $responseCode;
34
35
    /**
36
     * @param string                 $message
37
     * @param int                    $code
38
     * @param ResponseInterface|null $response
39
     */
40 6
    public function __construct($message, $code, ResponseInterface $response = null)
41
    {
42 6
        parent::__construct($message, $code);
43
44 6
        if ($response) {
45 6
            $this->response = $response;
46 6
            $this->responseCode = $response->getStatusCode();
47 6
            $body = $response->getBody()->__toString();
48 6
            if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) {
49
                $this->responseBody['message'] = $body;
50
            } else {
51 6
                $this->responseBody = json_decode($body, true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($body, true) of type * is incompatible with the declared type array of property $responseBody.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
            }
53 6
        }
54 6
    }
55
56 2
    public static function badRequest(ResponseInterface $response = null)
57
    {
58 2
        return new self('The parameters passed to the API were invalid. Check your inputs!', 400, $response);
59
    }
60
61
    public static function unauthorized(ResponseInterface $response = null)
62
    {
63
        return new self('Your credentials are incorrect.', 401, $response);
64
    }
65
66
    public static function requestFailed(ResponseInterface $response = null)
67
    {
68
        return new self('Parameters were valid but request failed. Try again.', 402, $response);
69
    }
70
71 4
    public static function notFound(ResponseInterface $response = null)
72
    {
73 4
        return new self('The endpoint you tried to access does not exist. Check your URL.', 404, $response);
74
    }
75
76
    /**
77
     * @return ResponseInterface
78
     */
79
    public function getResponse()
80
    {
81
        return $this->response;
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    public function getResponseBody()
88
    {
89
        return $this->responseBody;
90
    }
91
92
    /**
93
     * @return int
94
     */
95
    public function getResponseCode()
96
    {
97
        return $this->responseCode;
98
    }
99
}
100