Skip to content

Navigation Menu

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 e55762d

Browse filesBrowse files
committed
feature #39568 [Notifier] Add GatewayApi bridge (Piergiuseppe Longo)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Notifier] Add GatewayApi bridge | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Replaces #38685 | License | MIT | Doc PR | symfony/symfony-docs#14463 | Recipe PR | symfony/recipes#864 Initial PR by @PGLongo Commits ------- 6b9f721 [Notifier] Add GatewayApi bridge
2 parents c01b032 + 6b9f721 commit e55762d
Copy full SHA for e55762d

14 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\Notifier\Bridge\Esendex\EsendexTransportFactory;
106106
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
107107
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
108+
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
108109
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
109110
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
110111
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
@@ -2236,6 +2237,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
22362237
SendinblueNotifierTransportFactory::class => 'notifier.transport_factory.sendinblue',
22372238
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
22382239
LinkedInTransportFactory::class => 'notifier.transport_factory.linkedin',
2240+
GatewayApiTransportFactory::class => 'notifier.transport_factory.gatewayapi',
22392241
];
22402242

22412243
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
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
1616
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
1717
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
18+
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
1819
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
1920
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
2021
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
@@ -120,6 +121,10 @@
120121
->parent('notifier.transport_factory.abstract')
121122
->tag('chatter.transport_factory')
122123

