HandleMessageMiddlewareFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 44
ccs 18
cts 18
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 16 3
A __callStatic() 0 10 3
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