|
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
|
|
|
|
|
31
|
|
|
final class PreviewWebhookEventSubscriber extends AbstractWebhookEventSubscriber |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var SerializerInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $serializer; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct( |
|
39
|
|
|
SerializerInterface $serializer, |
|
40
|
|
|
WebhookRepositoryInterface $webhooksRepository, |
|
41
|
|
|
TenantContextInterface $tenantContext, |
|
42
|
|
|
TenantRepositoryInterface $tenantRepository |
|
43
|
|
|
) { |
|
44
|
|
|
$this->serializer = $serializer; |
|
45
|
|
|
|
|
46
|
|
|
parent::__construct($webhooksRepository, $tenantContext, $tenantRepository); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function getSubscribedEvents() |
|
50
|
|
|
{ |
|
51
|
|
|
return [ |
|
52
|
|
|
ArticleEvents::PREVIEW => 'processEvent', |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function processEvent(GenericEvent $event, string $dispatcherEventName, EventDispatcherInterface $dispatcher): void |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
$subject = $event->getSubject(); |
|
59
|
|
|
if (!$subject instanceof ArticlePreview) { |
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$article = $subject->getArticle(); |
|
64
|
|
|
$webhooks = $this->getWebhooks($article, WebhookEvents::PREVIEW_EVENT, $dispatcher); |
|
65
|
|
|
$headers = []; |
|
66
|
|
|
|
|
67
|
|
|
if (!isset($webhooks[0])) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** @var WebhookInterface $webhook */ |
|
72
|
|
|
$webhook = $webhooks[0]; |
|
73
|
|
|
|
|
74
|
|
|
$metadata = [ |
|
75
|
|
|
'event' => WebhookEvents::PREVIEW_EVENT, |
|
76
|
|
|
'tenant' => $webhook->getTenantCode(), |
|
|
|
|
|
|
77
|
|
|
]; |
|
78
|
|
|
|
|
79
|
|
|
foreach ($metadata as $header => $value) { |
|
80
|
|
|
$headers['X-WEBHOOK-'.\strtoupper($header)] = $value; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$client = new Client(); |
|
84
|
|
|
$requestOptions = [ |
|
85
|
|
|
'headers' => $headers, |
|
86
|
|
|
'body' => $this->serializer->serialize($article, 'json'), |
|
87
|
|
|
]; |
|
88
|
|
|
|
|
89
|
|
|
/** @var \GuzzleHttp\Psr7\Response $response */ |
|
90
|
|
|
$response = $client->post($webhook->getUrl(), $requestOptions); |
|
91
|
|
|
$content = $response->getBody()->getContents(); |
|
92
|
|
|
|
|
93
|
|
|
$content = json_decode($content, true); |
|
94
|
|
|
|
|
95
|
|
|
if (!isset($content['url'])) { |
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if ($this->isUrlValid($content['url'])) { |
|
100
|
|
|
$subject->setPreviewUrl($content['url']); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function isUrlValid(string $url): bool |
|
105
|
|
|
{ |
|
106
|
|
|
return false !== filter_var($url, FILTER_VALIDATE_URL); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.