TransportFactory::__invoke()   A
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 30
ccs 17
cts 17
cp 1
rs 9.0444
c 0
b 0
f 0
cc 6
nc 24
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\Laminas\Messenger\Factory\Transport;
6
7
use Psr\Container\ContainerInterface;
8
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
9
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
10
use Symfony\Component\Messenger\Transport\TransportFactory as SFTransportFactory;
11
use Symfony\Component\Messenger\Transport\TransportInterface;
12
use TMV\Laminas\Messenger\Exception\InvalidArgumentException;
13
14
use function array_key_exists;
15
use function is_array;
16
use function is_string;
17
use function sprintf;
18
19
/**
20
 * @psalm-api
21
 */
22
final class TransportFactory
23 3
{
24
    /** @var string */
25 3
    private $transportNameOrDsn;
26 3
27
    public function __construct(string $transportNameOrDsn)
28 3
    {
29
        $this->transportNameOrDsn = $transportNameOrDsn;
30
    }
31 3
32
    public function __invoke(ContainerInterface $container): TransportInterface
33 3
    {
34
        /** @var array<string, mixed> $config */
35 3
        $config = $container->has('config') ? $container->get('config') : [];
36 3
37 3
        $transportConfig = $config['messenger']['transports'][$this->transportNameOrDsn] ?? null;
38
39 3
        $dsn = null;
40 1
        $options = [];
41 1
        $serializerName = $config['messenger']['default_serializer'] ?? null;
42 1
43 2
        if (is_array($transportConfig)) {
44 1
            $dsn = $transportConfig['dsn'] ?? null;
45
            $options = $transportConfig['options'] ?? [];
46
            $serializerName = $transportConfig['serializer'] ?? $serializerName;
47 3
        } elseif (is_string($transportConfig)) {
48 1
            $dsn = $transportConfig;
49
        }
50
51 3
        if (null === $dsn) {
52
            $dsn = $this->transportNameOrDsn;
53 3
        }
54 3
55 3
        $transportFactory = $container->get(SFTransportFactory::class);
56
        /** @var SerializerInterface $serializer */
57 3
        $serializer = $serializerName
58
            ? $container->get($serializerName)
59
            : new PhpSerializer();
60
61
        return $transportFactory->createTransport($dsn, $options, $serializer);
62
    }
63
64
    /**
65
     * @psalm-api
66 4
     *
67
     * @param array<int, mixed> $arguments
68 4
     */
69 1
    public static function __callStatic(string $name, array $arguments): TransportInterface
70 1
    {
71 1
        if (! array_key_exists(0, $arguments) || ! $arguments[0] instanceof ContainerInterface) {
72
            throw new InvalidArgumentException(sprintf(
73
                'The first argument must be of type %s',
74
                ContainerInterface::class
75 3
            ));
76
        }
77
78
        return (new static($name))($arguments[0]);
79
    }
80
}
81