|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Mail; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Nip\Config\Utils\PackageHasConfigTrait; |
|
7
|
|
|
use Nip\Container\Utility\Container; |
|
8
|
|
|
use Nip\Mail\Transport\TransportFactory; |
|
9
|
|
|
use Symfony\Component\Mailer\Envelope; |
|
10
|
|
|
use Symfony\Component\Mailer\Mailer; |
|
11
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
|
12
|
|
|
use Symfony\Component\Mime\RawMessage; |
|
13
|
|
|
|
|
14
|
|
|
class MailerManager implements MailerInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use PackageHasConfigTrait; |
|
17
|
|
|
|
|
18
|
|
|
protected $transportManager = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The array of resolved mailers. |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $mailers = []; |
|
26
|
|
|
|
|
27
|
|
|
public function send(RawMessage $message, Envelope $envelope = null): void |
|
28
|
|
|
{ |
|
29
|
|
|
$this->mailer()->send($message, $envelope); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get a mailer instance by name. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string|null $name |
|
36
|
|
|
*/ |
|
37
|
|
|
public function mailer($name = null): Mailer |
|
38
|
|
|
{ |
|
39
|
|
|
$name = $name ?: $this->getDefaultDriver(); |
|
40
|
|
|
|
|
41
|
|
|
return $this->mailers[$name] = $this->get($name); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Attempt to get the mailer from the local cache. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $name |
|
48
|
|
|
* |
|
49
|
|
|
* @return Mailer |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function get($name) |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->mailers[$name] ?? $this->resolve($name); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Resolve the given mailer. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $name |
|
60
|
|
|
* |
|
61
|
|
|
* @return Mailer |
|
62
|
|
|
* |
|
63
|
|
|
* @throws \InvalidArgumentException |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function resolve($name) |
|
66
|
|
|
{ |
|
67
|
|
|
$config = static::getPackageConfig('mailers.'.$name); |
|
68
|
|
|
|
|
69
|
|
|
if (is_null($config)) { |
|
70
|
|
|
throw new InvalidArgumentException("Mailer [{$name}] is not defined."); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$config = $config->toArray(); |
|
74
|
|
|
// Once we have created the mailer instance we will set a container instance |
|
75
|
|
|
// on the mailer. This allows us to resolve mailer classes via containers |
|
76
|
|
|
// for maximum testability on said classes instead of passing Closures. |
|
77
|
|
|
$mailer = new Mailer( |
|
78
|
|
|
$this->transportManager()->fromConfig($config), |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
return $mailer; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the default mail driver name. |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getDefaultDriver(): ?string |
|
90
|
|
|
{ |
|
91
|
|
|
// Here we will check if the "driver" key exists and if it does we will use |
|
92
|
|
|
// that as the default driver in order to provide support for old styles |
|
93
|
|
|
// of the Laravel mail configuration file for backwards compatibility. |
|
94
|
|
|
return static::getPackageConfig('default'); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
public function transportManager() |
|
101
|
|
|
{ |
|
102
|
|
|
if (null === $this->transportManager) { |
|
103
|
|
|
$this->transportManager = Container::container()->get(TransportFactory::class); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this->transportManager; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param mixed $transportManager |
|
111
|
|
|
*/ |
|
112
|
|
|
public function setTransportManager($transportManager): void |
|
113
|
|
|
{ |
|
114
|
|
|
$this->transportManager = $transportManager; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Dynamically call the default driver instance. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $method |
|
121
|
|
|
* @param array $parameters |
|
122
|
|
|
* |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
*/ |
|
125
|
|
|
public function __call($method, $parameters) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->mailer()->$method(...$parameters); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritDoc} |
|
132
|
|
|
*/ |
|
133
|
|
|
protected static function getPackageConfigName(): string |
|
134
|
|
|
{ |
|
135
|
|
|
return MailServiceProvider::NAME; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|