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
|
|
|
Assert::isInstanceOf($subject, ArticlePreview::class); |
61
|
|
|
$article = $subject->getArticle(); |
62
|
|
|
|
63
|
|
|
$webhooks = $this->getWebhooks($article, WebhookEvents::PREVIEW_EVENT, $dispatcher); |
64
|
|
|
$headers = []; |
65
|
|
|
|
66
|
|
|
if (!isset($webhooks[0])) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @var WebhookInterface $webhook */ |
71
|
|
|
$webhook = $webhooks[0]; |
72
|
|
|
|
73
|
|
|
$metadata = [ |
74
|
|
|
'event' => WebhookEvents::PREVIEW_EVENT, |
75
|
|
|
'tenant' => $webhook->getTenantCode(), |
|
|
|
|
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
foreach ($metadata as $header => $value) { |
79
|
|
|
$headers['X-WEBHOOK-'.\strtoupper($header)] = $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$client = new Client(); |
83
|
|
|
$requestOptions = [ |
84
|
|
|
'headers' => $headers, |
85
|
|
|
'body' => $this->serializer->serialize($article, 'json'), |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
/** @var \GuzzleHttp\Psr7\Response $response */ |
89
|
|
|
$response = $client->post($webhook->getUrl(), $requestOptions); |
90
|
|
|
$content = $response->getBody()->getContents(); |
91
|
|
|
|
92
|
|
|
$content = json_decode($content, true); |
93
|
|
|
|
94
|
|
|
if (!isset($content['url'])) { |
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($this->isUrlValid($content['url'])) { |
99
|
|
|
$subject->setPreviewUrl($content['url']); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function isUrlValid(string $url): bool |
104
|
|
|
{ |
105
|
|
|
return false !== filter_var($url, FILTER_VALIDATE_URL); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.