TestTransportRegistry::get()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 11
nc 8
nop 1
dl 0
loc 23
rs 8.8333
c 1
b 0
f 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