-
Notifications
You must be signed in to change notification settings - Fork 56
/
OnEventStrategy.php
53 lines (45 loc) · 1.8 KB
/
OnEventStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
* This file is part of prooph/service-bus.
* (c) 2014-2021 Alexander Miertsch <kontakt@codeliner.ws>
* (c) 2015-2021 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Prooph\ServiceBus\Plugin\InvokeStrategy;
use Prooph\Common\Event\ActionEvent;
use Prooph\ServiceBus\EventBus;
use Prooph\ServiceBus\MessageBus;
use Prooph\ServiceBus\Plugin\AbstractPlugin;
final class OnEventStrategy extends AbstractPlugin
{
public function attachToMessageBus(MessageBus $messageBus): void
{
$this->listenerHandlers[] = $messageBus->attach(
MessageBus::EVENT_DISPATCH,
function (ActionEvent $actionEvent): void {
$target = $actionEvent->getTarget();
$message = $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE);
$handlers = $actionEvent->getParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, []);
foreach ($handlers as $handler) {
if (\is_callable($handler) || ! \is_object($handler) || ! \is_callable([$handler, 'onEvent'])) {
continue;
}
try {
$handler->onEvent($message);
} catch (\Throwable $e) {
if ($target->isCollectingExceptions()) {
$target->addCollectedException($e);
} else {
throw $e;
}
}
}
$actionEvent->setParam(MessageBus::EVENT_PARAM_MESSAGE_HANDLED, true);
},
MessageBus::PRIORITY_INVOKE_HANDLER
);
}
}