Completed
Push — master ( 48e5b8...eb0757 )
by Tobias
20:40
created

src/Mailgun/Exception/HttpClientException.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 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 (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
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 have tried to access does not exist. Check if the domain matches the domain you have configure on Mailgun.', 404, $response);
74
    }
75
76
    public static function payloadTooLarge(ResponseInterface $response = null)
77
    {
78
        return new self('Payload too large, your total attachment size is too big.', 413, $response);
79
    }
80
81
    /**
82
     * @return ResponseInterface
83
     */
84
    public function getResponse()
85
    {
86
        return $this->response;
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function getResponseBody()
93
    {
94
        return $this->responseBody;
95
    }
96
97
    /**
98
     * @return int
99
     */
100
    public function getResponseCode()
101
    {
102
        return $this->responseCode;
103
    }
104
}
105