124+
->set('notifier.transport_factory.gatewayapi', GatewayApiTransportFactory::class)
125+
->parent('notifier.transport_factory.abstract')
126+
->tag('texter.transport_factory')
127+
123128
->set('notifier.transport_factory.null', NullTransportFactory::class)
124129
->parent('notifier.transport_factory.abstract')
125130
->tag('chatter.transport_factory')
+4Lines changed: 4 additions & 0 deletions
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
+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
+80Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\GatewayApi;
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 Piergiuseppe Longo <piergiuseppe.longo@gmail.com>
25+
*/
26+
final class GatewayApiTransport extends AbstractTransport
27+
{
28+
protected const HOST = 'gatewayapi.com';
29+
30+
private $authToken;
31+
private $from;
32+
33+
public function __construct(string $authToken, string $from, 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+
return sprintf('gatewayapi://%s?from=%s', $this->getEndpoint(), $this->from);
44+
}
45+
46+
public function supports(MessageInterface $message): bool
47+
{
48+
return $message instanceof SmsMessage;
49+
}
50+
51+
protected function doSend(MessageInterface $message): SentMessage
52+
{
53+
if (!$message instanceof SmsMessage) {
54+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
55+
}
56+
57+
$endpoint = sprintf('https://%s/rest/mtsms', $this->getEndpoint());
58+
59+
$response = $this->client->request('POST', $endpoint, [
60+
'auth_basic' => [$this->authToken, ''],
61+
'json' => [
62+
'sender' => $this->from,
63+
'recipients' => [['msisdn' => $message->getPhone()]],
64+
'message' => $message->getSubject(),
65+
],
66+
]);
67+
68+
$statusCode = $response->getStatusCode();
69+
if (200 !== $statusCode) {
70+
throw new TransportException(sprintf('Unable to send the SMS: error %d.', $statusCode), $response);
71+
}
72+
73+
$content = $response->toArray(false);
74+
75+
$sentMessage = new SentMessage($message, (string) $this);
76+
$sentMessage->setMessageId((string) $content['ids'][0]);
77+
78+
return $sentMessage;
79+
}
80+
}
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\GatewayApi;
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 Piergiuseppe Longo <piergiuseppe.longo@gmail.com>
21+
*/
22+
final class GatewayApiTransportFactory extends AbstractTransportFactory
23+
{
24+
/**
25+
* @return GatewayApiTransport
26+
*/
27+
public function create(Dsn $dsn): TransportInterface
28+
{
29+
$scheme = $dsn->getScheme();
30+
31+
if ('gatewayapi' !== $scheme) {
32+
throw new UnsupportedSchemeException($dsn, 'gatewayapi', $this->getSupportedSchemes());
33+
}
34+
35+
$authToken = $this->getUser($dsn);
36+
$from = $dsn->getRequiredOption('from');
37+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
38+
$port = $dsn->getPort();
39+
40+
return (new GatewayApiTransport($authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
41+
}
42+
43+
protected function getSupportedSchemes(): array
44+
{
45+
return ['gatewayapi'];
46+
}
47+
}
+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.
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
GatewayApi Notifier
2+
===============
3+
4+
Provides GatewayApi integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
GATEWAYAPI_DSN=gatewayapi://TOKEN@default?from=FROM
11+
```
12+
13+
where:
14+
- `TOKEN` is API Token (OAuth)
15+
- `FROM` is sender name
16+
17+
See your account info at https://gatewayapi.com
18+
19+
Resources
20+
---------
21+
22+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
23+
* [Report issues](https://github.com/symfony/symfony/issues) and
24+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
25+
in the [main Symfony repository](https://github.com/symfony/symfony)
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Symfony\Component\Notifier\Bridge\GatewayApi\Tests;
4+
5+
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
6+
use Symfony\Component\Notifier\Tests\TransportFactoryTestCase;
7+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
8+
9+
/**
10+
* @author Piergiuseppe Longo <piergiuseppe.longo@gmail.com>
11+
* @author Oskar Stark <oskarstark@googlemail.com>
12+
*/
13+
final class GatewayApiTransportFactoryTest extends TransportFactoryTestCase
14+
{
15+
/**
16+
* @return GatewayApiTransportFactory
17+
*/
18+
public function createFactory(): TransportFactoryInterface
19+
{
20+
return new GatewayApiTransportFactory();
21+
}
22+
23+
public function createProvider(): iterable
24+
{
25+
yield [
26+
'gatewayapi://gatewayapi.com?from=Symfony',
27+
'gatewayapi://token@default?from=Symfony',
28+
];
29+
}
30+
31+
public function supportsProvider(): iterable
32+
{
33+
yield [true, 'gatewayapi://token@host.test?from=Symfony'];
34+
yield [false, 'somethingElse://token@default?from=Symfony'];
35+
}
36+
37+
public function incompleteDsnProvider(): iterable
38+
{
39+
yield 'missing token' => ['gatewayapi://host.test?from=Symfony'];
40+
}
41+
42+
public function missingRequiredOptionProvider(): iterable
43+
{
44+
yield 'missing option: from' => ['gatewayapi://token@host.test'];
45+
}
46+
}
+68Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Symfony\Component\Notifier\Bridge\GatewayApi\Tests;
4+
5+
use Symfony\Component\HttpClient\MockHttpClient;
6+
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransport;
7+
use Symfony\Component\Notifier\Message\ChatMessage;
8+
use Symfony\Component\Notifier\Message\MessageInterface;
9+
use Symfony\Component\Notifier\Message\SentMessage;
10+
use Symfony\Component\Notifier\Message\SmsMessage;
11+
use Symfony\Component\Notifier\Tests\TransportTestCase;
12+
use Symfony\Component\Notifier\Transport\TransportInterface;
13+
use Symfony\Contracts\HttpClient\HttpClientInterface;
14+
use Symfony\Contracts\HttpClient\ResponseInterface;
15+
16+
/**
17+
* @author Piergiuseppe Longo <piergiuseppe.longo@gmail.com>
18+
* @author Oskar Stark <oskarstark@googlemail.com>
19+
*/
20+
final class GatewayApiTransportTest extends TransportTestCase
21+
{
22+
/**
23+
* @return GatewayApiTransport
24+
*/
25+
public function createTransport(?HttpClientInterface $client = null): TransportInterface
26+
{
27+
return new GatewayApiTransport('authtoken', 'Symfony', $client ?: $this->createMock(HttpClientInterface::class));
28+
}
29+
30+
public function toStringProvider(): iterable
31+
{
32+
yield ['gatewayapi://gatewayapi.com?from=Symfony', $this->createTransport()];
33+
}
34+
35+
public function supportedMessagesProvider(): iterable
36+
{
37+
yield [new SmsMessage('0611223344', 'Hello!')];
38+
}
39+
40+
public function unsupportedMessagesProvider(): iterable
41+
{
42+
yield [new ChatMessage('Hello!')];
43+
yield [$this->createMock(MessageInterface::class)];
44+
}
45+
46+
public function testSend()
47+
{
48+
$response = $this->createMock(ResponseInterface::class);
49+
$response->expects($this->exactly(2))
50+
->method('getStatusCode')
51+
->willReturn(200);
52+
$response->expects($this->once())
53+
->method('getContent')
54+
->willReturn(json_encode(['ids' => [42]]));
55+
56+
$client = new MockHttpClient(static function () use ($response): ResponseInterface {
57+
return $response;
58+
});
59+
60+
$message = new SmsMessage('3333333333', 'Hello!');
61+
62+
$transport = $this->createTransport($client);
63+
$sentMessage = $transport->send($message);
64+
65+
$this->assertInstanceOf(SentMessage::class, $sentMessage);
66+
$this->assertSame('42', $sentMessage->getMessageId());
67+
}
68+
}
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "symfony/gatewayapi-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony GatewayApi Notifier Bridge",
5+
"keywords": ["sms", "gatewayapi", "notifier"],
6+
"homepage": "https://gatewayapi.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Fabien Potencier",
11+
"email": "fabien@symfony.com"
12+
},
13+
{
14+
"name": "Piergiuseppe Longo",
15+
"email": "piergiuseppe.longo@gmail.com"
16+
},
17+
{
18+
"name": "Symfony Community",
19+
"homepage": "https://symfony.com/contributors"
20+
}
21+
],
22+
"require": {
23+
"php": ">=7.2.5",
24+
"symfony/http-client": "^4.3|^5.0",
25+
"symfony/notifier": "^5.3"
26+
},
27+
"autoload": {
28+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\GatewayApi\\": "" },
29+
"exclude-from-classmap": [
30+
"/Tests/"
31+
]
32+
},
33+
"minimum-stability": "dev"
34+
}

0 commit comments

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