Symfony - Event handler no se invoca por Event

 
Vista:

Event handler no se invoca por Event

Publicado por Michel Andreu (1 intervención) el 30/08/2022 19:12:57
Hola, estoy trabajando con Symfony 5 tengo este evento creado:

class LeadWasCreated implements Event
{
public function __construct(
private Uuid $clientUuid,
private Uuid $leadUuid,
private DateTimeInterface $occurredOn,
private Uuid $eventId,
private Uuid $causationId,
private Uuid $correlationId,
) {
}

public static function create(
Uuid $clientUuid,
Uuid $leadUuid
): static {
$uuid = Uuid::generate();

$event = new static(
clientUuid: $clientUuid,
leadUuid: $leadUuid,
occurredOn: new DateTimeImmutable(),
eventId: $uuid,
causationId: $uuid,
correlationId: $uuid
);

return $event;
}
...
}

El cual se lanza cuando se crea un registro de este modelo:

class Lead
{
/**
* @param int|null $id
* @param Uuid $uuid
* @param string|null $name
* @param string|null $notes
* @param DateTimeInterface $createdAt
* @param DateTimeInterface $updatedAt
* @param DateTimeInterface|null $deletedAt
* @param bool $isDeleted
*/
public function __construct(
private ?int $id,
private Uuid $uuid,
private ?string $name,
private ?string $notes,
private DateTimeInterface $createdAt,
private DateTimeInterface $updatedAt,
private ?DateTimeInterface $deletedAt,
private bool $isDeleted,
) {
}

public static function create(Client $client): static
{
$now = new DateTimeImmutable();

$lead = new static(
id: null,
uuid: Uuid::generate(),
name: null,
notes: null,
createdAt: $now,
updatedAt: $now,
deletedAt: null,
isDeleted: false
);

EventDispatcher::instance()->dispatch(
LeadWasCreated::create(
clientUuid: $client->getUuid(),
leadUuid: $lead->getUuid()
)
);

return $lead;
}
...
}

Se puede ver como en el create se hace el dispatch del evento.
Este es el handler que tengo:

class ProcessWebhookWhenLeadsWasCreated implements EventHandler
{


public function __construct(
// private readonly WebhookRepository $webhookRepository,
) {


}

/**
* @throws ClientNotFoundException
* @throws TrackingLabelDictionaryNotFound
* @throws InteractionNotFoundException
*/
public function __invoke(LeadWasCreated $event): void
{
var_dump("lanzado Lead Was Created !!!");
$clientUuid = Uuid::fromString('10b830f5-fc5c-4528-a29a-0fc70f8d42dc');
$client = $this->getClientByUuidService->execute($clientUuid);

// $obj = Webhook::create("test","testing",$client);
// $this->webhookRepository->save($obj);

}
}

Se supone que cuando se crea un nuevo registro de ese modelo se lance el evento y se invoque el handler, pero el handler no se invoca, he revisado el messenger.yaml y el services.yaml y todo parece OK

#messenger.yaml

framework:
messenger:
# Uncomment this (and the failed transport below) to send failed messages to this transport for later handling.
# failure_transport: failed
default_bus: event.bus

serializer:
symfony_serializer:
format: json
context: { }

buses:
default.bus:

command.bus:
middleware:
- messenger.middleware.muticonn_doctrine_transaction
- messenger.middleware.event_dispatcher

query.bus:
middleware:
- messenger.middleware.event_dispatcher

event.bus:
default_middleware: allow_no_handlers
middleware:
- messenger.middleware.muticonn_doctrine_transaction
- messenger.middleware.event_dispatcher

# services.yaml

...

_instanceof:
# Messenger Component
App\Api\Framework\MessageBus\Command\CommandHandler:
tags: [ { name: messenger.message_handler, bus: command.bus } ]
App\Api\Framework\MessageBus\Query\QueryHandler:
tags: [ { name: messenger.message_handler, bus: query.bus } ]
App\Api\Framework\MessageBus\Event\EventHandler:
tags: [ { name: messenger.message_handler, bus: event.bus } ]

Se agradece la ayuda
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder