RoutableMessageBusFactory::__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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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