Passed
Pull Request — master (#873)
by butschster
07:05
created

TransportResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 9
c 1
b 0
f 0
dl 0
loc 30
ccs 12
cts 12
cp 1
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 8
    public function __construct(
18
        private readonly Transport $transport,
19
    ) {
20 8
    }
21
22 3
    public function registerTransport(TransportFactoryInterface $factory): void
23
    {
24 3
        $this->transports[] = $factory;
25
    }
26
27 6
    public function resolve(string $dsn): TransportInterface
28
    {
29 6
        $dsnDto = Dsn::fromString($dsn);
30 6
        foreach ($this->transports as $transport) {
31 2
            if ($transport->supports($dsnDto)) {
32 1
                return $transport->create($dsnDto);
33
            }
34
        }
35
36 5
        return $this->transport->fromString($dsn);
37
    }
38
39 1
    public function getTransports(): array
40
    {
41 1
        return $this->transports;
42
    }
43
}
44