Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #44874 [Notifier] Added 46elks notifier bridge (jongotlin)
This PR was merged into the 6.1 branch. Discussion ---------- [Notifier] Added 46elks notifier bridge | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | symfony/symfony-docs#16350 [46elks](https://46elks.se) is a Swedish sms provider. Commits ------- 1594296 Added 46elks notifier bridge
- Loading branch information
Showing
17 changed files
with
402 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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/FortySixElks/.gitattributes
This file contains 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 | ||
/.gitignore export-ignore |
3 changes: 3 additions & 0 deletions
3
src/Symfony/Component/Notifier/Bridge/FortySixElks/.gitignore
This file contains 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,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
7 changes: 7 additions & 0 deletions
7
src/Symfony/Component/Notifier/Bridge/FortySixElks/CHANGELOG.md
This file contains 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,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
6.1 | ||
--- | ||
|
||
* Add the bridge |
87 changes: 87 additions & 0 deletions
87
src/Symfony/Component/Notifier/Bridge/FortySixElks/FortySixElksTransport.php
This file contains 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,87 @@ | ||
<?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\FortySixElks; | ||
|
||
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\Exception\TransportExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author Jon Gotlin <jon@jon.se> | ||
*/ | ||
final class FortySixElksTransport extends AbstractTransport | ||
{ | ||
protected const HOST = 'api.46elks.com'; | ||
|
||
private string $apiUsername; | ||
private string $apiPassword; | ||
private string $from; | ||
|
||
public function __construct(string $apiUsername, string $apiPassword, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->apiUsername = $apiUsername; | ||
$this->apiPassword = $apiPassword; | ||
$this->from = $from; | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('forty-six-elks://%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/a1/sms', self::HOST); | ||
$response = $this->client->request('POST', $endpoint, [ | ||
'body' => [ | ||
'from' => $this->from, | ||
'to' => $message->getPhone(), | ||
'message' => $message->getSubject(), | ||
], | ||
'auth_basic' => [$this->apiUsername, $this->apiPassword], | ||
]); | ||
|
||
try { | ||
$statusCode = $response->getStatusCode(); | ||
} catch (TransportExceptionInterface $e) { | ||
throw new TransportException('Could not reach the remote 46elks server.', $response, 0, $e); | ||
} | ||
|
||
if (200 !== $statusCode) { | ||
throw new TransportException('Unable to post the 46elks message: '.$response->getContent(false), $response); | ||
} | ||
|
||
$result = $response->toArray(false); | ||
|
||
$sentMessage = new SentMessage($message, (string) $this); | ||
$sentMessage->setMessageId($result['id'] ?? ''); | ||
|
||
return $sentMessage; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/Notifier/Bridge/FortySixElks/FortySixElksTransportFactory.php
This file contains 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,39 @@ | ||
<?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\FortySixElks; | ||
|
||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
|
||
/** | ||
* @author Jon Gotlin <jon@jon.se> | ||
*/ | ||
final class FortySixElksTransportFactory extends AbstractTransportFactory | ||
{ | ||
public function create(Dsn $dsn): FortySixElksTransport | ||
{ | ||
if ('forty-six-elks' !== $dsn->getScheme()) { | ||
throw new UnsupportedSchemeException($dsn, 'forty-six-elks', $this->getSupportedSchemes()); | ||
} | ||
|
||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
$from = $dsn->getRequiredOption('from'); | ||
|
||
return (new FortySixElksTransport($this->getUser($dsn), $this->getPassword($dsn), $from, $this->client, $this->dispatcher))->setHost($host)->setPort($dsn->getPort()); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['forty-six-elks']; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Symfony/Component/Notifier/Bridge/FortySixElks/LICENSE
This file contains 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) 2022 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. |
24 changes: 24 additions & 0 deletions
24
src/Symfony/Component/Notifier/Bridge/FortySixElks/README.md
This file contains 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,24 @@ | ||
46elks Notifier | ||
=============== | ||
|
||
Provides [46elks](https://46elks.se) integration for Symfony Notifier. | ||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
FORTY_SIX_ELKS_DSN=forty-six-elks://API_USERNAME:API_PASSWORD@default?from=FROM | ||
``` | ||
|
||
where: | ||
- `API_USERNAME` is your 46elks API username | ||
- `API_PASSWORD` is your 46elks API password | ||
- `FROM` is the alphanumeric originator for the message to appear to originate from | ||
|
||
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) |
11 changes: 11 additions & 0 deletions
11
src/Symfony/Component/Notifier/Bridge/FortySixElks/Tests/Fixtures/success-response.json
This file contains 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,11 @@ | ||
{ | ||
"status": "created", | ||
"direction": "outgoing", | ||
"from": "+46702222222", | ||
"created": "2021-12-30T20:35:32.429389", | ||
"parts": 1, | ||
"to": "+46701111111", | ||
"cost": 3900, | ||
"message": "Symfony test", | ||
"id": "s0231d6d7d6bc14a7e7734e466785c4ce" | ||
} |
41 changes: 41 additions & 0 deletions
41
...Symfony/Component/Notifier/Bridge/FortySixElks/Tests/FortySixElksTransportFactoryTest.php
This file contains 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,41 @@ | ||
<?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 | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\FortySixElks\Tests; | ||
|
||
use Symfony\Component\Notifier\Bridge\FortySixElks\FortySixElksTransportFactory; | ||
use Symfony\Component\Notifier\Test\TransportFactoryTestCase; | ||
|
||
class FortySixElksTransportFactoryTest extends TransportFactoryTestCase | ||
{ | ||
public function createFactory(): FortySixElksTransportFactory | ||
{ | ||
return new FortySixElksTransportFactory(); | ||
} | ||
|
||
public function createProvider(): iterable | ||
{ | ||
yield [ | ||
'forty-six-elks://host.test?from=Symfony', | ||
'forty-six-elks://api_username:api_password@host.test?from=Symfony', | ||
]; | ||
} | ||
|
||
public function supportsProvider(): iterable | ||
{ | ||
yield [true, 'forty-six-elks://api_key@default']; | ||
yield [false, 'somethingElse://api_key@default']; | ||
} | ||
|
||
public function unsupportedSchemeProvider(): iterable | ||
{ | ||
yield ['somethingElse://api_key@default']; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/Symfony/Component/Notifier/Bridge/FortySixElks/Tests/FortySixElksTransportTest.php
This file contains 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,91 @@ | ||
<?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\FortySixElks\Tests; | ||
|
||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\Notifier\Bridge\FortySixElks\FortySixElksTransport; | ||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Message\ChatMessage; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SentMessage; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Test\TransportTestCase; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
class FortySixElksTransportTest extends TransportTestCase | ||
{ | ||
public function createTransport(HttpClientInterface $client = null): FortySixElksTransport | ||
{ | ||
return new FortySixElksTransport('api_username', 'api_password', 'Symfony', $client ?? $this->createMock(HttpClientInterface::class)); | ||
} | ||
|
||
public function toStringProvider(): iterable | ||
{ | ||
yield ['forty-six-elks://api.46elks.com?from=Symfony', $this->createTransport()]; | ||
} | ||
|
||
public function supportedMessagesProvider(): iterable | ||
{ | ||
yield [new SmsMessage('+46701111111', 'Hello!')]; | ||
} | ||
|
||
public function unsupportedMessagesProvider(): iterable | ||
{ | ||
yield [new ChatMessage('Hello!')]; | ||
yield [$this->createMock(MessageInterface::class)]; | ||
} | ||
|
||
public function testSendSuccessfully() | ||
{ | ||
$response = $this->createMock(ResponseInterface::class); | ||
$response->method('getStatusCode')->willReturn(200); | ||
$response->method('getContent')->willReturn(file_get_contents(__DIR__.'/Fixtures/success-response.json')); | ||
$client = new MockHttpClient($response); | ||
$transport = $this->createTransport($client); | ||
$sentMessage = $transport->send(new SmsMessage('+46701111111', 'Hello!')); | ||
|
||
$this->assertInstanceOf(SentMessage::class, $sentMessage); | ||
$this->assertSame('s0231d6d7d6bc14a7e7734e466785c4ce', $sentMessage->getMessageId()); | ||
} | ||
|
||
/** | ||
* @dataProvider errorProvider | ||
*/ | ||
public function testExceptionIsThrownWhenSendFailed(int $statusCode, string $content, string $expectedExceptionMessage) | ||
{ | ||
$response = $this->createMock(ResponseInterface::class); | ||
$response->method('getStatusCode')->willReturn($statusCode); | ||
$response->method('getContent')->willReturn($content); | ||
$client = new MockHttpClient($response); | ||
$transport = $this->createTransport($client); | ||
|
||
$this->expectException(TransportException::class); | ||
$this->expectExceptionMessage($expectedExceptionMessage); | ||
|
||
$transport->send(new SmsMessage('+46701111111', 'Hello!')); | ||
} | ||
|
||
public function errorProvider(): iterable | ||
{ | ||
yield [ | ||
401, | ||
'API access requires Basic HTTP authentication. Read documentation or examples.', | ||
'Unable to post the 46elks message: API access requires Basic HTTP authentication. Read documentation or examples.', | ||
]; | ||
yield [ | ||
403, | ||
'Missing key from', | ||
'Unable to post the 46elks message: Missing key from', | ||
]; | ||
} | ||
} |
Oops, something went wrong.