10000 [Notifier] Add Zendesk Notifier Bridge · symfony/symfony@da5a282 · GitHub
[go: up one dir, main page]

Skip to content

Commit da5a282

Browse files
committed
[Notifier] Add Zendesk Notifier Bridge
1 parent 1f73c49 commit da5a282

17 files changed

+434
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+2
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
169169
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransportFactory;
170170
use Symfony\Component\Notifier\Bridge\Yunpian\YunpianTransportFactory;
171+
use Symfony\Component\Notifier\Bridge\Zendesk\ZendeskTransportFactory;
171172
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
172173
use Symfony\Component\Notifier\Notifier;
173174
use Symfony\Component\Notifier\Recipient\Recipient;
@@ -2567,6 +2568,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25672568
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
25682569
VonageTransportFactory::class => 'notifier.transport_factory.vonage',
25692570
YunpianTransportFactory::class => 'notifier.transport_factory.yunpian',
2571+
ZendeskTransportFactory::class => 'notifier.transport_factory.zendesk',
25702572
ZulipTransportFactory::class => 'notifier.transport_factory.zulip',
25712573
];
25722574

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
6161
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransportFactory;
6262
use Symfony\Component\Notifier\Bridge\Yunpian\YunpianTransportFactory;
63+
use Symfony\Component\Notifier\Bridge\Zendesk\ZendeskTransportFactory;
6364
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
6465
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
6566
use Symfony\Component\Notifier\Transport\NullTransportFactory;
@@ -276,5 +277,9 @@
276277
->set('notifier.transport_factory.engagespot', EngagespotTransportFactory::class)
277278
->parent('notifier.transport_factory.abstract& A3D4 #39;)
278279
->tag('texter.transport_factory')
280+
281+
->set('notifier.transport_factory.zendesk', ZendeskTransportFactory::class)
282+
->parent('notifier.transport_factory.abstract')
283+
->tag('chatter.transport_factory')
279284
;
280285
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
6.2
5+
---
6+
7+
* Add the bridge
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2022 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Zendesk Notifier
2+
================
3+
4+
Provides [Zendesk](https://www.zendesk.com) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
ZENDESK_DSN=zendesk://EMAIL:TOKEN@SUBDOMAIN
11+
```
12+
13+
where:
14+
- `EMAIL` is your Zendesk Email Address
15+
- `TOKEN` is your Zendesk API token
16+
- `SUBDOMAIN` is your subdomain name at zendesk.com
17+
18+
Resources
19+
---------
20+
21+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
22+
* [Report issues](https://github.com/symfony/symfony/issues) and
23+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
24+
in the [main Symfony repository](https://github.com/symfony/symfony)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Zendesk\ZendeskTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
17+
final class ZendeskTransportFactoryTest extends TransportFactoryTestCase
18+
{
19+
public function createFactory(): ZendeskTransportFactory
20+
{
21+
return new ZendeskTransportFactory();
22+
}
23+
24+
public function createProvider(): iterable
25+
{
26+
yield [
27+
'zendesk://subdomain.zendesk.com',
28+
'zendesk://email:token@subdomain.zendesk.com',
29+
];
30+
}
31+
32+
public function supportsProvider(): iterable
33+
{
34+
yield [true, 'zendesk://host'];
35+
yield [false, 'somethingElse://host'];
36+
}
37+
38+
public function incompleteDsnProvider(): iterable
39+
{
40+
yield 'missing email or token' => ['zendesk://testOneOfEmailOrToken@host'];
41+
yield 'wrong host' => ['zendesk://testEmail:Token@host.com'];
42+
}
43+
44+
public function unsupportedSchemeProvider(): iterable
45+
{
46+
yield ['somethingElse://email:token@host'];
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Zendesk\ZendeskOptions;
15+
use Symfony\Component\Notifier\Bridge\Zendesk\ZendeskTransport;
16+
use Symfony\Component\Notifier\Message\ChatMessage;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Component\Notifier\Test\TransportTestCase;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
final class ZendeskTransportTest extends TransportTestCase
23+
{
24+
public function createTransport(HttpClientInterface $client = null): ZendeskTransport
25+
{
26+
return (new ZendeskTransport('testEmail', 'testToken', $client ?? $this->createMock(HttpClientInterface::class)))->setHost('test.zendesk.com');
27+
}
28+
29+
public function toStringProvider(): iterable
30+
{
31+
yield ['zendesk://test.zendesk.com', $this->createTransport()];
32+
}
33+
34+
public function supportedMessagesProvider(): iterable
35+
{
36+
yield [new ChatMessage('Hello!')];
37+
yield [new ChatMessage('Hello!', new ZendeskOptions('urgent'))];
38+
}
39+
40+
public function unsupportedMessagesProvider(): iterable
41+
{
42+
yield [new SmsMessage('0611223344', 'Hello!')];
43+
yield [$this->createMock(MessageInterface::class)];
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author Joseph Bielawski <stloyd@gmail.com>
18+
*/
19+
final class ZendeskOptions implements MessageOptionsInterface
20+
{
21+
private ?string $priority;
22+
23+
public function __construct(string $priority = null)
24+
{
25+
$this->priority = $priority;
26+
}
27+
28+
public function toArray(): array
29+
{
30+
return [
31+
'priority' => $this->priority,
32+
];
33+
}
34+
35+
public function getRecipientId(): ?string
36+
{
37+
return null;
38+
}
39+
40+
/**
41+
* @return $this
42+
*/
43+
public function priority(string $priority): static
44+
{
45+
$this->priority = $priority;
46+
47+
return $this;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)
0