TransportFactory   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 58
ccs 28
cts 28
cp 1
rs 10
wmc 10

3 Methods

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