Completed
Push — master ( f109b4...9b270d )
by David
01:41
created

HttpClientException   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 62.79%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 106
ccs 27
cts 43
cp 0.6279
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A badRequest() 0 14 3
A unauthorized() 0 4 1
A requestFailed() 0 4 1
A notFound() 0 4 1
A conflict() 0 4 1
A payloadTooLarge() 0 4 1
A tooManyRequests() 0 4 1
A forbidden() 0 14 3
A getResponse() 0 4 1
A getResponseBody() 0 4 1
A getResponseCode() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2013 Mailgun
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license. See the LICENSE file for details.
10
 */
11
12
namespace Mailgun\Exception;
13
14
use Mailgun\Exception;
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * @author Tobias Nyholm <[email protected]>
19
 */
20
final class HttpClientException extends \RuntimeException implements Exception
21
{
22
    /**
23
     * @var ResponseInterface|null
24
     */
25
    private $response;
26
27
    /**
28
     * @var array
29
     */
30
    private $responseBody = [];
31
32
    /**
33
     * @var int
34
     */
35
    private $responseCode;
36
37 6
    public function __construct(string $message, int $code, ResponseInterface $response)
38
    {
39 6
        parent::__construct($message, $code);
40
41 6
        $this->response = $response;
42 6
        $this->responseCode = $response->getStatusCode();
43 6
        $body = $response->getBody()->__toString();
44 6
        if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
45 3
            $this->responseBody['message'] = $body;
46
        } else {
47 3
            $this->responseBody = json_decode($body, true);
48
        }
49 6
    }
50
51 2
    public static function badRequest(ResponseInterface $response)
52
    {
53 2
        $body = $response->getBody()->__toString();
54 2
        if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
55 1
            $validationMessage = $body;
56
        } else {
57 1
            $jsonDecoded = json_decode($body, true);
58 1
            $validationMessage = isset($jsonDecoded['message']) ? $jsonDecoded['message'] : $body;
59
        }
60
61 2
        $message = sprintf("The parameters passed to the API were invalid. Check your inputs!\n\n%s", $validationMessage);
62
63 2
        return new self($message, 400, $response);
64
    }
65
66
    public static function unauthorized(ResponseInterface $response)
67
    {
68
        return new self('Your credentials are incorrect.', 401, $response);
69
    }
70
71
    public static function requestFailed(ResponseInterface $response)
72
    {
73
        return new self('Parameters were valid but request failed. Try again.', 402, $response);
74
    }
75
76
    public static function notFound(ResponseInterface $response)
77
    {
78
        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);
79
    }
80
81
    public static function conflict(ResponseInterface $response)
82
    {
83
        return new self('Request conflicts with current state of the target resource.', 409, $response);
84
    }
85
86
    public static function payloadTooLarge(ResponseInterface $response)
87
    {
88
        return new self('Payload too large, your total attachment size is too big.', 413, $response);
89
    }
90
91 1
    public static function tooManyRequests(ResponseInterface $response)
92
    {
93 1
        return new self('Too many requests.', 429, $response);
94
    }
95
96 3
    public static function forbidden(ResponseInterface $response)
97
    {
98 3
        $body = $response->getBody()->__toString();
99 3
        if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
100 1
            $validationMessage = $body;
101
        } else {
102 2
            $jsonDecoded = json_decode($body, true);
103 2
            $validationMessage = isset($jsonDecoded['Error']) ? $jsonDecoded['Error'] : $body;
104
        }
105
106 3
        $message = sprintf("Forbidden!\n\n%s", $validationMessage);
107
108 3
        return new self($message, 403, $response);
109
    }
110
111
    public function getResponse(): ?ResponseInterface
112
    {
113
        return $this->response;
114
    }
115
116
    public function getResponseBody(): array
117
    {
118
        return $this->responseBody;
119
    }
120
121
    public function getResponseCode(): int
122
    {
123
        return $this->responseCode;
124
    }
125
}
126