Completed
Push — master ( 5cad52...7b674d )
by David
01:24
created

HttpClientException::forbidden()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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 5
    public function __construct(string $message, int $code, ResponseInterface $response)
38
    {
39 5
        parent::__construct($message, $code);
40
41 5
        $this->response = $response;
42 5
        $this->responseCode = $response->getStatusCode();
43 5
        $body = $response->getBody()->__toString();
44 5
        if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
45 2
            $this->responseBody['message'] = $body;
46
        } else {
47 3
            $this->responseBody = json_decode($body, true);
48
        }
49 5
    }
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 payloadTooLarge(ResponseInterface $response)
82
    {
83
        return new self('Payload too large, your total attachment size is too big.', 413, $response);
84
    }
85
86 3
    public static function forbidden(ResponseInterface $response)
87
    {
88 3
        $body = $response->getBody()->__toString();
89 3
        if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
90 1
            $validationMessage = $body;
91
        } else {
92 2
            $jsonDecoded = json_decode($body, true);
93 2
            $validationMessage = isset($jsonDecoded['Error']) ? $jsonDecoded['Error'] : $body;
94
        }
95
96 3
        $message = sprintf("Forbidden!\n\n%s", $validationMessage);
97
98 3
        return new self($message, 403, $response);
99
    }
100
101
    public function getResponse(): ?ResponseInterface
102
    {
103
        return $this->response;
104
    }
105
106
    public function getResponseBody(): array
107
    {
108
        return $this->responseBody;
109
    }
110
111
    public function getResponseCode(): int
112
    {
113
        return $this->responseCode;
114
    }
115
}
116