TestTransportFactory::createTransport()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Zenstruck\Messenger\Test\Transport;
4
5
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
6
use Symfony\Component\Messenger\MessageBusInterface;
7
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
8
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
9
use Symfony\Component\Messenger\Transport\TransportInterface;
10
11
/**
12
 * @author Kevin Bond <[email protected]>
13
 *
14
 * @internal
15
 */
16
final class TestTransportFactory implements TransportFactoryInterface
17
{
18
    private MessageBusInterface $bus;
19
    private EventDispatcherInterface $dispatcher;
20
21
    public function __construct(MessageBusInterface $bus, EventDispatcherInterface $dispatcher)
22
    {
23
        $this->bus = $bus;
24
        $this->dispatcher = $dispatcher;
25
    }
26
27
    public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface // @phpstan-ignore-line
28
    {
29
        return new TestTransport($options['transport_name'], $this->bus, $this->dispatcher, $serializer, $this->parseDsn($dsn));
30
    }
31
32
    public function supports(string $dsn, array $options): bool // @phpstan-ignore-line
33
    {
34
        return 0 === \mb_strpos($dsn, 'test://');
35
    }
36
37
    /**
38
     * @return array<string,bool>
39
     */
40
    private function parseDsn(string $dsn): array
41
    {
42
        $query = [];
43
44
        if ($queryAsString = \mb_strstr($dsn, '?')) {
45
            \parse_str(\ltrim($queryAsString, '?'), $query);
46
        }
47
48
        return [
49
            'intercept' => \filter_var($query['intercept'] ?? true, \FILTER_VALIDATE_BOOLEAN),
50
            'catch_exceptions' => \filter_var($query['catch_exceptions'] ?? true, \FILTER_VALIDATE_BOOLEAN),
51
            'test_serialization' => \filter_var($query['test_serialization'] ?? true, \FILTER_VALIDATE_BOOLEAN),
52
            'disable_retries' => \filter_var($query['disable_retries'] ?? true, \FILTER_VALIDATE_BOOLEAN),
53
        ];
54
    }
55
}
56