RoutableMessageBusFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 9
dl 0
loc 33
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 6 2
A __construct() 0 3 1
A __callStatic() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\Laminas\Messenger\Factory;
6
7
use Psr\Container\ContainerInterface;
8
use Symfony\Component\Messenger\MessageBusInterface;
9
use Symfony\Component\Messenger\RoutableMessageBus;
10
use TMV\Laminas\Messenger\Exception\InvalidArgumentException;
11
12
use function array_key_exists;
13
use function sprintf;
14
15
/**
16
 * @psalm-api
17
 */
18
final class RoutableMessageBusFactory
19 2
{
20
    /** @var string */
21 2
    private $fallbackBusName;
22 2
23
    public function __construct(string $fallbackBusName = 'messenger.bus.default')
24 2
    {
25
        $this->fallbackBusName = $fallbackBusName;
26
    }
27 2
28
    public function __invoke(ContainerInterface $container): MessageBusInterface
29 2
    {
30
        /** @var null|MessageBusInterface $fallbackBus */
31
        $fallbackBus = $container->has($this->fallbackBusName) ? $container->get($this->fallbackBusName) : null;
32
33
        return new RoutableMessageBus($container, $fallbackBus);
34
    }
35
36
    /**
37
     * @psalm-api
38 3
     *
39
     * @param array<int, mixed> $arguments
40 3
     */
41 1
    public static function __callStatic(string $name, array $arguments): MessageBusInterface
42 1
    {
43 1
        if (! array_key_exists(0, $arguments) || ! $arguments[0] instanceof ContainerInterface) {
44
            throw new InvalidArgumentException(sprintf(
45
                'The first argument must be of type %s',
46
                ContainerInterface::class
47 2
            ));
48
        }
49
50
        return (new static($name))($arguments[0]);
51
    }
52
}
53