Last active
August 8, 2021 00:06
-
-
Save ismail1432/068ef6121081f0671af0707229102625 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\UserlandTransport; | |
use Symfony\Component\Notifier\Message\MessageInterface; | |
use Symfony\Component\Notifier\Message\SentMessage; | |
use Symfony\Component\Notifier\Transport\AbstractTransport; | |
final class UserlandTransport extends AbstractTransport | |
{ | |
private const TRANSPORT = 'userland'; | |
/** | |
* This method will be called by Symfony to send the message, | |
* It should contains the stuff to call the API where you want publish | |
* your message. | |
*/ | |
protected function doSend(MessageInterface $message): SentMessage | |
{ | |
// Retrieve the content of the Notification, "Welcome abroad <3" in our example | |
$message->getSubject(); | |
// Your custom stuff to call the API, | |
// I assume everything is good but you should check the code status and handle errors. | |
$result = $this->client->request('POST', 'https://www.api.userland.io', [ | |
'body' => ['message' => $message->getSubject()] | |
]); | |
// Create an instance of SentMessage that should be returned to respect the contract. | |
$sentMessage = new SentMessage($message, (string) $this); | |
// Suppose the API returns the id of the transaction. | |
$sentMessage->setMessageId($result['id']); | |
// 💡 for your information this object will be passed to an event. | |
return $sentMessage; | |
} | |
public function supports(MessageInterface $message): bool | |
{ | |
return self::TRANSPORT === $message->getTransport(); | |
} | |
public function __toString(): string | |
{ | |
return self::TRANSPORT; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment