DoctrineDBALTransportFactory::createTransport()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\Messenger\Transport\Doctrine;
6
7
use InvalidArgumentException;
8
use Psr\Container\ContainerInterface;
9
use function sprintf;
10
use function strpos;
11
use Symfony\Component\Messenger\Exception\TransportException;
12
use Symfony\Component\Messenger\Transport\Doctrine\Connection;
13
use Symfony\Component\Messenger\Transport\Doctrine\DoctrineTransport;
14
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
15
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
16
use Symfony\Component\Messenger\Transport\TransportInterface;
17
18
class DoctrineDBALTransportFactory implements TransportFactoryInterface
19
{
20
    /** @var ContainerInterface */
21
    private $container;
22
23 4
    public function __construct(ContainerInterface $registry)
24
    {
25 4
        $this->container = $registry;
26 4
    }
27
28 2
    public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface
29
    {
30 2
        $configuration = Connection::buildConfiguration($dsn, $options);
31
32
        try {
33 2
            $driverConnection = $this->container->get($configuration['connection']);
34 1
        } catch (InvalidArgumentException $e) {
35 1
            throw new TransportException(sprintf('Could not find Doctrine connection from Messenger DSN "%s".', $dsn), 0, $e);
36
        }
37
38 1
        $connection = new Connection($configuration, $driverConnection);
39
40 1
        return new DoctrineTransport($connection, $serializer);
41
    }
42
43 1
    public function supports(string $dsn, array $options): bool
44
    {
45 1
        return 0 === strpos($dsn, 'doctrinedbal://');
46
    }
47
}
48