class SendDiscordNotification {
private SerializerInterface $serializer;
private HttpClientInterface $httpClient;
private string $webhookUrl;
public function __construct(SerializerInterface $serializer, HttpClientInterface $httpClient, string $webhookUrl) {
$this->serializer = $serializer;
$this->httpClient = $httpClient;
$this->webhookUrl = $webhookUrl;
}
public function __invoke(DiscordNotification $notification) {
$data = $this->serializer->serialize($notification, 'json');
$this->httpClient->request('POST', $this->webhookUrl, [
'body' => $data,
]);
}
}
class DiscordNotificationParser {
private SerializerInterface $serializer;
public function __construct(SerializerInterface $serializer) {
$this->serializer = $serializer;
}
public function parse(string $data): DiscordNotification {
return $this->serializer->deserialize($data, DiscordNotification::class, 'json');
}
}
use HexiumAgency\SymfonySerializerForLaravel\Facades\Serializer;
class DiscordNotificationParser {
public function parse(string $data): DiscordNotification {
return Serializer::deserialize($data, DiscordNotification::class, 'json');
}
}