-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] add support for gatewayapi-notifier #38685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
53 commits
Select commit
Hold shift + click to select a range
962b19b
[Notifier] add support for gatewayapi-notifier
PGLongo 8de3bdd
Make GatewayAPITransportFactory final
PGLongo 6b9eca0
Fix typo in gatewaypi
PGLongo 86018e7
Fix typo
PGLongo 2a4c6a8
Update README.md
PGLongo da125b5
Rename GatewayAPI -> GatewayApi
PGLongo 8231389
Add GatewayApiTransportFactory tests
PGLongo f5a5f3b
Add GatewayApiTransportTest testSend
PGLongo c5dd5d8
Add GatewayApiTransportTest testSupportsSmsMessage
PGLongo 3ec6c59
Add GatewayApiTransportTest testNotSupportsChatMessage
PGLongo c10d7d6
Lint code
PGLongo 56aefa1
Fix name and email in composer.json
PGLongo 2575ca2
[Notifier] add support for gatewayapi-notifier
PGLongo ef3e509
Make GatewayAPITransportFactory final
PGLongo 10c8269
Fix typo in gatewaypi
PGLongo 633941b
Fix typo
PGLongo 9ce1bab
Update README.md
PGLongo c7f7d5f
Rename GatewayAPI -> GatewayApi
PGLongo 622a8bf
Add GatewayApiTransportFactory tests
PGLongo 4d9ef5c
Add GatewayApiTransportTest testSend
PGLongo 2faa1de
Add GatewayApiTransportTest testSupportsSmsMessage
PGLongo fc8fba8
Add GatewayApiTransportTest testNotSupportsChatMessage
PGLongo 0c03fa2
Lint code
PGLongo 88d533a
Fix name and email in composer.json
PGLongo 1010e7c
Remove extra from composer.json
PGLongo e2fe4fb
Update version tag in GatewayApiTransport
PGLongo d0e94db
Add /.gitattributes export-ignore in .gitattributes
PGLongo 9c482dc
Update UnsupportedSchemeException
PGLongo 716c3b6
Add required space in phpdoc
PGLongo 628fb46
Update GatewayApiTransportFactory.php
PGLongo 208899c
Merge branch '5.x' of https://github.com/PGLongo/symfony into 5.x
PGLongo 48002b5
Update src/Symfony/Component/Notifier/Bridge/GatewayApi/README.md
PGLongo f6e7cbe
Update src/Symfony/Component/Notifier/Bridge/GatewayApi/README.md
PGLongo 8c7e6d1
Update authors in composer.json
PGLongo 3370b92
Remove :void in GatewayApiTransportTest
PGLongo 9c084ff
Remove :void in GatewayApiTransportFactoryTest
PGLongo afff051
Rename initFactory to createFactory
PGLongo fd7144b
Remove not used createMock from getTransport
PGLongo 0d2b491
Update .gitattributes
PGLongo f9bbdb7
Update src/Symfony/Component/Notifier/Bridge/GatewayApi/composer.json
PGLongo 9506724
Update src/Symfony/Component/Notifier/Bridge/GatewayApi/composer.json
PGLongo 8ae74ff
Update src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/Gateway…
PGLongo 0e069b2
Update src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/Gateway…
PGLongo 23fc6ef
Update src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/Gateway…
PGLongo d41d30e
Update src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/Gateway…
PGLongo 09613da
Update src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/Gateway…
PGLongo b8412ee
Merge remote-tracking branch 'upstream/5.x' into 5.x
PGLongo fe9473d
Use UnsupportedMessageTypeException instead of LogicException in Gate…
PGLongo 139728f
Add first check if schema is supported in GatewayApiTransportFactory
PGLongo 0a848b0
Update GatewayApiTransportFactory.php
PGLongo 24fac37
Update src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/Gateway…
PGLongo 7bb93b8
Update composer.json
PGLongo ffbd0de
Fix code style issue in GatewayApiTransport
PGLongo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/Symfony/Component/Notifier/Bridge/GatewayApi/.gitattributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
PGLongo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/.gitignore export-ignore | ||
PGLongo marked this conversation as resolved.
Show resolved
Hide resolved
|
77 changes: 77 additions & 0 deletions
77
src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiTransport.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\GatewayApi; | ||
|
||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SentMessage; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Transport\AbstractTransport; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author Piergiuseppe Longo <piergiuseppe.longo@gmail.com> | ||
* | ||
* @experimental in 5.3 | ||
*/ | ||
final class GatewayApiTransport extends AbstractTransport | ||
{ | ||
protected const HOST = 'gatewayapi.com'; | ||
|
||
private $authToken; | ||
private $from; | ||
|
||
public function __construct(string $authToken, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->authToken = $authToken; | ||
$this->from = $from; | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('gatewayapi://%s?from=%s', $this->getEndpoint(), $this->from); | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return $message instanceof SmsMessage; | ||
} | ||
|
||
protected function doSend(MessageInterface $message): SentMessage | ||
{ | ||
if (!$message instanceof SmsMessage) { | ||
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); | ||
} | ||
|
||
$endpoint = sprintf('https://%s/rest/mtsms', $this->getEndpoint()); | ||
|
||
$response = $this->client->request('POST', $endpoint, [ | ||
'auth_basic' => [$this->authToken, ''], | ||
'json' => [ | ||
'sender' => $this->from, | ||
'recipients' => [['msisdn' => $message->getPhone()]], | ||
'message' => $message->getSubject(), | ||
], | ||
]); | ||
|
||
$statusCode = $response->getStatusCode(); | ||
if (200 !== $statusCode) { | ||
throw new TransportException(sprintf('Unable to send the SMS: error %d.', $statusCode), $response); | ||
} | ||
|
||
return new SentMessage($message, (string) $this); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/Symfony/Component/Notifier/Bridge/GatewayApi/GatewayApiTransportFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\GatewayApi; | ||
|
||
use Symfony\Component\Notifier\Exception\IncompleteDsnException; | ||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
use Symfony\Component\Notifier\Transport\TransportInterface; | ||
|
||
/** | ||
* @author Piergiuseppe Longo <piergiuseppe.longo@gmail.com> | ||
* | ||
* @experimental in 5.3 | ||
*/ | ||
final class GatewayApiTransportFactory extends AbstractTransportFactory | ||
{ | ||
/** | ||
* @return GatewayApiTransport | ||
*/ | ||
public function create(Dsn $dsn): TransportInterface | ||
{ | ||
$scheme = $dsn->getScheme(); | ||
|
||
if ('gatewayapi' !== $scheme) { | ||
throw new UnsupportedSchemeException($dsn, 'gatewayapi', $this->getSupportedSchemes()); | ||
} | ||
|
||
$authToken = $this->getUser($dsn); | ||
$from = $dsn->getOption('from'); | ||
|
||
if (!$from) { | ||
throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn()); | ||
} | ||
|
||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
$port = $dsn->getPort(); | ||
|
||
return (new GatewayApiTransport($authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['gatewayapi']; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2020 Fabien Potencier | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/Notifier/Bridge/GatewayApi/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
GatewayAPI Notifier | ||
=================== | ||
|
||
Provides [GatewayAPI](https://gatewayapi.com) integration for Symfony Notifier. | ||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
GATEWAYAPI_DSN=gatewayapi://TOKEN@default?from=FROM | ||
``` | ||
|
||
where: | ||
- `TOKEN` is API Token (OAuth) | ||
- `FROM` is sender name | ||
|
||
See your account info at https://gatewayapi.com | ||
|
||
Resources | ||
--------- | ||
|
||
* [Contributing](https://symfony.com/doc/current/contributing/index.html) | ||
* [Report issues](https://github.com/symfony/symfony/issues) and | ||
[send Pull Requests](https://github.com/symfony/symfony/pulls) | ||
in the [main Symfony repository](https://github.com/symfony/symfony) |
51 changes: 51 additions & 0 deletions
51
src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiTransportFactoryTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Notifier\Bridge\GatewayApi\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory; | ||
use Symfony\Component\Notifier\Exception\IncompleteDsnException; | ||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
|
||
final class GatewayApiTransportFactoryTest extends TestCase | ||
{ | ||
public function testSupportsGatewayApiScheme() | ||
{ | ||
$factory = $this->createFactory(); | ||
|
||
$this->assertTrue($factory->supports(Dsn::fromString('gatewayapi://token@host.test?from=Symfony'))); | ||
} | ||
|
||
public function testUnSupportedGatewayShouldThrowsUnsupportedSchemeException() | ||
{ | ||
$factory = $this->createFactory(); | ||
|
||
$this->expectException(UnsupportedSchemeException::class); | ||
|
||
$factory->create(Dsn::fromString('somethingElse://token@default?from=Symfony')); | ||
} | ||
|
||
public function testCreateWithNoTokenThrowsIncompleteDsnException() | ||
{ | ||
$factory = $this->createFactory(); | ||
|
||
$this->expectException(IncompleteDsnException::class); | ||
|
||
$factory->create(Dsn::fromString('gatewayapi://host.test?from=Symfony')); | ||
} | ||
|
||
public function testCreateWithNoFromShouldThrowsIncompleteDsnException() | ||
{ | ||
$factory = $this->createFactory(); | ||
|
||
$this->expectException(IncompleteDsnException::class); | ||
|
||
$factory->create(Dsn::fromString('gatewayapi://token@host.test')); | ||
} | ||
|
||
private function createFactory(): GatewayApiTransportFactory | ||
{ | ||
return new GatewayApiTransportFactory(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Symfony/Component/Notifier/Bridge/GatewayApi/Tests/GatewayApiTransportTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Notifier\Bridge\GatewayApi\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransport; | ||
use Symfony\Component\Notifier\Message\ChatMessage; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
final class GatewayApiTransportTest extends TestCase | ||
{ | ||
public function testSend() | ||
{ | ||
$response = $this->createMock(ResponseInterface::class); | ||
$response->expects($this->exactly(2)) | ||
->method('getStatusCode') | ||
->willReturn(200); | ||
|
||
$client = new MockHttpClient(static function () use ($response): ResponseInterface { | ||
return $response; | ||
}); | ||
|
||
$transport = $this->getTransport($client); | ||
$message = new SmsMessage('3333333333', 'Test Messgage'); | ||
$sentMessage = $transport->send($message); | ||
$this->assertNotNull($sentMessage); | ||
} | ||
|
||
public function testSupportsSmsMessage() | ||
{ | ||
$transport = $this->getTransport(); | ||
$message = new SmsMessage('3333333333', 'Test Messgage'); | ||
$this->assertTrue($transport->supports($message)); | ||
} | ||
|
||
public function testNotSupportsChatMessage() | ||
{ | ||
$transport = $this->getTransport(); | ||
$message = new ChatMessage('3333333333'); | ||
$this->assertFalse($transport->supports($message)); | ||
} | ||
|
||
private function getTransport(MockHttpClient $client = null): GatewayApiTransport | ||
{ | ||
return (new GatewayApiTransport('authtoken', 'Symfony', $client))->setHost('host.test'); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Symfony/Component/Notifier/Bridge/GatewayApi/composer.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "symfony/gatewayapi-notifier", | ||
"type": "symfony-bridge", | ||
"description": "Symfony GatewayAPI Notifier Bridge", | ||
"keywords": ["sms", "gatewayapi", "notifier"], | ||
"homepage": "https://gatewayapi.com", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Fabien Potencier", | ||
"email": "fabien@symfony.com" | ||
}, | ||
{ | ||
"name": "Piergiuseppe Longo", | ||
"email": "piergiuseppe.longo@gmail.com" | ||
PGLongo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
{ | ||
"name": "Symfony Community", | ||
"homepage": "https://symfony.com/contributors" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.2.5", | ||
"symfony/http-client": "^4.3|^5.0", | ||
"symfony/notifier": "~5.3.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\GatewayApi\\": "" }, | ||
"exclude-from-classmap": [ | ||
"/Tests/" | ||
] | ||
}, | ||
"minimum-stability": "dev" | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/Notifier/Bridge/GatewayApi/phpunit.xml.dist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd" | ||
backupGlobals="false" | ||
colors="true" | ||
bootstrap="vendor/autoload.php" | ||
failOnRisky="true" | ||
failOnWarning="true" | ||
> | ||
<php> | ||
<ini name="error_reporting" value="-1" /> | ||
</php> | ||
|
||
<testsuites> | ||
<testsuite name="Symfony GatewayAPI Notifier Bridge Test Suite"> | ||
<directory>./Tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./</directory> | ||
<exclude> | ||
<directory>./Resources</directory> | ||
<directory>./Tests</directory> | ||
<directory>./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.