TestTransportRegistry   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
dl 0
loc 33
rs 10
c 1
b 0
f 1
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B get() 0 23 7
A register() 0 3 1
1
<?php
2
3
namespace Zenstruck\Messenger\Test\Transport;
4
5
use Symfony\Component\Messenger\Transport\TransportInterface;
6
7
/**
8
 * @author Kevin Bond <[email protected]>
9
 *
10
 * @internal
11
 */
12
final class TestTransportRegistry
13
{
14
    /** @var TransportInterface[] */
15
    private array $transports = [];
16
17
    public function register(string $name, TransportInterface $transport): void
18
    {
19
        $this->transports[$name] = $transport;
20
    }
21
22
    public function get(?string $name = null): TestTransport
23
    {
24
        if (0 === \count($this->transports)) {
25
            throw new \LogicException('No transports registered.');
26
        }
27
28
        if (null === $name && 1 !== \count($this->transports)) {
29
            throw new \InvalidArgumentException(\sprintf('Multiple transports are registered (%s), you must specify a name.', \implode(', ', \array_keys($this->transports))));
30
        }
31
32
        if (null === $name) {
33
            $name = \array_key_first($this->transports);
34
        }
35
36
        if (!$transport = $this->transports[$name] ?? null) {
37
            throw new \InvalidArgumentException("Transport \"{$name}\" not registered.");
38
        }
39
40
        if (!$transport instanceof TestTransport) {
41
            throw new \LogicException("Transport \"{$name}\" needs to be set to \"test://\" in your test config to use this feature.");
42
        }
43
44
        return $transport;
45
    }
46
}
47