RoutableMessageBusFactory::__callStatic()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 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