-
Notifications
You must be signed in to change notification settings - Fork 0
/
FakeSmsTransport.php
79 lines (66 loc) · 2.24 KB
/
FakeSmsTransport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace YieldStudio\Notifier\FakeSms;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
/**
* @author James Hemery <james@yieldstudio.fr>
*/
final class FakeSmsTransport extends AbstractTransport
{
protected const HOST = 'email';
private string $to;
private string $from;
private ?MailerInterface $mailer;
public function __construct(
string $to,
string $from,
MailerInterface $mailer = null
) {
$this->to = $to;
$this->from = $from;
$this->mailer = $mailer;
parent::__construct();
}
public function __toString(): string
{
return sprintf('fakesms://%s?to=%s&from=%s', $this->getEndpoint(), $this->to, $this->from);
}
public function supports(MessageInterface $message): bool
{
return ($message instanceof SmsMessage) && self::HOST === $this->getEndpoint();
}
/**
* @param MessageInterface|SmsMessage $message
* @return SentMessage
* @throws TransportExceptionInterface
*/
protected function doSend(MessageInterface $message): SentMessage
{
if (!$this->supports($message)) {
throw new LogicException(sprintf(
'The "%s" transport only supports instances of "%s" ("%s" given) and the host email ("%s" given).',
__CLASS__,
SmsMessage::class,
\get_class($message),
$this->host
));
}
if (!$this->mailer) {
throw new \LogicException('Missing mailer.');
}
$email = (new Email())
->from($this->from)
->to($this->to)
->subject('New SMS on ' . $message->getPhone())
->html($message->getSubject())
->text($message->getSubject());
$this->mailer->send($email);
return new SentMessage($message, (string)$this);
}
}