1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace TMV\Laminas\Messenger\Factory\Middleware; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; |
9
|
|
|
use Symfony\Component\Messenger\Middleware\SendMessageMiddleware; |
10
|
|
|
use TMV\Laminas\Messenger\Exception\InvalidArgumentException; |
11
|
|
|
use TMV\Laminas\Messenger\Factory\Transport\Sender\SendersLocatorFactory; |
12
|
|
|
|
13
|
|
|
use function array_key_exists; |
14
|
|
|
use function sprintf; |
15
|
|
|
|
16
|
|
|
final class SendMessageMiddlewareFactory |
17
|
|
|
{ |
18
|
|
|
private string $busName; |
19
|
3 |
|
|
20
|
|
|
public function __construct(string $busName = 'messenger.bus.default') |
21
|
3 |
|
{ |
22
|
3 |
|
$this->busName = $busName; |
23
|
|
|
} |
24
|
3 |
|
|
25
|
|
|
public function __invoke(ContainerInterface $container): SendMessageMiddleware |
26
|
|
|
{ |
27
|
3 |
|
/** @var array<string, mixed> $config */ |
28
|
|
|
$config = $container->has('config') ? $container->get('config') : []; |
29
|
3 |
|
/** @var string|null $logger */ |
30
|
|
|
$logger = $config['messenger']['logger'] ?? null; |
31
|
3 |
|
/** @var string|null $eventDispatcher */ |
32
|
|
|
$eventDispatcher = $config['messenger']['event_dispatcher'] ?? null; |
33
|
3 |
|
|
34
|
3 |
|
$factory = new SendersLocatorFactory($this->busName); |
35
|
3 |
|
$middleware = new SendMessageMiddleware( |
36
|
3 |
|
$factory($container), |
37
|
|
|
$eventDispatcher ? $container->get($eventDispatcher) : null |
38
|
|
|
); |
39
|
3 |
|
|
40
|
1 |
|
if ($logger && $container->has($logger)) { |
41
|
|
|
$middleware->setLogger($container->get($logger)); |
42
|
|
|
} |
43
|
3 |
|
|
44
|
|
|
return $middleware; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function __set_state(array $data): self |
48
|
|
|
{ |
49
|
|
|
return new self($data['busName'] ?? null); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
/** |
53
|
|
|
* @psalm-api |
54
|
3 |
|
* |
55
|
1 |
|
* @param array<int, mixed> $arguments |
56
|
1 |
|
*/ |
57
|
1 |
|
public static function __callStatic(string $name, array $arguments): MiddlewareInterface |
58
|
|
|
{ |
59
|
|
|
if (! array_key_exists(0, $arguments) || ! $arguments[0] instanceof ContainerInterface) { |
60
|
|
|
throw new InvalidArgumentException(sprintf( |
61
|
2 |
|
'The first argument must be of type %s', |
62
|
|
|
ContainerInterface::class |
63
|
|
|
)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return (new self($name))($arguments[0]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|