SendersLocatorFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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