-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmsSender.php
61 lines (50 loc) · 1.71 KB
/
SmsSender.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
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <francois.pluchino@klipper.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\SmsSender;
use Klipper\Component\SmsSender\Exception\TransportException;
use Klipper\Component\SmsSender\Messenger\SendSmsMessage;
use Klipper\Component\SmsSender\Transport\TransportInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Mime\Message;
use Symfony\Component\Mime\RawMessage;
/**
* Sms sender.
*
* @author François Pluchino <francois.pluchino@klipper.dev>
*/
class SmsSender implements SmsSenderInterface
{
private TransportInterface $transport;
private ?MessageBusInterface $bus;
/**
* @param TransportInterface $transport The transport
* @param null|MessageBusInterface $bus The message bus
*/
public function __construct(TransportInterface $transport, ?MessageBusInterface $bus = null)
{
$this->transport = $transport;
$this->bus = $bus;
}
public function send(RawMessage $message, ?Envelope $envelope = null): void
{
if ($message instanceof Message && $this->hasRequiredFrom() && !$message->getHeaders()->has('From')) {
throw new TransportException('The transport required the "From" information');
}
if (null === $this->bus) {
$this->transport->send($message, $envelope);
return;
}
$this->bus->dispatch(new SendSmsMessage($message, $envelope));
}
public function hasRequiredFrom(): bool
{
return $this->transport->hasRequiredFrom();
}
}