Completed
Push — master ( 07da83...02e319 )
by Tobias
04:27
created

HttpClientException::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 12
cts 12
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
/*
4
 * Copyright (C) 2013 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 2
    public function __construct($message, $code, ResponseInterface $response = null)
41
    {
42 2
        parent::__construct($message, $code);
43
44 2
        if ($response) {
45 2
            $this->response = $response;
46 2
            $this->responseCode = $response->getStatusCode();
47 2
            $body = $response->getBody()->__toString();
48 2
            if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
49 1
                $this->responseBody['message'] = $body;
50 1
            } else {
51 1
                $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 2
        }
54 2
    }
55
56 2
    public static function badRequest(ResponseInterface $response = null)
57
    {
58 2
        $message = 'The parameters passed to the API were invalid. Check your inputs!';
59
60 2
        if (null !== $response) {
61 2
            $body = $response->getBody()->__toString();
62 2
            if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
63 1
                $validationMessage = $body;
64 1
            } else {
65 1
                $jsonDecoded = json_decode($body, true);
66 1
                $validationMessage = isset($jsonDecoded['message']) ? $jsonDecoded['message'] : $body;
67
            }
68
69 2
            $message = sprintf("%s\n\n%s", $message, $validationMessage);
70 2
        }
71
72 2
        return new self($message, 400, $response);
73
    }
74
75
    public static function unauthorized(ResponseInterface $response = null)
76
    {
77
        return new self('Your credentials are incorrect.', 401, $response);
78
    }
79
80
    public static function requestFailed(ResponseInterface $response = null)
81
    {
82
        return new self('Parameters were valid but request failed. Try again.', 402, $response);
83
    }
84
85
    public static function notFound(ResponseInterface $response = null)
86
    {
87
        return new self('The endpoint you have tried to access does not exist. Check if the domain matches the domain you have configure on Mailgun.', 404, $response);
88
    }
89
90
    public static function payloadTooLarge(ResponseInterface $response = null)
91
    {
92
        return new self('Payload too large, your total attachment size is too big.', 413, $response);
93
    }
94
95
    /**
96
     * @return ResponseInterface
97
     */
98
    public function getResponse()
99
    {
100
        return $this->response;
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function getResponseBody()
107
    {
108
        return $this->responseBody;
109
    }
110
111
    /**
112
     * @return int
113
     */
114
    public function getResponseCode()
115
    {
116
        return $this->responseCode;
117
    }
118
}
119