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