8000 Added 46elks notifier bridge · symfony/symfony@b08871f · GitHub
[go: up one dir, main page]

Skip to content

Commit b08871f

Browse files
committed
Added 46elks notifier bridge
1 parent 4b41d17 commit b08871f

File tree

18 files changed

+404
-1
lines changed

18 files changed

+404
-1
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;
@@ -2419,6 +2420,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24192420
FakeChatTransportFactory::class => 'notifier.transport_factory.fake-chat',
24202421
FakeSmsTransportFactory::class => 'notifier.transport_factory.fake-sms',
24212422
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',
2423+
FortysixElksTransportFactory::class => 'notifier.transport_factory.46elks',
24222424
FreeMobileTransportFactory::class => 'notifier.transport_factory.free-mobile',
24232425
GatewayApiTransportFactory::class => 'notifier.transport_factory.gateway-api',
24242426
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;
@@ -104,6 +105,10 @@
104105
->parent('notifier.transport_factory.abstract')
105106
->tag('chatter.transport_factory')
106107

108+
->set('notifier.transport_factory.46elks', FortysixElksTransportFactory::class)
109+
->parent('notifier.transport_factory.abstract')
110+
->tag('texter.transport_factory')
111+
107112
->set('notifier.transport_factory.free-mobile', FreeMobileTransportFactory::class)
108113
->parent('notifier.transport_factory.abstract')
109114
->tag('texter.transport_factory')

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1896,7 +1896,7 @@ public function testIfNotifierTransportsAreKnownByFrameworkExtension()
18961896

18971897
$container = $this->createContainerFromFile('notifier');
18981898

1899-
foreach ((new Finder())->in(\dirname(__DIR__, 4).'/Component/Notifier/Bridge')->directories()->depth(0)->exclude('Mercure') as $bridgeDirectory) {
1899+
foreach ((new Finder())->in(\dirname(__DIR__, 4).'/Component/Notifier/Bridge')->directories()->depth(0)->exclude(['Mercure', 'FortysixElks']) as $bridgeDirectory) {
19001900
$transportFactoryName = strtolower(preg_replace('/(.)([A-Z])/', '$1-$2', $bridgeDirectory->getFilename()));
19011901
$this->assertTrue($container->hasDefinition('notifier.transport_factory.'.$transportFactoryName), sprintf('Did you forget to add the "%s" TransportFactory to the $classToServices array in FrameworkExtension?', $bridgeDirectory->getFilename()));
19021902
}
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 <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\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 <jon@jon.se>
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('46elks://%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+
}
B41A 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 <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\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 <jon@jon.se>< F438 /span>
20+
*/
21+
final class FortysixElksTransportFactory extends AbstractTransportFactory
22+
{
23+
public function create(Dsn $dsn): FortysixElksTransport
24+
{
25+
if ('46elks' !== $dsn->getScheme()) {
26+
throw new UnsupportedSchemeException($dsn, '46elks', $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 ['46elks'];
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+
FORTYSIX_ELKS_DSN=46elks://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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\FortysixElks\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\FortysixElks\FortysixElksTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
17+
final class FortysixElksTransportFactoryTest extends TransportFactoryTestCase
18+
{
19+
public function createFactory(): FortysixElksTransportFactory
20+
{
21+
return new FortysixElksTransportFactory();
22+
}
23+
24+
public function createProvider(): iterable
25+
{
26+
yield [
27+
'46elks://host.test?from=Symfony',
28+
'46elks://api_username:api_password@host.test?from=Symfony',
29+
];
30+
}
31+
32+
public function supportsProvider(): iterable
33+
{
34+
yield [true, '46elks://api_key@default'];
35+
yield [false, 'somethingElse://api_key@default'];
36+
}
37+
38+
public function unsupportedSchemeProvider(): iterable
39+
{
40+
yield ['somethingElse://api_key@default'];
41+
}
42+
}
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 <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\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+
final 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 ['46elks://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)
0