Version 1.0.0
This commit is contained in:
49
src/Service/EventMapper.php
Normal file
49
src/Service/EventMapper.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Diffhead\PHP\LaravelRabbitMQ\Service;
|
||||
|
||||
use Diffhead\PHP\LaravelRabbitMQ\Exception\AssociatedEventNotFound;
|
||||
use Diffhead\PHP\LaravelRabbitMQ\Interface\EventMapper as EventMapperInterface;
|
||||
use Diffhead\PHP\LaravelRabbitMQ\Object\Queue;
|
||||
use App\Shared\Event\Event;
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user