Completed
Pull Request — master (#26)
by
unknown
15:31 queued 04:31
created

WebhookServer::getSymfonyResponse()   B

Complexity

Conditions 5
Paths 19

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 22
ccs 0
cts 21
cp 0
rs 8.6737
cc 5
eloc 15
nc 19
nop 2
crap 30
1
<?php
2
3
namespace Xsolla\SDK\Webhook;
4
5
use Symfony\Component\HttpFoundation\Response;
6
use Xsolla\SDK\Exception\Webhook\XsollaWebhookException;
7
use Xsolla\SDK\Webhook\Message\Message;
8
9
class WebhookServer
10
{
11
    /**
12
     * @var WebhookAuthenticator
13
     */
14
    protected $webhookAuthenticator;
15
16
    /**
17
     * @var callable
18
     */
19
    protected $webhookCallback;
20
21
    /**
22
     * @param callable $webhookCallback
23
     * @param string   $projectSecretKey
24
     *
25
     * @return WebhookServer
26
     */
27
    public static function create($webhookCallback, $projectSecretKey)
28
    {
29
        return new static($webhookCallback, new WebhookAuthenticator($projectSecretKey));
30
    }
31
32
    /**
33
     * @param callable             $webhookCallback
34
     * @param WebhookAuthenticator $webhookAuthenticator
35
     *
36
     * @throws XsollaWebhookException
37
     */
38
    public function __construct($webhookCallback, WebhookAuthenticator $webhookAuthenticator)
39
    {
40
        if (!is_callable($webhookCallback)) {
41
            throw new XsollaWebhookException(
42
                '$webhookCallback parameter passed to WebhookServer should be callable. Learn more about callbacks: http://php.net/manual/en/language.types.callable.php'
43
            );
44
        }
45
        $this->webhookCallback = $webhookCallback;
46
        $this->webhookAuthenticator = $webhookAuthenticator;
47
    }
48
49
    /**
50
     * @param WebhookRequest $webhookRequest
51
     * @param bool           $authenticateClientIp
52
     */
53
    public function start(WebhookRequest $webhookRequest = null, $authenticateClientIp = true)
54
    {
55
        $response = $this->getSymfonyResponse($webhookRequest, $authenticateClientIp);
56
        $response->send();
57
    }
58
59
    /**
60
     * @param WebhookRequest $webhookRequest
61
     * @param bool           $authenticateClientIp
62
     *
63
     * @return Response
64
     */
65
    public function getSymfonyResponse(WebhookRequest $webhookRequest = null, $authenticateClientIp = true)
66
    {
67
        try {
68
            if (!$webhookRequest) {
69
                $webhookRequest = WebhookRequest::fromGlobals();
70
            }
71
            $this->webhookAuthenticator->authenticate($webhookRequest, $authenticateClientIp);
72
            $message = Message::fromArray($webhookRequest->toArray());
73
            $webhookResponse = call_user_func($this->webhookCallback, $message);
74
            if ($webhookResponse === null) {
75
                $webhookResponse = new WebhookResponse();
76
            } elseif (!$webhookResponse instanceof WebhookResponse) {
77
                throw new XsollaWebhookException(
78
                    'Webhook callback should return null or \Xsolla\SDK\Webhook\WebhookResponse.'
79
                );
80
            }
81
82
            return $webhookResponse->getSymfonyResponse();
83
        } catch (\Exception $e) {
84
            return WebhookResponse::fromException($e)->getSymfonyResponse();
85
        }
86
    }
87
}
88