|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xsolla\SDK\Webhook; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
6
|
|
|
use Xsolla\SDK\API\XsollaClient; |
|
7
|
|
|
use Xsolla\SDK\Exception\Webhook\XsollaWebhookException; |
|
8
|
|
|
use Xsolla\SDK\Version; |
|
9
|
|
|
|
|
10
|
|
|
class WebhookResponse |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var int |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $httpStatusCode; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $body; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Response |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $symfonyResponse; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param \Exception $e |
|
29
|
|
|
* |
|
30
|
|
|
* @return WebhookResponse |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function fromException(\Exception $e) |
|
33
|
|
|
{ |
|
34
|
|
|
if ($e instanceof XsollaWebhookException) { |
|
35
|
|
|
return static::fromErrorCode($e->getXsollaErrorCode(), $e->getMessage(), $e->getHttpStatusCode()); |
|
36
|
|
|
} else { |
|
37
|
|
|
return static::fromErrorCode('FATAL_ERROR', $e->getMessage()); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $xsollaErrorCode |
|
43
|
|
|
* @param string $message |
|
44
|
|
|
* @param int $httpStatus |
|
45
|
|
|
* |
|
46
|
|
|
* @return WebhookResponse |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function fromErrorCode($xsollaErrorCode, $message = '', $httpStatus = 500) |
|
49
|
|
|
{ |
|
50
|
|
|
$body = array( |
|
51
|
|
|
'error' => array( |
|
52
|
|
|
'code' => $xsollaErrorCode, |
|
53
|
|
|
'message' => $message, |
|
54
|
|
|
), |
|
55
|
|
|
); |
|
56
|
|
|
$encodedBody = XsollaClient::jsonEncode($body); |
|
57
|
|
|
|
|
58
|
|
|
return new static($httpStatus, $encodedBody); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param int $httpStatusCode |
|
63
|
|
|
* @param string $body |
|
64
|
|
|
*/ |
|
65
|
3 |
|
public function __construct($httpStatusCode = 204, $body = null) |
|
66
|
|
|
{ |
|
67
|
3 |
|
$this->symfonyResponse = new Response($body, $httpStatusCode); |
|
68
|
3 |
|
$this->symfonyResponse->headers->set('x-xsolla-sdk', Version::getVersion()); |
|
69
|
3 |
|
if ($body) { |
|
|
|
|
|
|
70
|
3 |
|
$contentType = 'application/json'; |
|
71
|
|
|
} else { |
|
72
|
|
|
$contentType = 'text/plain'; |
|
73
|
|
|
} |
|
74
|
3 |
|
$this->symfonyResponse->headers->set('content-type', $contentType); |
|
75
|
3 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return Response |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function getSymfonyResponse() |
|
81
|
|
|
{ |
|
82
|
3 |
|
return $this->symfonyResponse; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
9 |
|
protected function validateStringParameter($name, $value) |
|
86
|
|
|
{ |
|
87
|
9 |
|
if (!is_string($value)) { |
|
88
|
4 |
|
throw new XsollaWebhookException(sprintf( |
|
89
|
4 |
|
'%s should be non-empty string. %s given', |
|
90
|
|
|
$name, |
|
91
|
4 |
|
is_object($value) ? get_class($value) : gettype($value) |
|
92
|
|
|
)); |
|
93
|
|
|
} |
|
94
|
5 |
|
if ('' === $value) { |
|
95
|
2 |
|
throw new XsollaWebhookException($name.' should be non-empty string. Empty string given'); |
|
96
|
|
|
} |
|
97
|
3 |
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: