HandleMessageMiddlewareFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
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);
0 ignored issues
show
Bug introduced by
It seems like $data['busName'] ?? null can also be of type null; however, parameter $busName of TMV\Laminas\Messenger\Fa...eFactory::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        return new self(/** @scrutinizer ignore-type */ $data['busName'] ?? null);
Loading history...
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