Completed
Push — master ( 66631d...a00063 )
by
unknown
9s
created

WebhookResponse   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 62.96%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 10
c 2
b 0
f 1
lcom 0
cbo 5
dl 0
loc 89
rs 10
ccs 17
cts 27
cp 0.6296

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromException() 0 8 2
A getSymfonyResponse() 0 4 1
A fromErrorCode() 0 12 1
A __construct() 0 11 2
A validateStringParameter() 0 13 4
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $body of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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