|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Notifier\Bridge\Zendesk; |
| 13 | + |
| 14 | +use Symfony\Component\Notifier\Exception\LogicException; |
| 15 | +use Symfony\Component\Notifier\Exception\TransportException; |
| 16 | +use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; |
| 17 | +use Symfony\Component\Notifier\Message\ChatMessage; |
| 18 | +use Symfony\Component\Notifier\Message\MessageInterface; |
| 19 | +use Symfony\Component\Notifier\Message\SentMessage; |
| 20 | +use Symfony\Component\Notifier\Transport\AbstractTransport; |
| 21 | +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
| 22 | +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 23 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Joseph Bielawski <stloyd@gmail.com> |
| 27 | + */ |
| 28 | +final class ZendeskTransport extends AbstractTransport |
| 29 | +{ |
| 30 | + private string $email; |
| 31 | + private string $token; |
| 32 | + |
| 33 | + public function __construct(string $email, string $token, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) |
| 34 | + { |
| 35 | + parent::__construct($client, $dispatcher); |
| 36 | + |
| 37 | + $this->email = $email; |
| 38 | + $this->token = $token; |
| 39 | + } |
| 40 | + |
| 41 | + public function __toString(): string |
| 42 | + { |
| 43 | + return sprintf('zendesk://%s', $this->getEndpoint()); |
| 44 | + } |
| 45 | + |
| 46 | + public function supports(MessageInterface $message): bool |
| 47 | + { |
| 48 | + return $message instanceof ChatMessage; |
| 49 | + } |
| 50 | + |
| 51 | + protected function doSend(MessageInterface $message = null): SentMessage |
| 52 | + { |
| 53 | + if (!$message instanceof ChatMessage) { |
| 54 | + throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); |
| 55 | + } |
| 56 | + |
| 57 | + if (null !== $message->getOptions() && !($message->getOptions() instanceof ZendeskOptions)) { |
| 58 | + throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, ZendeskOptions::class)); |
| 59 | + } |
| 60 | + |
| 61 | + $endpoint = sprintf('https://%s/api/v2/tickets.json', $this->getEndpoint()); |
| 62 | + |
| 63 | + $body = [ |
| 64 | + 'ticket' => [ |
| 65 | + 'subject' => $message->getSubject(), |
| 66 | + 'comment' => [ |
| 67 | + 'body' => $message->getNotification() ? $message->getNotification()->getContent() : '', |
| 68 | + ], |
| 69 | + ], |
| 70 | + ]; |
| 71 | + |
| 72 | + $options = ($opts = $message->getOptions()) ? $opts->toArray() : []; |
| 73 | + if ($options['priority'] ?? null) { |
| 74 | + $body['ticket']['priority'] = $options['priority']; |
| 75 | + } |
| 76 | + |
| 77 | + $response = $this->client->request('POST', $endpoint, [ |
| 78 | + 'auth_basic' => [$this->email.'/token', $this->token], |
| 79 | + 'json' => $body, |
| 80 | + ]); |
| 81 | + |
| 82 | + try { |
| 83 | + $statusCode = $response->getStatusCode(); |
| 84 | + } catch (TransportExceptionInterface $e) { |
| 85 | + throw new TransportException('Could not reach the remote Zendesk server.', $response, 0, $e); |
| 86 | + } |
| 87 | + |
| 88 | + if (201 !== $statusCode) { |
| 89 | + $result = $response->toArray(false); |
| 90 | + |
| 91 | + $errorMessage = $result['error']; |
| 92 | + if (\is_array($errorMessage)) { |
| 93 | + $errorMessage = implode(' | ', array_values($errorMessage)); |
| 94 | + } |
| 95 | + |
| 96 | + throw new TransportException(sprintf('Unable to post the Zendesk message: "%s".', $errorMessage), $response); |
| 97 | + } |
| 98 | + |
| 99 | + return new SentMessage($message, (string) $this); |
| 100 | + } |
| 101 | +} |
0 commit comments