|
| 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\Mattermost; |
| 13 | + |
| 14 | +use Symfony\Component\Notifier\Exception\LogicException; |
| 15 | +use Symfony\Component\Notifier\Exception\TransportException; |
| 16 | +use Symfony\Component\Notifier\Message\ChatMessage; |
| 17 | +use Symfony\Component\Notifier\Message\MessageInterface; |
| 18 | +use Symfony\Component\Notifier\Transport\AbstractTransport; |
| 19 | +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
| 20 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Emanuele Panzeri <thepanz@gmail.com> |
| 24 | + * |
| 25 | + * @experimental in 5.1 |
| 26 | + */ |
| 27 | +final class MattermostTransport extends AbstractTransport |
| 28 | +{ |
| 29 | + private $token; |
| 30 | + private $channel; |
| 31 | + |
| 32 | + public function __construct(string $token, string $channel, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) |
| 33 | + { |
| 34 | + $this->token = $token; |
| 35 | + $this->channel = $channel; |
| 36 | + |
| 37 | + parent::__construct($client, $dispatcher); |
| 38 | + } |
| 39 | + |
| 40 | + public function __toString(): string |
| 41 | + { |
| 42 | + return sprintf('mattermost://%s?channel=%s', $this->getEndpoint(), $this->channel); |
| 43 | + } |
| 44 | + |
| 45 | + public function supports(MessageInterface $message): bool |
| 46 | + { |
| 47 | + return $message instanceof ChatMessage; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @see https://api.mattermost.com |
| 52 | + */ |
| 53 | + protected function doSend(MessageInterface $message): void |
| 54 | + { |
| 55 | + if (!$message instanceof ChatMessage) { |
| 56 | + throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, \get_class($message))); |
| 57 | + } |
| 58 | + |
| 59 | + $endpoint = sprintf('https://%s/api/v4/post', $this->getEndpoint()); |
| 60 | + |
| 61 | + $options = ($opts = $message->getOptions()) ? $opts->toArray() : []; |
| 62 | + $options['message'] = $message->getSubject(); |
| 63 | + |
| 64 | + if (!isset($options['channel_id'])) { |
| 65 | + $options['channel_id'] = $message->getRecipientId() ?: $this->channel; |
| 66 | + } |
| 67 | + $response = $this->client->request('POST', $endpoint, [ |
| 68 | + 'bearer' => $this->token, |
| 69 | + 'json' => array_filter($options), |
| 70 | + ]); |
| 71 | + |
| 72 | + if (200 !== $response->getStatusCode()) { |
| 73 | + $result = $response->toArray(false); |
| 74 | + |
| 75 | + throw new TransportException(sprintf('Unable to post the Mattermost message: %s (%s).', $result['message'], $result['id']), $response); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments