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\HandleMessageMiddleware; |
9
|
|
|
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; |
10
|
|
|
use TMV\Laminas\Messenger\Exception\InvalidArgumentException; |
11
|
|
|
use TMV\Laminas\Messenger\Factory\Handler\HandlersLocatorFactory; |
12
|
|
|
|
13
|
|
|
use function array_key_exists; |
14
|
|
|
use function sprintf; |
15
|
|
|
|
16
|
|
|
final class HandleMessageMiddlewareFactory |
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): HandleMessageMiddleware |
26
|
|
|
{ |
27
|
3 |
|
/** @var array<string, mixed> $config */ |
28
|
|
|
$config = $container->has('config') ? $container->get('config') : []; |
29
|
3 |
|
/** @var string|null $logger */ |
30
|
3 |
|
$logger = $config['messenger']['logger'] ?? null; |
31
|
|
|
$allowNoHandlers = (bool) ($config['messenger']['buses'][$this->busName]['allow_no_handler'] ?? false); |
32
|
3 |
|
|
33
|
3 |
|
$handlersLocator = (new HandlersLocatorFactory($this->busName))($container); |
34
|
|
|
$middleware = new HandleMessageMiddleware($handlersLocator, $allowNoHandlers); |
35
|
3 |
|
|
36
|
1 |
|
if ($logger && $container->has($logger)) { |
37
|
|
|
$middleware->setLogger($container->get($logger)); |
38
|
|
|
} |
39
|
3 |
|
|
40
|
|
|
return $middleware; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function __set_state(array $data): self |
44
|
|
|
{ |
45
|
|
|
return new self($data['busName'] ?? null); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
/** |
49
|
|
|
* @psalm-api |
50
|
3 |
|
* |
51
|
1 |
|
* @param array<int, mixed> $arguments |
52
|
1 |
|
*/ |
53
|
1 |
|
public static function __callStatic(string $name, array $arguments): MiddlewareInterface |
54
|
|
|
{ |
55
|
|
|
if (! array_key_exists(0, $arguments) || ! $arguments[0] instanceof ContainerInterface) { |
56
|
|
|
throw new InvalidArgumentException(sprintf( |
57
|
2 |
|
'The first argument must be of type %s', |
58
|
|
|
ContainerInterface::class |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return (new self($name))($arguments[0]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|