51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Diffhead\PHP\LaravelRabbitMQ\Service;
|
|
|
|
use Diffhead\PHP\LaravelRabbitMQ\Exception\AssociatedEventNotFound;
|
|
use Diffhead\PHP\LaravelRabbitMQ\Interface\Event;
|
|
use Diffhead\PHP\LaravelRabbitMQ\Interface\EventMapper as EventMapperInterface;
|
|
use Diffhead\PHP\LaravelRabbitMQ\Object\Queue;
|
|
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
|
|
class EventMapper implements EventMapperInterface
|
|
{
|
|
public function __construct(
|
|
private Application $app
|
|
) {}
|
|
|
|
public function map(Queue $queue, array $payload): Event
|
|
{
|
|
$map = config('rabbitmq.event.map', []);
|
|
|
|
$queueName = $queue->name();
|
|
$routingKey = $queue->bindings()->routingKey();
|
|
|
|
foreach ($map as $eventClass => $config) {
|
|
$match = null;
|
|
|
|
if (! empty($config['queues'] ?? [])) {
|
|
$match = in_array($queueName, $config['queues'], true);
|
|
}
|
|
|
|
if (! empty($config['routing_keys'] ?? [])) {
|
|
$match = is_null($match) ? true : $match;
|
|
$match = $match && in_array($routingKey, $config['routing_keys'], true);
|
|
}
|
|
|
|
if (is_null($match)) {
|
|
return $this->app->make($eventClass, $payload);
|
|
}
|
|
|
|
if (is_bool($match) && $match) {
|
|
return $this->app->make($eventClass, $payload);
|
|
}
|
|
}
|
|
|
|
throw new AssociatedEventNotFound(json_encode($payload));
|
|
}
|
|
}
|