Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CustomSmsMessage #1

Open
wants to merge 2 commits into
base: 5.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions EsendexTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use App\Model\CustomSmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -31,13 +31,11 @@ final class EsendexTransport extends AbstractTransport

private $token;
private $accountReference;
private $from;

public function __construct(string $token, string $accountReference, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
public function __construct(string $token, string $accountReference, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
$this->token = $token;
$this->accountReference = $accountReference;
$this->from = $from;

parent::__construct($client, $dispatcher);
}
Expand All @@ -49,24 +47,21 @@ public function __toString(): string

public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage;
return $message instanceof CustomSmsMessage;
}

protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof SmsMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, get_debug_type($message)));
if (!$message instanceof CustomSmsMessage) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, CustomSmsMessage::class, get_debug_type($message)));
}

$messageData = [
'to' => $message->getPhone(),
'body' => $message->getSubject(),
'from' => $message->getFrom(),
];

if (null !== $this->from) {
$messageData['from'] = $this->from;
}

$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v1.0/messagedispatcher', [
'auth_basic' => $this->token,
'json' => [
Expand Down Expand Up @@ -96,4 +91,4 @@ protected function doSend(MessageInterface $message): SentMessage

throw new TransportException($message, $response);
}
}
}
10 changes: 2 additions & 8 deletions EsendexTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,14 @@ public function create(Dsn $dsn): TransportInterface
throw new IncompleteDsnException('Missing accountreference.', $dsn->getOriginalDsn());
}

$from = $dsn->getOption('from');

if (!$from) {
throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn());
}

$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

return (new EsendexTransport($token, $accountReference, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
return (new EsendexTransport($token, $accountReference, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

protected function getSupportedSchemes(): array
{
return ['esendex'];
}
}
}