Completed
Push — list_events_limit_parameter_an... ( 671774 )
by Vitaliy
05:09 queued 02:50
created

XsollaAPIException::fromBadResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 0
cts 10
cp 0
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Xsolla\SDK\Exception\API;
4
5
use Guzzle\Http\Exception\BadResponseException;
6
use Xsolla\SDK\Exception\XsollaException;
7
8
class XsollaAPIException extends XsollaException
9
{
10
    protected static $exceptions = array(
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
    public static function fromBadResponse(BadResponseException $previous)
37
    {
38
        $statusCode = $previous->getResponse()->getStatusCode();
39
        $message = sprintf(
40
            static::$messageTemplate,
41
            $previous->getMessage(),
42
            $previous->getRequest(),
43
            $previous->getResponse()
44
        );
45
        if (array_key_exists($statusCode, static::$exceptions)) {
46
            return new static::$exceptions[$statusCode]($message, 0, $previous);
47
        }
48
49
        return new self($message, 0, $previous);
50
    }
51
}
52