Test Failed
Pull Request — master (#873)
by butschster
08:25
created

TransportResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 9
c 1
b 0
f 0
dl 0
loc 30
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 10 3
A __construct() 0 3 1
A registerTransport() 0 3 1
A getTransports() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\SendIt;
6
7
use Symfony\Component\Mailer\Transport;
8
use Symfony\Component\Mailer\Transport\Dsn;
9
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;
10
use Symfony\Component\Mailer\Transport\TransportInterface;
11
12
final class TransportResolver implements TransportResolverInterface, TransportRegistryInterface
13
{
14
    /** @var TransportFactoryInterface[] */
15
    private array $transports = [];
16
17
    public function __construct(
18
        private readonly Transport $transport,
19
    ) {
20
    }
21
22
    public function registerTransport(TransportFactoryInterface $factory): void
23
    {
24
        $this->transports[] = $factory;
25
    }
26
27
    public function resolve(string $dsn): TransportInterface
28
    {
29
        $dsnDto = Dsn::fromString($dsn);
30
        foreach ($this->transports as $transport) {
31
            if ($transport->supports($dsnDto)) {
32
                return $transport->create($dsnDto);
33
            }
34
        }
35
36
        return $this->transport->fromString($dsn);
37
    }
38
39
    public function getTransports(): array
40
    {
41
        return $this->transports;
42
    }
43
}
44