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 2016 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 2016 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\EventSubscriber; |
18
|
|
|
|
19
|
|
|
use JMS\Serializer\SerializerInterface; |
20
|
|
|
use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface; |
21
|
|
|
use SWP\Bundle\ContentBundle\Event\ArticleEvent; |
22
|
|
|
use SWP\Bundle\ContentBundle\Event\RouteEvent; |
23
|
|
|
use SWP\Bundle\CoreBundle\Model\WebhookInterface; |
24
|
|
|
use SWP\Bundle\CoreBundle\Repository\WebhookRepositoryInterface; |
25
|
|
|
use SWP\Bundle\CoreBundle\Webhook\WebhookEvents; |
26
|
|
|
use SWP\Bundle\MultiTenancyBundle\Context\TenantContext; |
27
|
|
|
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface; |
28
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
29
|
|
|
use Symfony\Component\EventDispatcher\Event; |
30
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
31
|
|
|
|
32
|
|
|
final class WebhookEventsSubscriber extends AbstractWebhookEventSubscriber |
33
|
|
|
{ |
34
|
|
|
private $producer; |
35
|
|
|
|
36
|
|
|
private $serializer; |
37
|
|
|
|
38
|
|
|
public function __construct( |
39
|
|
|
ProducerInterface $producer, |
40
|
|
|
SerializerInterface $serializer, |
41
|
|
|
WebhookRepositoryInterface $webhooksRepository, |
42
|
|
|
TenantContext $tenantContext, |
43
|
|
|
TenantRepositoryInterface $tenantRepository |
44
|
|
|
) { |
45
|
|
|
$this->producer = $producer; |
46
|
|
|
$this->serializer = $serializer; |
47
|
|
|
|
48
|
|
|
parent::__construct($webhooksRepository, $tenantContext, $tenantRepository); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public static function getSubscribedEvents(): array |
52
|
|
|
{ |
53
|
|
|
$subscribedEvents = []; |
54
|
|
|
foreach (WebhookEvents::EVENTS as $webhookEvent) { |
55
|
|
|
$subscribedEvents[$webhookEvent] = 'handleEvent'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $subscribedEvents; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function handleEvent(Event $event, string $dispatcherEventName, EventDispatcherInterface $dispatcher): void |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$webhookEventName = $this->getEventName($event); |
64
|
|
|
if (!is_string($webhookEventName)) { |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$subject = $this->getSubject($event); |
69
|
|
|
$webhooks = $this->getWebhooks($subject, $webhookEventName, $dispatcher); |
70
|
|
|
|
71
|
|
|
/** @var WebhookInterface $webhook */ |
72
|
|
|
foreach ($webhooks as $webhook) { |
73
|
|
|
$this->producer->publish($this->serializer->serialize([ |
74
|
|
|
'url' => $webhook->getUrl(), |
75
|
|
|
'metadata' => [ |
76
|
|
|
'event' => $webhookEventName, |
77
|
|
|
'tenant' => $webhook->getTenantCode(), |
78
|
|
|
], |
79
|
|
|
'subject' => $subject, |
80
|
|
|
], 'json')); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function getEventName(Event $event): ?string |
85
|
|
|
{ |
86
|
|
|
if ($event instanceof GenericEvent) { |
87
|
|
|
$arguments = $event->getArguments(); |
88
|
|
|
if (array_key_exists('eventName', $arguments)) { |
89
|
|
|
return array_search($arguments['eventName'], WebhookEvents::EVENTS); |
90
|
|
|
} |
91
|
|
|
} elseif (method_exists($event, 'getEventName')) { |
92
|
|
|
return array_search($event->getEventName(), WebhookEvents::EVENTS); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function getSubject(Event $event) |
99
|
|
|
{ |
100
|
|
|
switch ($event) { |
101
|
|
|
case $event instanceof GenericEvent: |
102
|
|
|
return $event->getSubject(); |
|
|
|
|
103
|
|
|
case $event instanceof ArticleEvent: |
104
|
|
|
return $event->getArticle(); |
|
|
|
|
105
|
|
|
case $event instanceof RouteEvent: |
106
|
|
|
return $event->getRoute(); |
|
|
|
|
107
|
|
|
default: |
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.