SendersLocatorFactory::__invoke()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 6
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 6
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\Messenger\Factory\Transport\Sender;
6
7
use function class_exists;
8
use function interface_exists;
9
use Psr\Container\ContainerInterface;
10
use function sprintf;
11
use Symfony\Component\Messenger\Transport\Sender\SendersLocator;
12
use Symfony\Component\Messenger\Transport\Sender\SendersLocatorInterface;
13
use TMV\Messenger\Exception\LogicException;
14
15
final class SendersLocatorFactory
16
{
17
    /** @var string */
18
    private $busName;
19
20 5
    public function __construct(string $busName = 'messenger.bus.default')
21
    {
22 5
        $this->busName = $busName;
23 5
    }
24
25 5
    public function __invoke(ContainerInterface $container): SendersLocatorInterface
26
    {
27
        /** @var array|array<string, mixed> $config */
28 5
        $config = $container->has('config') ? $container->get('config') : [];
29
        /** @var string[][]|array<string, string[]> $routing */
30 5
        $routing = $config['messenger']['buses'][$this->busName]['routes'] ?? [];
31
32 5
        foreach ($routing as $message => $messageConfiguration) {
33 2
            if ('*' !== $message && ! class_exists((string) $message) && ! interface_exists((string) $message, false)) {
34 1
                throw new LogicException(sprintf('Invalid Messenger routing configuration: class or interface "%s" not found.', $message));
35
            }
36
        }
37
38 4
        return new SendersLocator($routing, $container);
39
    }
40
}
41