Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c508732

Browse filesBrowse files
Kevin AuivinetOskarStark
Kevin Auivinet
authored andcommitted
[Notifier] Add notifier for Clickatell
1 parent 72a82c3 commit c508732
Copy full SHA for c508732

File tree

13 files changed

+374
-0
lines changed
Filter options

13 files changed

+374
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
use Symfony\Component\Mime\MimeTypeGuesserInterface;
106106
use Symfony\Component\Mime\MimeTypes;
107107
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory;
108+
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
108109
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
109110
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
110111
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
@@ -2249,6 +2250,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
22492250
OctopushTransportFactory::class => 'notifier.transport_factory.octopush',
22502251
MercureTransportFactory::class => 'notifier.transport_factory.mercure',
22512252
GitterTransportFactory::class => 'notifier.transport_factory.gitter',
2253+
ClickatellTransportFactory::class => 'notifier.transport_factory.clickatell',
22522254
];
22532255

22542256
foreach ($classToServices as $class => $service) {

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

1414
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory;
15+
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
1516
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
1617
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
1718
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
@@ -145,6 +146,10 @@
145146
->parent('notifier.transport_factory.abstract')
146147
->tag('chatter.transport_factory')
147148

149+
->set('notifier.transport_factory.clickatell', ClickatellTransportFactory::class)
150+
->parent('notifier.transport_factory.abstract')
151+
->tag('texter.transport_factory')
152+
148153
->set('notifier.transport_factory.null', NullTransportFactory::class)
149154
->parent('notifier.transport_factory.abstract')
150155
->tag('chatter.transport_factory')
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.3
5+
---
6+
7+
* Add the bridge
+92Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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\Clickatell;
13+
14+
use Symfony\Component\Notifier\Exception\TransportException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SentMessage;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Component\Notifier\Transport\AbstractTransport;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author Kevin Auvinet <k.auvinet@gmail.com>
25+
*/
26+
final class ClickatellTransport extends AbstractTransport
27+
{
28+
protected const HOST = 'api.clickatell.com';
29+
30+
private $authToken;
31+
private $from;
32+
33+
public function __construct(string $authToken, string $from = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
34+
{
35+
$this->authToken = $authToken;
36+
$this->from = $from;
37+
38+
parent::__construct($client, $dispatcher);
39+
}
40+
41+
public function __toString(): string
42+
{
43+
if (null === $this->from) {
44+
return sprintf('clickatell://%s', $this->getEndpoint());
45+
}
46+
47+
return sprintf('clickatell://%s?from=%s', $this->getEndpoint(), $this->from);
48+
}
49+
50+
public function supports(MessageInterface $message): bool
51+
{
52+
return $message instanceof SmsMessage;
53+
}
54+
55+
protected function doSend(MessageInterface $message): SentMessage
56+
{
57+
if (!$message instanceof SmsMessage) {
58+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
59+
}
60+
61+
$endpoint = sprintf('https://%s/rest/message', $this->getEndpoint());
62+
63+
$response = $this->client->request('POST', $endpoint, [
64+
'headers' => [
65+
'Accept' => 'application/json',
66+
'Authorization' => 'Bearer '.$this->authToken,
67+
'Content-Type' => 'application/json',
68+
'X-Version' => 1,
69+
],
70+
'json' => [
71+
'from' => $this->from ?? '',
72+
'to' => [$message->getPhone()],
73+
'text' => $message->getSubject(),
74+
],
75+
]);
76+
77+
if (202 === $response->getStatusCode()) {
78+
$result = $response->toArray();
79+
$sentMessage = new SentMessage($message, (string) $this);
80+
$sentMessage->setMessageId($result['data']['message'][0]['apiMessageId']);
81+
82+
return $sentMessage;
83+
}
84+
85+
$content = $response->toArray(false);
86+
$errorCode = $content['error']['code'] ?? '';
87+
$errorInfo = $content['error']['description'] ?? '';
88+
$errorDocumentation = $content['error']['documentation'] ?? '';
89+
90+
throw new TransportException(sprintf('Unable to send SMS with Clickatell: Error code %d with message "%s" (%s).', $errorCode, $errorInfo, $errorDocumentation), $response);
91+
}
92+
}
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Clickatell;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @author Kevin Auvinet <k.auvinet@gmail.com>
21+
*/
22+
final class ClickatellTransportFactory extends AbstractTransportFactory
23+
{
24+
public function create(Dsn $dsn): TransportInterface
25+
{
26+
$scheme = $dsn->getScheme();
27+
28+
if ('clickatell' !== $scheme) {
29+
throw new UnsupportedSchemeException($dsn, 'clickatell', $this->getSupportedSchemes());
30+
}
31+
32+
$authToken = $this->getUser($dsn);
33+
$from = $dsn->getOption('from');
34+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
35+
$port = $dsn->getPort();
36+
37+
return (new ClickatellTransport($authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
38+
}
39+
40+
protected function getSupportedSchemes(): array
41+
{
42+
return ['clickatell'];
43+
}
44+
}
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 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.
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Clickatell Notifier
2+
===================
3+
4+
Provides [Clickatell](https://www.clickatell.com) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
CLICKATELL_DSN=clickatell://ACCESS_TOKEN@default?from=FROM
11+
```
12+
13+
where:
14+
- `ACCESS_TOKEN` is your Clickatell auth access token
15+
- `FROM` is the sender
16+
17+
Resources
18+
---------
19+
20+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
21+
* [Report issues](https://github.com/symfony/symfony/issues) and
22+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
23+
in the [main Symfony repository](https://github.com/symfony/symfony)
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Symfony\Component\Notifier\Bridge\Clickatell\Tests;
4+
5+
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
6+
use Symfony\Component\Notifier\Tests\TransportFactoryTestCase;
7+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
8+
9+
class ClickatellTransportFactoryTest extends TransportFactoryTestCase
10+
{
11+
/**
12+
* @return ClickatellTransportFactory
13+
*/
14+
public function createFactory(): TransportFactoryInterface
15+
{
16+
return new ClickatellTransportFactory();
17+
}
18+
19+
public function createProvider(): iterable
20+
{
21+
yield [
22+
'clickatell://host.test?from=0611223344',
23+
'clickatell://authtoken@host.test?from=0611223344',
24+
];
25+
}
26+
27+
public function supportsProvider(): iterable
28+
{
29+
yield [true, 'clickatell://authtoken@default?from=0611223344'];
30+
yield [false, 'somethingElse://authtoken@default?from=0611223344'];
31+
}
32+
33+
public function incompleteDsnProvider(): iterable
34+
{
35+
yield 'missing auth token' => ['clickatell://host?from=FROM'];
36+
}
37+
38+
public function unsupportedSchemeProvider(): iterable
39+
{
40+
yield ['somethingElse://authtoken@default?from=FROM'];
41+
yield ['somethingElse://authtoken@default']; // missing "from" option
42+
}
43+
}
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Symfony\Component\Notifier\Bridge\Clickatell\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\HttpClient\MockHttpClient;
7+
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransport;
8+
use Symfony\Component\Notifier\Exception\LogicException;
9+
use Symfony\Component\Notifier\Exception\TransportException;
10+
use Symfony\Component\Notifier\Message\MessageInterface;
11+
use Symfony\Component\Notifier\Message\SmsMessage;
12+
use Symfony\Contracts\HttpClient\HttpClientInterface;
13+
use Symfony\Contracts\HttpClient\ResponseInterface;
14+
15+
class ClickatellTransportTest extends TestCase
16+
{
17+
public function testToString()
18+
{
19+
$transport = new ClickatellTransport('authToken', 'fromValue', $this->createMock(HttpClientInterface::class));
20+
$transport->setHost('clickatellHost');
21+
22+
$this->assertSame('clickatell://clickatellHost?from=fromValue', (string) $transport);
23+
}
24+
25+
public function testSupports()
26+
{
27+
$transport = new ClickatellTransport('authToken', 'fromValue', $this->createMock(HttpClientInterface::class));
28+
29+
$this->assertTrue($transport->supports(new SmsMessage('+33612345678', 'testSmsMessage')));
30+
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
31+
}
32+
33+
public function testExceptionIsThrownWhenNonMessageIsSend()
34+
{
35+
$transport = new ClickatellTransport('authToken', 'fromValue', $this->createMock(HttpClientInterface::class));
36+
37+
$this->expectException(LogicException::class);
38+
$transport->send($this->createMock(MessageInterface::class));
39+
}
40+
41+
public function testExceptionIsThrownWhenHttpSendFailed()
42+
{
43+
$response = $this->createMock(ResponseInterface::class);
44+
$response->expects($this->exactly(2))
45+
->method('getStatusCode')
46+
->willReturn(500);
47+
$response->expects($this->once())
48+
->method('getContent')
49+
->willReturn(json_encode([
50+
'error' => [
51+
'code' => 105,
52+
'description' => 'Invalid Account Reference EX0000000',
53+
'documentation' => 'https://documentation-page',
54+
],
55+
]));
56+
57+
$client = new MockHttpClient($response);
58+
59+
$transport = new ClickatellTransport('authToken', 'fromValue', $client);
60+
$this->expectException(TransportException::class);
61+
$this->expectExceptionMessage('Unable to send SMS with Clickatell: Error code 105 with message "Invalid Account Reference EX0000000" (https://documentation-page).');
62+
63+
$transport->send(new SmsMessage('+33612345678', 'testSmsMessage'));
64+
}
65+
}
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "symfony/clickatell-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony Clickatell Notifier Bridge",
5+
"keywords": ["sms", "clickatell", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Fabien Potencier",
11+
"email": "fabien@symfony.com"
12+
},
13+
{
14+
"name": "Symfony Community",
15+
"homepage": "https://symfony.com/contributors"
16+
},
17+
{
18+
"name": "Kevin Auvinet",
19+
"email": "k.auvinet@gmail.com"
20+
}
21+
],
22+
"require": {
23+
"php": ">=7.2.5",
24+
"symfony/http-client": "^4.3|^5.0",
25+
"symfony/notifier": "^5.3"
26+
},
27+
"require-dev": {
28+
"symfony/event-dispatcher": "^4.3|^5.0"
29+
},
30+
"autoload": {
31+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Clickatell\\": "" },
32+
"exclude-from-classmap": [
33+
"/Tests/"
34+
]
35+
},
36+
"minimum-stability": "dev"
37+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.