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

TransportResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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