SesSdkTransportFactory::getSupportedSchemes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the badams/symfony-amazon-sdk-mailer package.
5
 *
6
 * (c) Byron Adams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Badams\AmazonMailerSdk;
13
14
use Aws\Credentials\CredentialProvider;
15
use Aws\Credentials\Credentials;
16
use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
17
use Symfony\Component\Mailer\Transport\AbstractTransportFactory;
18
use Symfony\Component\Mailer\Transport\Dsn;
19
use Symfony\Component\Mailer\Transport\TransportInterface;
20
21
final class SesSdkTransportFactory extends AbstractTransportFactory
22
{
23
    public function create(Dsn $dsn): TransportInterface
24
    {
25
        if (!$this->supports($dsn)) {
26
            throw new UnsupportedSchemeException($dsn);
27
        }
28
29
        $config = new SesSdkTransportConfig($this->getCredentials($dsn), $dsn->getHost(), [
30
            'ConfigurationSetName' => $dsn->getOption('ConfigurationSetName'),
31
        ]);
32
33
        return new SesSdkTransport($config, $this->dispatcher, $this->logger);
34
    }
35
36
    protected function getCredentials(Dsn $dsn): callable
37
    {
38
        $user = $dsn->getUser();
39
        $password = $dsn->getPassword();
40
        $credentialType = $dsn->getOption('credentials');
41
42
        if (null !== $user || null !== $password) {
43
            return CredentialProvider::fromCredentials(new Credentials($user, $password));
44
        }
45
46
        switch ($credentialType) {
47
            case 'env':
48
                return CredentialProvider::env();
49
            case 'instance':
50
                return CredentialProvider::instanceProfile();
51
            case 'ecs':
52
                return CredentialProvider::ecsCredentials();
53
        }
54
55
        return CredentialProvider::defaultProvider();
56
    }
57
58
    protected function getSupportedSchemes(): array
59
    {
60
        return ['ses+sdk'];
61
    }
62
}
63