|
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\Model\TenantAwareInterface; |
|
28
|
|
|
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface; |
|
29
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
30
|
|
|
use Symfony\Component\EventDispatcher\Event; |
|
31
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
|
32
|
|
|
|
|
33
|
|
|
final class WebhookEventsSubscriber implements EventSubscriberInterface |
|
34
|
|
|
{ |
|
35
|
|
|
private $producer; |
|
36
|
|
|
|
|
37
|
|
|
private $serializer; |
|
38
|
|
|
|
|
39
|
|
|
private $webhooksRepository; |
|
40
|
|
|
|
|
41
|
|
|
private $tenantContext; |
|
42
|
|
|
|
|
43
|
|
|
private $tenantRepository; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct( |
|
46
|
|
|
ProducerInterface $producer, |
|
47
|
|
|
SerializerInterface $serializer, |
|
48
|
|
|
WebhookRepositoryInterface $webhooksRepository, |
|
49
|
|
|
TenantContext $tenantContext, |
|
50
|
|
|
TenantRepositoryInterface $tenantRepository |
|
51
|
|
|
) { |
|
52
|
|
|
$this->producer = $producer; |
|
53
|
|
|
$this->serializer = $serializer; |
|
54
|
|
|
$this->webhooksRepository = $webhooksRepository; |
|
55
|
|
|
$this->tenantContext = $tenantContext; |
|
56
|
|
|
$this->tenantRepository = $tenantRepository; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function getSubscribedEvents(): array |
|
60
|
|
|
{ |
|
61
|
|
|
$subscribedEvents = []; |
|
62
|
|
|
foreach (WebhookEvents::EVENTS as $webhookEvent) { |
|
63
|
|
|
$subscribedEvents[$webhookEvent] = 'handleEvent'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $subscribedEvents; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function handleEvent(Event $event): void |
|
70
|
|
|
{ |
|
71
|
|
|
$eventName = $this->getEventName($event); |
|
72
|
|
|
if (!is_string($eventName)) { |
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$subject = $this->getSubject($event); |
|
77
|
|
|
$webhooks = $this->getWebhooks($event, $subject); |
|
78
|
|
|
|
|
79
|
|
|
/** @var WebhookInterface $webhook */ |
|
80
|
|
|
foreach ($webhooks as $webhook) { |
|
81
|
|
|
$this->producer->publish($this->serializer->serialize([ |
|
82
|
|
|
'url' => $webhook->getUrl(), |
|
83
|
|
|
'metadata' => [ |
|
84
|
|
|
'event' => $this->getEventName($event), |
|
85
|
|
|
'tenant' => $webhook->getTenantCode(), |
|
86
|
|
|
], |
|
87
|
|
|
'subject' => $subject, |
|
88
|
|
|
], 'json')); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function getWebhooks(Event $event, $subject): array |
|
93
|
|
|
{ |
|
94
|
|
|
$originalTenant = null; |
|
95
|
|
|
|
|
96
|
|
|
if ( |
|
97
|
|
|
$subject instanceof TenantAwareInterface |
|
98
|
|
|
&& $subject->getTenantCode() !== $this->tenantContext->getTenant()->getCode() |
|
99
|
|
|
&& null !== $subject->getTenantCode() |
|
100
|
|
|
&& null !== ($subjectTenant = $this->tenantRepository->findOneByCode($subject->getTenantCode())) |
|
101
|
|
|
) { |
|
102
|
|
|
$originalTenant = $this->tenantContext->getTenant(); |
|
103
|
|
|
$this->tenantContext->setTenant($subjectTenant); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$webhooks = $this->webhooksRepository->getEnabledForEvent($this->getEventName($event))->getResult(); |
|
107
|
|
|
|
|
108
|
|
|
if (null !== $originalTenant) { |
|
109
|
|
|
$this->tenantContext->setTenant($originalTenant); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $webhooks; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function getEventName(Event $event): ?string |
|
116
|
|
|
{ |
|
117
|
|
|
if ($event instanceof GenericEvent) { |
|
118
|
|
|
$arguments = $event->getArguments(); |
|
119
|
|
|
if (array_key_exists('eventName', $arguments)) { |
|
120
|
|
|
return array_search($arguments['eventName'], WebhookEvents::EVENTS); |
|
121
|
|
|
} |
|
122
|
|
|
} elseif (method_exists($event, 'getEventName')) { |
|
123
|
|
|
return array_search($event->getEventName(), WebhookEvents::EVENTS); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return null; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
private function getSubject(Event $event) |
|
130
|
|
|
{ |
|
131
|
|
|
switch ($event) { |
|
132
|
|
|
case $event instanceof GenericEvent: |
|
133
|
|
|
return $event->getSubject(); |
|
|
|
|
|
|
134
|
|
|
case $event instanceof ArticleEvent: |
|
135
|
|
|
return $event->getArticle(); |
|
|
|
|
|
|
136
|
|
|
case $event instanceof RouteEvent: |
|
137
|
|
|
return $event->getRoute(); |
|
|
|
|
|
|
138
|
|
|
default: |
|
139
|
|
|
return null; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: