RoutableMessageBusFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

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