Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6153815

Browse files
committed
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
2 parents 0e67f9e + 1594296 commit 6153815

File tree

17 files changed

+402
-0
lines changed

17 files changed

+402
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatTransportFactory;
123123
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
124124
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
125+
use Symfony\Component\Notifier\Bridge\FortySixElks\FortySixElksTransportFactory;
125126
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
126127
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
127128
use Symfony\Component\Notifier\Bridge\Gitter\GitterTransportFactory;
@@ -2421,6 +2422,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24212422
FakeChatTransportFactory::class => 'notifier.transport_factory.fake-chat',
24222423
FakeSmsTransportFactory::class => 'notifier.transport_factory.fake-sms',
24232424
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',
2425+
FortySixElksTransportFactory::class => 'notifier.transport_factory.forty-six-elks',
24242426
FreeMobileTransportFactory::class => 'notifier.transport_factory.free-mobile',
24252427
GatewayApiTransportFactory::class => 'notifier.transport_factory.gateway-api',
24262428
GitterTransportFactory::class => 'notifier.transport_factory.gitter',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatTransportFactory;
2121
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
2222
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
23+
use Symfony\Component\Notifier\Bridge\FortySixElks\FortySixElksTransportFactory;
2324
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
2425
use Symfony\Component\Notifier\Bridge\GatewayApi\GatewayApiTransportFactory;
2526
use Symfony\Component\Notifier\Bridge\Gitter\GitterTransportFactory;
@@ -105,6 +106,10 @@
105106
->parent('notifier.transport_factory.abstract')
106107
->tag('chatter.transport_factory')
107108

