Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 18
TransportFactory
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
132
0.00% covered (danger)
0.00%
0 / 18
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 createTransport
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 13
 supports
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 4
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Messenger\Transport;
13
14use Symfony\Component\Messenger\Exception\InvalidArgumentException;
15use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
16
17/**
18 * @author Samuel Roze <samuel.roze@gmail.com>
19 */
20class TransportFactory implements TransportFactoryInterface
21{
22    private $factories;
23
24    /**
25     * @param iterable|TransportFactoryInterface[] $factories
26     */
27    public function __construct(iterable $factories)
28    {
29        $this->factories = $factories;
30    }
31
32    public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface
33    {
34        foreach ($this->factories as $factory) {
35            if ($factory->supports($dsn, $options)) {
36                return $factory->createTransport($dsn, $options, $serializer);
37            }
38        }
39
40        // Help the user to select Symfony packages based on protocol.
41        $packageSuggestion = '';
42        if (0 === strpos($dsn, 'amqp://')) {
43            $packageSuggestion = ' Run "composer require symfony/amqp-messenger" to install AMQP transport.';
44        } elseif (0 === strpos($dsn, 'doctrine://')) {
45            $packageSuggestion = ' Run "composer require symfony/doctrine-messenger" to install Doctrine transport.';
46        } elseif (0 === strpos($dsn, 'redis://')) {
47            $packageSuggestion = ' Run "composer require symfony/redis-messenger" to install Redis transport.';
48        } elseif (0 === strpos($dsn, 'sqs://')) {
49            $packageSuggestion = ' Run "composer require symfony/amazon-sqs-messenger" to install Amazon SQS transport.';
50        }
51
52        throw new InvalidArgumentException(sprintf('No transport supports the given Messenger DSN "%s".%s.', $dsn, $packageSuggestion));
53    }
54
55    public function supports(string $dsn, array $options): bool
56    {
57        foreach ($this->factories as $factory) {
58            if ($factory->supports($dsn, $options)) {
59                return true;
60            }
61        }
62
63        return false;
64    }
65}