Passed
Push — master ( e1ded7...51c993 )
by Gabriel
03:12 queued 11s
created

TransportManager::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Nip\Mail;
4
5
use InvalidArgumentException;
6
use Nip\Config\Utils\PackageHasConfigTrait;
7
use Nip\Mail\Transport\AbstractTransport;
8
use Nip\Mail\Transport\SendgridTransport;
9
use Swift_SmtpTransport as SmtpTransport;
10
11
/**
12
 * Class TransportManager
13
 * @package Nip\Mail
14
 */
15
class TransportManager
16
{
17
    use PackageHasConfigTrait;
18
19
    /**
20
     * The array of resolved mailers.
21
     *
22
     * @var array
23
     */
24
    protected $transports = [];
25
26
    /**
27
     * The registered custom driver creators.
28
     *
29
     * @var array
30
     */
31
    protected $customCreators = [];
32
33
    /**
34
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
35
     * @return AbstractTransport
36
     */
37 3
    public function transport($name = null)
38
    {
39 3
        $name = $name ?: $this->getDefaultDriver();
0 ignored issues
show
introduced by
$name is of type null, thus it always evaluated to false.
Loading history...
40
41 3
        return $this->transports[$name] = $this->get($name);
42
    }
43
44
    /**
45
     * Attempt to get the transport from the local cache.
46
     *
47
     * @param  string  $name
48
     * @return AbstractTransport
49
     */
50 3
    protected function get($name)
51
    {
52 3
        return $this->transports[$name] ?? $this->resolve($name);
53
    }
54
55
    /**
56
     * @param string $name
57
     * @return AbstractTransport
58
     */
59 2
    protected function resolve($name)
60
    {
61 2
        $config = static::getPackageConfig('mailers.' . $name);
62
63 2
        if (is_null($config)) {
64
            throw new InvalidArgumentException("Mailer [{$name}] is not defined.");
65
        }
66 2
        $config = $config->toArray();
67
68 2
        if (isset($this->customCreators[$name])) {
69
            return call_user_func($this->customCreators[$name], $config);
70
        }
71
72 2
        if (trim($name) === '' || !method_exists($this, $method = 'create' . ucfirst($name) . 'Transport')) {
73
            throw new InvalidArgumentException("Unsupported mail transport [{$name}].");
74
        }
75
76 2
        return $this->{$method}($config);
77
    }
78
79
    /**
80
     * Create an instance of the Mailgun Swift Transport driver.
81
     *
82
     * @param array $config
83
     * @return SendgridTransport
84
     */
85 1
    protected function createSendgridTransport(array $config)
86
    {
87 1
        $transport = new SendgridTransport();
88 1
        $transport->setApiKey($config['api_key']);
89
90 1
        return $transport;
91
    }
92
93
    /**
94
     * Create an instance of the SMTP Swift Transport driver.
95
     *
96
     * @param array $config
97
     * @return SmtpTransport
98
     */
99 1
    protected function createSmtpTransport(array $config)
100
    {
101
        // The Swift SMTP transport instance will allow us to use any SMTP backend
102
        // for delivering mail such as Sendgrid, Amazon SES, or a custom server
103
        // a developer has available. We will just pass this configured host.
104 1
        $transport = SmtpTransport::newInstance($config['host'], $config['port']);
105
106 1
        if (isset($config['encryption'])) {
107 1
            $transport->setEncryption($config['encryption']);
108
        }
109
        // Once we have the transport we will check for the presence of a username
110
        // and password. If we have it we will set the credentials on the Swift
111
        // transporter instance so that we'll properly authenticate delivery.
112 1
        if (isset($config['username'])) {
113
            $transport->setUsername($config['username']);
114
            $transport->setPassword($config['password']);
115
        }
116 1
        if (isset($config['stream'])) {
117
            $transport->setStreamOptions($config['stream']);
118
        }
119 1
        return $transport;
120
    }
121
122
    /**
123
     * Get the default mail driver name.
124
     *
125
     * @return string
126
     */
127 2
    public function getDefaultDriver()
128
    {
129
        // Here we will check if the "driver" key exists and if it does we will use
130
        // that as the default driver in order to provide support for old styles
131
        // of the Laravel mail configuration file for backwards compatibility.
132 2
        return static::getPackageConfig('default', 'smtp');
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::getPackag...nfig('default', 'smtp') also could return the type Nip\Config\Config which is incompatible with the documented return type string.
Loading history...
133
    }
134
135
    /**
136
     * @inheritDoc
137
     */
138 3
    protected static function getPackageConfigName()
139
    {
140 3
        return 'mail';
141
    }
142
}
143