109+
->set('notifier.transport_factory.forty-six-elks', FortySixElksTransportFactory::class)
110+
->parent('notifier.transport_factory.abstract')
111+
->tag('texter.transport_factory')
112+
108113
->set('notifier.transport_factory.free-mobile', FreeMobileTransportFactory::class)
109114
->parent('notifier.transport_factory.abstract')
110115
->tag('texter.transport_factory')
Lines 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
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
6.1
5+
---
6+
7+
* Add the bridge
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\FortySixElks;
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\Exception\TransportExceptionInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @author Jon Gotlin <[email protected]>
26+
*/
27+
final class FortySixElksTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'api.46elks.com';
30+
31+
private string $apiUsername;
32+
private string $apiPassword;
33+
private string $from;
34+
35+
public function __construct(string $apiUsername, string $apiPassword, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
{
37+
$this->apiUsername = $apiUsername;
38+
$this->apiPassword = $apiPassword;
39+
$this->from = $from;
40+
41+
parent::__construct($client, $dispatcher);
42+
}
43+
44+
public function __toString(): string
45+
{
46+
return sprintf('forty-six-elks://%s?from=%s', $this->getEndpoint(), $this->from);
47+
}
48+
49+
public function supports(MessageInterface $message): bool
50+
{
51+
return $message instanceof SmsMessage;
52+
}
53+
54+
protected function doSend(MessageInterface $message): SentMessage
55+
{
56+
if (!$message instanceof SmsMessage) {
57+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
58+
}
59+
60+
$endpoint = sprintf('https://%s/a1/sms', self::HOST);
61+
$response = $this->client->request('POST', $endpoint, [
62+
'body' => [
63+
'from' => $this->from,
64+
'to' => $message->getPhone(),
65+
'message' => $message->getSubject(),
66+
],
67+
'auth_basic' => [$this->apiUsername, $this->apiPassword],
68+
]);
69+
70+
try {
71+
$statusCode = $response->getStatusCode();
72+
} catch (TransportExceptionInterface $e) {
73+
throw new TransportException('Could not reach the remote 46elks server.', $response, 0, $e);
74+
}
75+
76+
if (200 !== $statusCode) {
77+
throw new TransportException('Unable to post the 46elks message: '.$response->getContent(false), $response);
78+
}
79+
80+
$result = $response->toArray(false);
81+
82+
$sentMessage = new SentMessage($message, (string) $this);
83+
$sentMessage->setMessageId($result['id'] ?? '');
84+
85+
return $sentMessage;
86+
}
87+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\FortySixElks;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
18+
/**
19+
* @author Jon Gotlin <[email protected]>
20+
*/
21+
final class FortySixElksTransportFactory extends AbstractTransportFactory
22+
{
23+
public function create(Dsn $dsn): FortySixElksTransport
24+
{
25+
if ('forty-six-elks' !== $dsn->getScheme()) {
26+
throw new UnsupportedSchemeException($dsn, 'forty-six-elks', $this->getSupportedSchemes());
27+
}
28+
29+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
30+
$from = $dsn->getRequiredOption('from');
31+
32+
return (new FortySixElksTransport($this->getUser($dsn), $this->getPassword($dsn), $from, $this->client, $this->dispatcher))->setHost($host)->setPort($dsn->getPort());
33+
}
34+
35+
protected function getSupportedSchemes(): array
36+
{
37+
return ['forty-six-elks'];
38+
}
39+
}
Lines changed: 19 additions & 0 deletions
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.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
46elks Notifier
2+
===============
3+
4+
Provides [46elks](https://46elks.se) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
FORTY_SIX_ELKS_DSN=forty-six-elks://API_USERNAME:API_PASSWORD@default?from=FROM
11+
```
12+
13+
where:
14+
- `API_USERNAME` is your 46elks API username
15+
- `API_PASSWORD` is your 46elks API password
16+
- `FROM` is the alphanumeric originator for the message to appear to originate from
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)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"status": "created",
3+
"direction": "outgoing",
4+
"from": "+46702222222",
5+
"created": "2021-12-30T20:35:32.429389",
6+
"parts": 1,
7+
"to": "+46701111111",
8+
"cost": 3900,
9+
"message": "Symfony test",
10+
"id": "s0231d6d7d6bc14a7e7734e466785c4ce"
11+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
*/
10+
11+
namespace Symfony\Component\Notifier\Bridge\FortySixElks\Tests;
12+
13+
use Symfony\Component\Notifier\Bridge\FortySixElks\FortySixElksTransportFactory;
14+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
15+
16+
class FortySixElksTransportFactoryTest extends TransportFactoryTestCase
17+
{
18+
public function createFactory(): FortySixElksTransportFactory
19+
{
20+
return new FortySixElksTransportFactory();
21+
}
22+
23+
public function createProvider(): iterable
24+
{
25+
yield [
26+
'forty-six-elks://host.test?from=Symfony',
27+
'forty-six-elks://api_username:[email protected]?from=Symfony',
28+
];
29+
}
30+
31+
public function supportsProvider(): iterable
32+
{
33+
yield [true, 'forty-six-elks://api_key@default'];
34+
yield [false, 'somethingElse://api_key@default'];
35+
}
36+
37+
public function unsupportedSchemeProvider(): iterable
38+
{
39+
yield ['somethingElse://api_key@default'];
40+
}
41+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\FortySixElks\Tests;
13+
14+
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\Notifier\Bridge\FortySixElks\FortySixElksTransport;
16+
use Symfony\Component\Notifier\Exception\TransportException;
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\Message\SmsMessage;
21+
use Symfony\Component\Notifier\Test\TransportTestCase;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
use Symfony\Contracts\HttpClient\ResponseInterface;
24+
25+
class FortySixElksTransportTest extends TransportTestCase
26+
{
27+
public function createTransport(HttpClientInterface $client = null): FortySixElksTransport
28+
{
29+
return new FortySixElksTransport('api_username', 'api_password', 'Symfony', $client ?? $this->createMock(HttpClientInterface::class));
30+
}
31+
32+
public function toStringProvider(): iterable
33+
{
34+
yield ['forty-six-elks://api.46elks.com?from=Symfony', $this->createTransport()];
35+
}
36+
37+
public function supportedMessagesProvider(): iterable
38+
{
39+
yield [new SmsMessage('+46701111111', 'Hello!')];
40+
}
41+
42+
public function unsupportedMessagesProvider(): iterable
43+
{
44+
yield [new ChatMessage('Hello!')];
45+
yield [$this->createMock(MessageInterface::class)];
46+
}
47+
48+
public function testSendSuccessfully()
49+
{
50+
$response = $this->createMock(ResponseInterface::class);
51+
$response->method('getStatusCode')->willReturn(200);
52+
$response->method('getContent')->willReturn(file_get_contents(__DIR__.'/Fixtures/success-response.json'));
53+
$client = new MockHttpClient($response);
54+
$transport = $this->createTransport($client);
55+
$sentMessage = $transport->send(new SmsMessage('+46701111111', 'Hello!'));
56+
57+
$this->assertInstanceOf(SentMessage::class, $sentMessage);
58+
$this->assertSame('s0231d6d7d6bc14a7e7734e466785c4ce', $sentMessage->getMessageId());
59+
}
60+
61+
/**
62+
* @dataProvider errorProvider
63+
*/
64+
public function testExceptionIsThrownWhenSendFailed(int $statusCode, string $content, string $expectedExceptionMessage)
65+
{
66+
$response = $this->createMock(ResponseInterface::class);
67+
$response->method('getStatusCode')->willReturn($statusCode);
68+
$response->method('getContent')->willReturn($content);
69+
$client = new MockHttpClient($response);
70+
$transport = $this->createTransport($client);
71+
72+
$this->expectException(TransportException::class);
73+
$this->expectExceptionMessage($expectedExceptionMessage);
74+
75+
$transport->send(new SmsMessage('+46701111111', 'Hello!'));
76+
}
77+
78+
public function errorProvider(): iterable
79+
{
80+
yield [
81+
401,
82+
'API access requires Basic HTTP authentication. Read documentation or examples.',
83+
'Unable to post the 46elks message: API access requires Basic HTTP authentication. Read documentation or examples.',
84+
];
85+
yield [
86+
403,
87+
'Missing key from',
88+
'Unable to post the 46elks message: Missing key from',
89+
];
90+
}
91+
}

0 commit comments

Comments
 (0)