-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwilioNotificationChannel.php
76 lines (59 loc) · 2.22 KB
/
TwilioNotificationChannel.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
<?php
declare(strict_types=1);
namespace EightLines\SyliusNotificationPlugin\NotificationChannel\Symfony;
use EightLines\SyliusNotificationPlugin\NotificationChannel\NotificationBody;
use EightLines\SyliusNotificationPlugin\NotificationChannel\NotificationChannelInterface;
use EightLines\SyliusNotificationPlugin\NotificationChannel\NotificationContext;
use EightLines\SyliusNotificationPlugin\NotificationChannel\NotificationRecipient;
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\TexterInterface;
final class TwilioNotificationChannel implements NotificationChannelInterface
{
private const TWILIO_TRANSPORT = 'twilio';
public function __construct(
private TexterInterface $texter,
) {
}
/**
* @throws TransportExceptionInterface
*/
public function send(
?NotificationRecipient $recipient,
NotificationBody $body,
NotificationContext $context,
): void {
$message = $body->getMessage();
if (null === $message) {
throw new \InvalidArgumentException('Message cannot be empty.');
}
$recipientPhoneNumber = $recipient?->getPhoneNumber();
if (null === $recipientPhoneNumber) {
throw new \InvalidArgumentException('Recipient phone number cannot be empty.');
}
$configuration = $context->getConfiguration();
$phoneNumberFrom = $configuration->getCustomValue('phone_number_from');
if (null === $phoneNumberFrom) {
throw new \InvalidArgumentException('Phone number from cannot be empty.');
}
$smsMessage = new SmsMessage(
phone: $recipientPhoneNumber,
subject: $message,
from: $phoneNumberFrom,
);
$smsMessage->transport(self::TWILIO_TRANSPORT);
$this->texter->send($smsMessage);
}
public static function getIdentifier(): string
{
return 'twilio';
}
public static function supportsUnknownRecipient(): bool
{
return false;
}
public static function getConfigurationFormType(): ?string
{
return TwilioNotificationChannelFormType::class;
}
}