-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPublisher.php
32 lines (28 loc) · 1.15 KB
/
Publisher.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
<?php
namespace Samples\UserService\Messaging;
use Micronative\EventSchema\ValidatorInterface;
use Samples\UserService\Events\UserEvent;
use Micronative\MockBroker\PublisherInterface as MockPublisherInterface;
class Publisher implements PublisherInterface
{
private ValidatorInterface $validator;
private MockPublisherInterface $publisher;
/**
* @param MockPublisherInterface $publisher
* @param ValidatorInterface|null $validator
*/
public function __construct(MockPublisherInterface $publisher, ValidatorInterface $validator = null)
{
$this->validator = $validator;
$this->publisher = $publisher;
}
public function publishEvent(UserEvent $userEvent)
{
echo "-- Validating outgoing event message: {$userEvent->getName()}" . PHP_EOL;
if ($this->validator->validate($userEvent, true)) {
echo "-- Start publishing event message to broker: {$userEvent->getName()}" . PHP_EOL;
$this->publisher->publish($userEvent->toJson(), UserEvent::USER_EVENT_TOPIC);
echo "-- Finish publishing event message to broker: {$userEvent->getName()}" . PHP_EOL;
}
}
}