Completed
Push — master ( f785fb...b21f25 )
by Rafał
09:51
created

WebhookHandler::__invoke()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2020 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2020 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\MessageHandler;
18
19
use GuzzleHttp\Client;
20
use GuzzleHttp\Psr7\Request;
21
use SWP\Bundle\CoreBundle\Webhook\Message\WebhookMessage;
22
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
23
24
class WebhookHandler implements MessageHandlerInterface
25
{
26
    public function __invoke(WebhookMessage $webhookMessage)
27
    {
28
        $headers = ['content-type' => 'application/json'];
29
        if (!empty($metadata = $webhookMessage->getMetadata())) {
30
            foreach ($metadata as $header => $value) {
31
                $headers['X-WEBHOOK-'.\strtoupper($header)] = $value;
32
            }
33
        }
34
35
        $webhookRequest = new Request(
36
            'POST',
37
            $webhookMessage->getUrl(),
38
            $headers,
39
            $webhookMessage->getBody()
40
        );
41
42
        $this->getClient()->send($webhookRequest);
43
    }
44
45
    protected function getClient(): Client
46
    {
47
        return new Client();
48
    }
49
}
50