|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Derafu: Biblioteca PHP (Núcleo). |
|
7
|
|
|
* Copyright (C) Derafu <https://www.derafu.org> |
|
8
|
|
|
* |
|
9
|
|
|
* Este programa es software libre: usted puede redistribuirlo y/o modificarlo |
|
10
|
|
|
* bajo los términos de la Licencia Pública General Affero de GNU publicada por |
|
11
|
|
|
* la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o |
|
12
|
|
|
* (a su elección) cualquier versión posterior de la misma. |
|
13
|
|
|
* |
|
14
|
|
|
* Este programa se distribuye con la esperanza de que sea útil, pero SIN |
|
15
|
|
|
* GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD |
|
16
|
|
|
* PARA UN PROPÓSITO DETERMINADO. Consulte los detalles de la Licencia Pública |
|
17
|
|
|
* General Affero de GNU para obtener una información más detallada. |
|
18
|
|
|
* |
|
19
|
|
|
* Debería haber recibido una copia de la Licencia Pública General Affero de GNU |
|
20
|
|
|
* junto a este programa. |
|
21
|
|
|
* |
|
22
|
|
|
* En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Derafu\Lib\Core\Package\Prime\Component\Mail\Abstract; |
|
26
|
|
|
|
|
27
|
|
|
use Derafu\Lib\Core\Foundation\Abstract\AbstractStrategy; |
|
28
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Mail\Contract\PostmanInterface; |
|
29
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Mail\Contract\SenderStrategyInterface; |
|
30
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Mail\Exception\MailException; |
|
31
|
|
|
use Derafu\Lib\Core\Support\Store\Contract\DataContainerInterface; |
|
32
|
|
|
use Exception; |
|
33
|
|
|
use Symfony\Component\Mailer\Envelope as SymfonyEnvelope; |
|
34
|
|
|
use Symfony\Component\Mailer\Mailer; |
|
35
|
|
|
use Symfony\Component\Mailer\Transport; |
|
36
|
|
|
use Symfony\Component\Mime\Email as SymfonyEmail; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Estrategia base para el envío de correos electrónicos utilizando el Mailer de |
|
40
|
|
|
* Symfony. |
|
41
|
|
|
*/ |
|
42
|
|
|
abstract class AbstractMailerStrategy extends AbstractStrategy implements SenderStrategyInterface |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritDoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function send(PostmanInterface $postman): array |
|
48
|
|
|
{ |
|
49
|
|
|
$options = $this->resolveOptions($postman->getOptions()->all()); |
|
50
|
|
|
|
|
51
|
|
|
$mailer = $this->createMailer($options); |
|
52
|
|
|
|
|
53
|
|
|
foreach ($postman->getEnvelopes() as $envelope) { |
|
54
|
|
|
assert($envelope instanceof SymfonyEnvelope); |
|
55
|
|
|
foreach ($envelope->getMessages() as $message) { |
|
56
|
|
|
assert($message instanceof SymfonyEmail); |
|
57
|
|
|
try { |
|
58
|
|
|
$mailer->send($message, clone $envelope); |
|
59
|
|
|
} catch (Exception $e) { |
|
60
|
|
|
$message->error($e); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $postman->getEnvelopes(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Crea un remitente de correo. |
|
70
|
|
|
* |
|
71
|
|
|
* @param DataContainerInterface $options Opciones del remitente. |
|
72
|
|
|
* @return Mailer |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function createMailer(DataContainerInterface $options): Mailer |
|
75
|
|
|
{ |
|
76
|
|
|
$dsn = $this->resolveDsn($options); |
|
77
|
|
|
$transport = Transport::fromDsn($dsn); |
|
78
|
|
|
$mailer = new Mailer($transport); |
|
79
|
|
|
|
|
80
|
|
|
$this->resolveEndpoint($options); |
|
81
|
|
|
|
|
82
|
|
|
return $mailer; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Resuelve el DSN para el mailer. |
|
87
|
|
|
* |
|
88
|
|
|
* @param DataContainerInterface $options Configuración del mailer. |
|
89
|
|
|
* @return string DSN. |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function resolveDsn(DataContainerInterface $options): string |
|
92
|
|
|
{ |
|
93
|
|
|
$transportOptions = $options->get('transport'); |
|
94
|
|
|
|
|
95
|
|
|
if (empty($transportOptions['dsn'])) { |
|
96
|
|
|
throw new MailException('No está definido el DSN para el Mailer.'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $transportOptions['dsn']; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Resuelve el endpoint personalizado. |
|
104
|
|
|
* |
|
105
|
|
|
* @param DataContainerInterface $options Configuración del mailer. |
|
106
|
|
|
* @return string Endpoint personalizado. |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function resolveEndpoint(DataContainerInterface $options): string |
|
109
|
|
|
{ |
|
110
|
|
|
$transportOptions = $options->get('transport'); |
|
111
|
|
|
|
|
112
|
|
|
if (!empty($transportOptions['endpoint'])) { |
|
113
|
|
|
return $transportOptions['endpoint']; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$endpoint = $this->resolveDsn($options); |
|
117
|
|
|
|
|
118
|
|
|
$options->set('transport.endpoint', $endpoint); |
|
119
|
|
|
|
|
120
|
|
|
return $endpoint; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|