WebhookServer::start()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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('$webhookCallback parameter passed to WebhookServer should be callable. Learn more about callbacks: http://php.net/manual/en/language.types.callable.php');
42
        }
43
        $this->webhookCallback = $webhookCallback;
44
        $this->webhookAuthenticator = $webhookAuthenticator;
45
    }
46
47
    /**
48
     * @param WebhookRequest $webhookRequest
49
     * @param bool           $authenticateClientIp
50
     */
51
    public function start(WebhookRequest $webhookRequest = null, $authenticateClientIp = true)
52
    {
53
        $response = $this->getSymfonyResponse($webhookRequest, $authenticateClientIp);
54
        $response->send();
55
    }
56
57
    /**
58
     * @param WebhookRequest $webhookRequest
59
     * @param bool           $authenticateClientIp
60
     *
61
     * @return Response
62
     */
63
    public function getSymfonyResponse(WebhookRequest $webhookRequest = null, $authenticateClientIp = true)
64
    {
65
        try {
66
            if (!$webhookRequest) {
67
                $webhookRequest = WebhookRequest::fromGlobals();
68
            }
69
            $this->webhookAuthenticator->authenticate($webhookRequest, $authenticateClientIp);
70
            $message = Message::fromArray($webhookRequest->toArray());
71
            $webhookResponse = call_user_func($this->webhookCallback, $message);
72
            if (!$webhookResponse instanceof WebhookResponse) {
73
                $webhookResponse = new WebhookResponse();
74
            }
75
76
            return $webhookResponse->getSymfonyResponse();
77
        } catch (\Exception $e) {
78
            return WebhookResponse::fromException($e)->getSymfonyResponse();
79
        }
80
    }
81
}
82