Passed
Push — develop ( 38ffd9...9f7101 )
by Jens
31:57 queued 05:22
created

ApiException::getErrors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Commercetools\Core\Error;
4
5
use Exception;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use function GuzzleHttp\Psr7\stream_for;
9
10
/**
11
 * Base exception for responses with http status code different than 200 or 201
12
 * @package Commercetools\Core\Error
13
 */
14
class ApiException extends Exception
15
{
16
    /**
17
     * @var RequestInterface
18
     */
19
    protected $request;
20
21
    /**
22
     * @var ResponseInterface
23
     */
24
    protected $response;
25
26 226
    public function __construct(
27
        $message,
28
        RequestInterface $request,
29
        ResponseInterface $response = null,
30
        Exception $previous = null
31
    ) {
32 226
        $code = $response ? $response->getStatusCode() : ($previous ? $previous->getCode() : 0);
33 226
        parent::__construct($message, $code, $previous);
34 226
        $this->request = $request;
35 226
        $this->response = $response;
36 226
    }
37
38
    /**
39
     * @param RequestInterface $request
40
     * @param ResponseInterface|null $response
41
     * @param Exception|null $previous
42
     */
43 226
    public static function create(
44
        RequestInterface $request,
45
        ResponseInterface $response = null,
46
        Exception $previous = null
47
    ) {
48 226
        if (is_null($response)) {
49 2
            $message = $previous ? 'Error completing request: ' . $previous->getMessage() : "Error completing request";
50 2
            return new self($message, $request, null, $previous);
51
        }
52
53 224
        $level = floor($response->getStatusCode() / 100);
54 224
        if ($level == 4) {
55 215
            $label = 'Client error response';
56 9
        } elseif ($level == 5) {
57 9
            $label = 'Server error response';
58
        } else {
59
            $label = 'Unsuccessful response';
60
        }
61
62 224
        $message = $label . ' [url] ' . $request->getUri()
63 224
             . ' [status code] ' . $response->getStatusCode()
64 224
             . ' [reason phrase] ' . $response->getReasonPhrase();
65
66 224
        switch ($response->getStatusCode()) {
67 224
            case 400:
68 59
                if ($response->getBody()->getSize() > 0) {
69 58
                    $body = (string)$response->getBody()->getContents();
70 58
                    $response = $response->withBody(stream_for($body));
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Psr7\stream_for() has been deprecated: stream_for will be removed in guzzlehttp/psr7:2.0. Use Utils::streamFor instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

70
                    $response = $response->withBody(/** @scrutinizer ignore-deprecated */ stream_for($body));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
71 58
                    $message .= ' [body] ' . $body;
72
                }
73 59
                return new ErrorResponseException($message, $request, $response, $previous);
74 186
            case 401:
75 10
                $body = $response->getBody()->getContents();
76 10
                $response = $response->withBody(stream_for($body));
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Psr7\stream_for() has been deprecated: stream_for will be removed in guzzlehttp/psr7:2.0. Use Utils::streamFor instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

76
                $response = $response->withBody(/** @scrutinizer ignore-deprecated */ stream_for($body));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
77 10
                if (strpos($body, 'invalid_token') !== false) {
78 6
                    return new InvalidTokenException($message, $request, $response, $previous);
79
                }
80 4
                return new InvalidClientCredentialsException($message, $request, $response, $previous);
81 176
            case 403:
82 2
                return new ForbiddenException($message, $request, $response, $previous);
83 174
            case 404:
84 28
                return new NotFoundException($message, $request, $response, $previous);
85 150
            case 409:
86 139
                return new ConcurrentModificationException($message, $request, $response, $previous);
87 11
            case 500:
88 6
                return new InternalServerErrorException($message, $request, $response, $previous);
89 5
            case 502:
90 1
                return new BadGatewayException($message, $request, $response, $previous);
91 4
            case 503:
92 1
                return new ServiceUnavailableException($message, $request, $response, $previous);
93 3
            case 504:
94 1
                return new GatewayTimeoutException($message, $request, $response, $previous);
95
        }
96
97 2
        return new self($message, $request, $response, $previous);
98
    }
99
100
    /**
101
     * @return RequestInterface
102
     */
103 3
    public function getRequest()
104
    {
105 3
        return $this->request;
106
    }
107
108
    /**
109
     * @return ResponseInterface
110
     */
111 208
    public function getResponse()
112
    {
113 208
        return $this->response;
114
    }
115
116 139
    protected function getResponseJson()
117
    {
118 139
        $response = $this->getResponse();
119 139
        if ($response instanceof ResponseInterface) {
0 ignored issues
show
introduced by
$response is always a sub-type of Psr\Http\Message\ResponseInterface.
Loading history...
120 139
            $json = json_decode($response->getBody(), true);
121 139
            return $json;
122
        }
123
124
        return [];
125
    }
126
127
    /**
128
     * @return array
129
     */
130 139
    public function getErrors()
131
    {
132 139
        $data = $this->getResponseJson();
133
134 139
        return isset($data['errors']) ? $data['errors'] : [];
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getResponseMessage()
141
    {
142
        $data = $this->getResponseJson();
143
144
        return isset($data['message']) ? $data['message'] : '';
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getResponseStatusCode()
151
    {
152
        $data = $this->getResponseJson();
153
154
        return isset($data['statusCode']) ? $data['statusCode'] : '';
155
    }
156
}
157