8000 feature #43134 [Notifier] Add sms77 Notifier Bridge (matthiez) · symfony/symfony@a8a57a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit a8a57a1

Browse files
committed
feature #43134 [Notifier] Add sms77 Notifier Bridge (matthiez)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Notifier] Add sms77 Notifier Bridge | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT | Doc PR | [symfony/symfony-docs/pull/15840](symfony/symfony-docs#15840) | Recipe PR | [symfony/recipes/pull/1003](symfony/recipes#1003) Add sms77 bridge to Symfony Notifier. A Germany based SMS provider established in 2003. Commits ------- 6d5be78 [Notifier] Add sms77 Notifier Bridge
2 parents 21cc2b6 + 6d5be78 commit a8a57a1

17 files changed

+385
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory as SendinblueNotifierTransportFactory;
141141
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
142142
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
143+
use Symfony\Component\Notifier\Bridge\Sms77\Sms77TransportFactory;
143144
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
144145
use Symfony\Component\Notifier\Bridge\SmsBiuras\SmsBiurasTransportFactory;
145146
use Symfony\Component\Notifier\Bridge\Smsc\SmscTransportFactory;
@@ -2469,6 +2470,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24692470
SendinblueNotifierTransportFactory::class => 'notifier.transport_factory.sendinblue',
24702471
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
24712472
SlackTransportFactory::class => 'notifier.transport_factory.slack',
2473+
Sms77TransportFactory::class => 'notifier.transport_factory.sms77',
24722474
SmsapiTransportFactory::class => 'notifier.transport_factory.smsapi',
24732475
SmsBiurasTransportFactory::class => 'notifier.transport_factory.smsbiuras',
24742476
SmscTransportFactory::class => 'notifier.transport_factory.smsc',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory;
4242
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
4343
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
44+
use Symfony\Component\Notifier\Bridge\Sms77\Sms77TransportFactory;
4445
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
4546
use Symfony\Component\Notifier\Bridge\SmsBiuras\SmsBiurasTransportFactory;
4647
use Symfony\Component\Notifier\Bridge\Smsc\SmscTransportFactory;
@@ -225,5 +226,9 @@
225226
->set('notifier.transport_factory.turbosms', TurboSmsTransportFactory::class)
226227
->parent('notifier.transport_factory.abstract')
227228
->tag('texter.transport_factory')
229+
230+
->set('notifier.transport_factory.sms77', Sms77TransportFactory::class)
231+
->parent('notifier.transport_factory.abstract')
232+
->tag('texter.transport_factory')
228233
;
229234
};
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+
5.4
5+
---
6+
7+
* Add the bridge
Lines 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.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
sms77 Notifier
2+
=================
3+
4+
Provides [sms77](https://www.sms77.io/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
SMS77_DSN=sms77://API_KEY@default?from=FROM
11+
```
12+
13+
where:
14+
- `API_KEY` is your sms77 API key
15+
- `FROM` is your sender (optional, default: SMS)
16+
17+
Resources
18+
---------
19+
20+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
21+
* [Report issues](https://github.com/symfony/symfony/issues) and
22+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
23+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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\Sms77;
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 André Matthies <matthiez@gmail.com>
26+
*/
27+
final class Sms77Transport extends AbstractTransport
28+
{
29+
protected const HOST = 'gateway.sms77.io';
30+
31+
private $apiKey;
32+
private $from;
33+
34+
public function __construct(string $apiKey, string $from = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
35+
{
36+
$this->apiKey = $apiKey;
37+
$this->from = $from;
38+
39+
parent::__construct($client, $dispatcher);
40+
}
41+
42+
public function __toString(): string
43+
{
44+
if (null === $this->from) {
45+
return sprintf('sms77://%s', $this->getEndpoint());
46+
}
47+
48+
return sprintf('sms77://%s?from=%s', $this->getEndpoint(), $this->from);
49+
}
50+
51+
public function supports(MessageInterface $message): bool
52+
{
53+
return $message instanceof SmsMessage;
54+
}
55+
56+
protected function doSend(MessageInterface $message): SentMessage
57+
{
58+
if (!$message instanceof SmsMessage) {
59+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
60+
}
61+
62+
$endpoint = sprintf('https://%s/api/sms', $this->getEndpoint());
63+
$response = $this->client->request('POST', $endpoint, [
64+
'headers' => [
65+
'Content-Type' => 'application/json',
66+
'SentWith' => 'Symfony Notifier',
67+
'X-Api-Key' => $this->apiKey,
68+
],
69+
'json' => [
70+
'from' => $this->from,
71+
'json' => 1,
72+
'text' => $message->getSubject(),
73+
'to' => $message->getPhone(),
74+
],
75+
]);
76+
77+
try {
78+
$statusCode = $response->getStatusCode();
79+
} catch (TransportExceptionInterface $e) {
80+
throw new TransportException('Could not reach the remote Sms77 server.', $response, 0, $e);
81+
}
82+
83+
if (200 !== $statusCode) {
84+
$error = $response->toArray(false);
85+
86+
throw new TransportException(sprintf('Unable to send the SMS: "%s" (%s).', $error['description'], $error['code']), $response);
87+
}
88+
89+
$success = $response->toArray(false);
90+
91+
if (false === \in_array($success['success'], [100, 101])) {
92+
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $success['success']), $response);
93+
}
94+
95+
$sentMessage = new SentMessage($message, (string) $this);
96+
$sentMessage->setMessageId((int) $success['messages'][0]['id']);
97+
98+
return $sentMessage;
99+
}
100+
}
Lines 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\Sms77;
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 André Matthies <matthiez@gmail.com>
21+
*/
22+
final class Sms77TransportFactory extends AbstractTransportFactory
23+
{
24+
/**
25+
* @return Sms77Transport
26+
*/
27+
public function create(Dsn $dsn): TransportInterface
28+
{
29+
$scheme = $dsn->getScheme();
30+
31+
if ('sms77' !== $scheme) {
32+
throw new UnsupportedSchemeException($dsn, 'sms77', $this->getSupportedSchemes());
33+
}
34+
35+
$apiKey = $this->getUser($dsn);
36+
$from = $dsn->getOption('from');
37+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
38+
$port = $dsn->getPort();
39+
40+
return (new Sms77Transport($apiKey, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
41+
}
42+
43+
protected function getSupportedSchemes(): array
44+
{
45+
return ['sms77'];
46+
}
47+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Sms77\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Sms77\Sms77TransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
17+
18+
final class Sms77TransportFactoryTest extends TransportFactoryTestCase
19+
{
20+
/**
21+
* @return Sms77TransportFactory
22+
*/
23+
public function createFactory(): TransportFactoryInterface
24+
{
25+
return new Sms77TransportFactory();
26+
}
27+
28+
public function createProvider(): iterable
29+
{
30+
yield [
31+
'sms77://host.test',
32+
'sms77://apiKey@host.test',
33+
];
34+
35+
yield [
36+
'sms77://host.test?from=TEST',
37+
'sms77://apiKey@host.test?from=TEST',
38+
];
39+
}
40+
41+
public function incompleteDsnProvider(): iterable
42+
{
43+
yield 'missing api key' => ['sms77://host?from=TEST'];
44+
}
45+
46+
public function supportsProvider(): iterable
47+
{
48+
yield [true, 'sms77://apiKey@default?from=TEST'];
49+
yield [false, 'somethingElse://apiKey@default?from=TEST'];
50+
}
51+
52+
public function unsupportedSchemeProvider(): iterable
53+
{
54+
yield ['somethingElse://apiKey@default?from=FROM'];
55+
}
56+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\Sms77\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Sms77\Sms77Transport;
15+
use Symfony\Component\Notifier\Message\ChatMessage;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SmsMessage;
18+
use Symfony\Component\Notifier\Test\TransportTestCase;
19+
use Symfony\Component\Notifier\Transport\TransportInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
final class Sms77TransportTest extends TransportTestCase
23+
{
24+
/**
25+
* @return Sms77Transport
26+
*/
27+
public function createTransport(HttpClientInterface $client = null, string $from = null): TransportInterface
28+
{
29+
return new Sms77Transport('apiKey', $from, $client ?? $this->createMock(HttpClientInterface::class));
30+
}
31+
32+
public function toStringProvider(): iterable
33+
{
34+
yield ['sms77://gateway.sms77.io', $this->createTransport()];
35+
yield ['sms77://gateway.sms77.io?from=TEST', $this->createTransport(null, 'TEST')];
36+
}
37+
38+
public function supportedMessagesProvider(): iterable
39+
{
40+
yield [new SmsMessage('0611223344', 'Hello!')];
41+
}
42+
43+
public function unsupportedMessagesProvider(): iterable
44+
{
45+
yield [new ChatMessage('Hello!')];
46+
yield [$this->createMock(MessageInterface::class)];
47+
}
48+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "symfony/sms77-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony sms77 Notifier Bridge",
5+
"keywords": ["sms", "sms77", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "André Matthies",
11+
"email": "matthiez@gmail.com"
12+
},
13+
{
14+
"name": "Symfony Community",
15+
"homepage": "https://symfony.com/contributors"
16+
}
17+
],
18+
"require": {
19+
"php": ">=7.2.5",
20+
"symfony/http-client": "^4.3|^5.0|^6.0",
21+
"symfony/notifier": "^5.3|^6.0"
22+
},
23+
"autoload": {
24+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Sms77\\": "" },
25+
"exclude-from-classmap": [
26+
"/Tests/"
27+
]
28+
},
29+
"minimum-stability": "dev"
30+
}

0 commit comments

Comments
 (0)
0