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