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 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\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