Completed
Push — master ( 087020...368970 )
by
unknown
03:33
created

XsollaAPIException   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 2
dl 0
loc 44
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromBadResponse() 0 15 2
1
<?php
2
3
namespace Xsolla\SDK\Exception\API;
4
5
use GuzzleHttp\Exception\BadResponseException;
6
use Xsolla\SDK\Exception\XsollaException;
7
8
class XsollaAPIException extends XsollaException
9
{
10
    protected static $exceptions = [
11
        422 => '\Xsolla\SDK\Exception\API\UnprocessableEntityException',
12
        403 => '\Xsolla\SDK\Exception\API\AccessDeniedException',
13
    ];
14
15
    protected static $messageTemplate =
16
<<<'EOF'
17
Xsolla API Error Response:
18
19
Previous Exception:
20
===================
21
%s
22
23
Request:
24
===================
25
%s
26
27
Response:
28
===================
29
%s
30
EOF;
31
32
    /**
33
     * @param  BadResponseException $previous
34
     * @return XsollaAPIException
35
     */
36 1
    public static function fromBadResponse(BadResponseException $previous)
37
    {
38 1
        $statusCode = $previous->getResponse()->getStatusCode();
39 1
        $message = sprintf(
40 1
            static::$messageTemplate,
41 1
            $previous->getMessage(),
42 1
            $previous->getRequest(),
43 1
            $previous->getResponse()
44
        );
45 1
        if (array_key_exists($statusCode, static::$exceptions)) {
46 1
            return new static::$exceptions[$statusCode]($message, 0, $previous);
47
        }
48
49
        return new self($message, 0, $previous);
50
    }
51
}
52