SendMessageMiddlewareFactory   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 51
rs 10
ccs 20
cts 20
cp 1
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 20 5
A __callStatic() 0 10 3
A __set_state() 0 3 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\MiddlewareInterface;
9
use Symfony\Component\Messenger\Middleware\SendMessageMiddleware;
10
use TMV\Laminas\Messenger\Exception\InvalidArgumentException;
11
use TMV\Laminas\Messenger\Factory\Transport\Sender\SendersLocatorFactory;
12
13
use function array_key_exists;
14
use function sprintf;
15
16
final class SendMessageMiddlewareFactory
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): SendMessageMiddleware
26
    {
27 3
        /** @var array<string, mixed> $config */
28
        $config = $container->has('config') ? $container->get('config') : [];
29 3
        /** @var string|null $logger */
30
        $logger = $config['messenger']['logger'] ?? null;
31 3
        /** @var string|null $eventDispatcher */
32
        $eventDispatcher = $config['messenger']['event_dispatcher'] ?? null;
33 3
34 3
        $factory = new SendersLocatorFactory($this->busName);
35 3
        $middleware = new SendMessageMiddleware(
36 3
            $factory($container),
37
            $eventDispatcher ? $container->get($eventDispatcher) : null
38
        );
39 3
40 1
        if ($logger && $container->has($logger)) {
41
            $middleware->setLogger($container->get($logger));
42
        }
43 3
44
        return $middleware;
45
    }
46
47
    public static function __set_state(array $data): self
48
    {
49
        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

49
        return new self(/** @scrutinizer ignore-type */ $data['busName'] ?? null);
Loading history...
50
    }
51
52 3
    /**
53
     * @psalm-api
54 3
     *
55 1
     * @param array<int, mixed> $arguments
56 1
     */
57 1
    public static function __callStatic(string $name, array $arguments): MiddlewareInterface
58
    {
59
        if (! array_key_exists(0, $arguments) || ! $arguments[0] instanceof ContainerInterface) {
60
            throw new InvalidArgumentException(sprintf(
61 2
                'The first argument must be of type %s',
62
                ContainerInterface::class
63
            ));
64
        }
65
66
        return (new self($name))($arguments[0]);
67
    }
68
}
69