Broadcastable event shouldnt be a JsonSerializable everytime
This commit is contained in:
@@ -33,8 +33,9 @@ return [
|
||||
|
||||
### 1. Creating regular events for publishing to a RabbitMQ
|
||||
|
||||
Create an event that implements the `Broadcast` interface. `Broadcast` interface
|
||||
extends `JsonSerializable` then your event should implements `jsonSerialize` method.
|
||||
Create an event that implements the `Broadcast` interface.
|
||||
Your event should implements `JsonSerializable` interface if
|
||||
default serializer is used.
|
||||
|
||||
```php
|
||||
namespace App\Events;
|
||||
@@ -42,8 +43,9 @@ namespace App\Events;
|
||||
use Diffhead\PHP\LaravelRabbitMQ\Event\Broadcast;
|
||||
use Diffhead\PHP\LaravelRabbitMQ\Trait\BroadcastEvent;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use JsonSerializable;
|
||||
|
||||
class UserCreated implements Broadcast
|
||||
class UserCreated implements Broadcast, JsonSerializable
|
||||
{
|
||||
use Dispatchable, BroadcastEvent;
|
||||
|
||||
|
||||
@@ -4,9 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Diffhead\PHP\LaravelRabbitMQ\Event;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
interface Broadcast extends JsonSerializable
|
||||
interface Broadcast
|
||||
{
|
||||
public function getConnection(): string;
|
||||
public function getQueue(): string;
|
||||
|
||||
@@ -11,16 +11,16 @@ use PhpAmqpLib\Message\AMQPMessage;
|
||||
|
||||
class Serializer implements SerializerInterface
|
||||
{
|
||||
public function serialize(object $data): AMQPMessage
|
||||
public function serialize(object $event): AMQPMessage
|
||||
{
|
||||
if ($data instanceof JsonSerializable) {
|
||||
if ($event instanceof JsonSerializable) {
|
||||
return new AMQPMessage(
|
||||
json_encode($data->jsonSerialize())
|
||||
json_encode($event->jsonSerialize())
|
||||
);
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException(
|
||||
'Data should be an instance of BroadcastEvent'
|
||||
'Event should be an instance of JsonSerializable'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user