QueueTransportFactory::createTransport()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Bdf\QueueMessengerBundle\Transport;
4
5
use Bdf\Dsn\Dsn;
6
use Bdf\Queue\Destination\DestinationManager;
7
use Bdf\QueueMessengerBundle\Transport\Stamp\NullStampsSerializer;
8
use Bdf\QueueMessengerBundle\Transport\Stamp\PhpStampsSerializer;
9
use Bdf\QueueMessengerBundle\Transport\Stamp\StampsSerializerInterface;
10
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
11
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
12
use Symfony\Component\Messenger\Transport\TransportInterface;
13
14
class QueueTransportFactory implements TransportFactoryInterface
15
{
16
    private $manager;
17
    private $defaultStampsSerializer;
18
19
    /**
20
     * @var array<string, callable():StampsSerializerInterface>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, callable()...mpsSerializerInterface> at position 4 could not be parsed: Expected '>' at position 4, but found 'callable'.
Loading history...
21
     */
22
    private $serializersFactories = [];
23
24
    /**
25
     * QueueTransportFactory constructor.
26
     */
27 3
    public function __construct(DestinationManager $manager, StampsSerializerInterface $stampsSerializer = null)
28
    {
29 3
        $this->manager = $manager;
30 3
        $this->defaultStampsSerializer = $stampsSerializer ?: new PhpStampsSerializer();
31 3
        $this->serializersFactories = [
32 3
            'null' => function () { return new NullStampsSerializer(); },
33 3
            'php' => function () { return new PhpStampsSerializer(); },
34 3
        ];
35
    }
36
37
    /**
38
     * @param string                               $name    The serializer name
39
     * @param callable():StampsSerializerInterface $factory
40
     */
41 1
    public function registerStampSerializer(string $name, callable $factory): void
42
    {
43 1
        $this->serializersFactories[$name] = $factory;
44
    }
45
46 2
    public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface
47
    {
48 2
        $request = Dsn::parse($dsn);
49
50 2
        if ($stampSerializer = $request->query('stamp_serializer')) {
51 1
            $stampSerializer = ($this->serializersFactories[$stampSerializer])();
52
        } else {
53 1
            $stampSerializer = $this->defaultStampsSerializer;
54
        }
55
56 2
        return new QueueTransport($this->manager->guess($request->getHost()), $stampSerializer, $request->query('consumer_timeout', 1));
57
    }
58
59 1
    public function supports(string $dsn, array $options): bool
60
    {
61 1
        return 0 === strpos($dsn, 'bdfqueue');
62
    }
63
}
64