Completed
Push — takeit-patch-2 ( fb795f )
by Rafał
09:58
created

PreviewWebhookEventSubscriber   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 76
rs 10
c 0
b 0
f 0
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 2018 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 2018 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\EventSubscriber;
18
19
use GuzzleHttp\Client;
20
use SWP\Bundle\ContentBundle\ArticleEvents;
21
use SWP\Bundle\CoreBundle\Model\ArticlePreview;
22
use SWP\Bundle\CoreBundle\Repository\WebhookRepositoryInterface;
23
use SWP\Bundle\CoreBundle\Webhook\WebhookEvents;
24
use SWP\Bundle\WebhookBundle\Model\WebhookInterface;
25
use SWP\Component\Common\Serializer\SerializerInterface;
26
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
27
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
28
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
29
use Symfony\Component\EventDispatcher\GenericEvent;
30
use Webmozart\Assert\Assert;
31
32
final class PreviewWebhookEventSubscriber extends AbstractWebhookEventSubscriber
33
{
34
    /**
35
     * @var SerializerInterface
36
     */
37
    private $serializer;
38
39
    public function __construct(
40
        SerializerInterface $serializer,
41
        WebhookRepositoryInterface $webhooksRepository,
42
        TenantContextInterface $tenantContext,
43
        TenantRepositoryInterface $tenantRepository
44
    ) {
45
        $this->serializer = $serializer;
46
47
        parent::__construct($webhooksRepository, $tenantContext, $tenantRepository);
48
    }
49
50
    public static function getSubscribedEvents()
51
    {
52
        return [
53
            ArticleEvents::PREVIEW => 'processEvent',
54
        ];
55
    }
56
57
    public function processEvent(GenericEvent $event, string $dispatcherEventName, EventDispatcherInterface $dispatcher): void
58
    {
59
        $subject = $event->getSubject();
60
        if (!$subject instanceof ArticlePreview::class) {
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_CLASS, expecting T_VARIABLE or '$'
Loading history...
61
            return;
62
        }
63
 
64
        $article = $subject->getArticle();
65
        $webhooks = $this->getWebhooks($article, WebhookEvents::PREVIEW_EVENT, $dispatcher);
66
        $headers = [];
67
68
        if (!isset($webhooks[0])) {
69
            return;
70
        }
71
72
        /** @var WebhookInterface $webhook */
73
        $webhook = $webhooks[0];
74
75
        $metadata = [
76
            'event' => WebhookEvents::PREVIEW_EVENT,
77
            'tenant' => $webhook->getTenantCode(),
78
        ];
79
80
        foreach ($metadata as $header => $value) {
81
            $headers['X-WEBHOOK-'.\strtoupper($header)] = $value;
82
        }
83
84
        $client = new Client();
85
        $requestOptions = [
86
            'headers' => $headers,
87
            'body' => $this->serializer->serialize($article, 'json'),
88
        ];
89
90
        /** @var \GuzzleHttp\Psr7\Response $response */
91
        $response = $client->post($webhook->getUrl(), $requestOptions);
92
        $content = $response->getBody()->getContents();
93
94
        $content = json_decode($content, true);
95
96
        if (!isset($content['url'])) {
97
            return;
98
        }
99
100
        if ($this->isUrlValid($content['url'])) {
101
            $subject->setPreviewUrl($content['url']);
102
        }
103
    }
104
105
    private function isUrlValid(string $url): bool
106
    {
107
        return false !== filter_var($url, FILTER_VALIDATE_URL);
108
    }
109
}
110