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

Skip to content

Commit 6b781fb

Browse files
NeoBlackOskarStark
authored andcommitted
[Notifier] Add Seven Notifier Bridge
Seven.io is the new name of SMS77, they changed also the URL to the gateway. To reflect that change, this patch introduces the new Seven Notifier Bridge. The current SMS77 Bridge should be deprecated in another patch
1 parent 52431f6 commit 6b781fb

File tree

16 files changed

+361
-0
lines changed

16 files changed

+361
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,6 +2745,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
27452745
NotifierBridge\RocketChat\RocketChatTransportFactory::class => 'notifier.transport_factory.rocket-chat',
27462746
NotifierBridge\Sendberry\SendberryTransportFactory::class => 'notifier.transport_factory.sendberry',
27472747
NotifierBridge\SimpleTextin\SimpleTextinTransportFactory::class => 'notifier.transport_factory.simple-textin',
2748+
NotifierBridge\Sevenio\SevenIoTransportFactory::class => 'notifier.transport_factory.sevenio',
27482749
NotifierBridge\Sinch\SinchTransportFactory::class => 'notifier.transport_factory.sinch',
27492750
NotifierBridge\Slack\SlackTransportFactory::class => 'notifier.transport_factory.slack',
27502751
NotifierBridge\Sms77\Sms77TransportFactory::class => 'notifier.transport_factory.sms77',

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
'sms-biuras' => Bridge\SmsBiuras\SmsBiurasTransportFactory::class,
109109
'smsbox' => Bridge\Smsbox\SmsboxTransportFactory::class,
110110
'sms-sluzba' => Bridge\SmsSluzba\SmsSluzbaTransportFactory::class,
111+
'sevenio' => Bridge\Sevenio\SevenIoTransportFactory::class,
111112
];
112113

113114
foreach ($texterFactories as $name => $class) {
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+
7.1
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) 2023-present 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+
Seven.io Notifier
2+
=================
3+
4+
Provides [Seven.io](https://www.seven.io/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
SEVENIO_DSN=sevenio://API_KEY@default?from=FROM
11+
```
12+
13+
where:
14+
- `API_KEY` is your seven.io 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: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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\Sevenio;
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 Frank Nägler <[email protected]>
26+
*/
27+
final class SevenIoTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'gateway.seven.io';
30+
31+
public function __construct(
32+
#[\SensitiveParameter]
33+
private string $apiKey,
34+
private ?string $from = null,
35+
HttpClientInterface $client = null,
36+
EventDispatcherInterface $dispatcher = null,
37+
) {
38+
parent::__construct($client, $dispatcher);
39+
}
40+
41+
public function __toString(): string
42+
{
43+
return sprintf('sevenio://%s%s', $this->getEndpoint(), null !== $this->from ? '?from='.$this->from : '');
44+
}
45+
46+
public function supports(MessageInterface $message): bool
47+
{
48+
return $message instanceof SmsMessage;
49+
}
50+
51+
protected function doSend(MessageInterface $message): SentMessage
52+
{
53+
if (!$message instanceof SmsMessage) {
54+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
55+
}
56+
57+
$endpoint = sprintf('https://%s/api/sms', $this->getEndpoint());
58+
$response = $this->client->request('POST', $endpoint, [
59+
'headers' => [
60+
'Content-Type' => 'application/json',
61+
'SentWith' => 'symfony/sevenio-notifier',
62+
'X-Api-Key' => $this->apiKey,
63+
],
64+
'json' => [
65+
'from' => $message->getFrom() ?: $this->from,
66+
'json' => 1,
67+
'text' => $message->getSubject(),
68+
'to' => $message->getPhone(),
69+
],
70+
]);
71+
72+
try {
73+
$statusCode = $response->getStatusCode();
74+
} catch (TransportExceptionInterface $e) {
75+
throw new TransportException('Could not reach the remote seven.io server.', $response, 0, $e);
76+
}
77+
78+
if (200 !== $statusCode) {
79+
$error = $response->toArray(false);
80+
81+
throw new TransportException(sprintf('Unable to send the SMS: "%s" (%s).', $error['description'], $error['code']), $response);
82+
}
83+
84+
$success = $response->toArray(false);
85+
86+
if (false === \in_array($success['success'], [100, 101])) {
87+
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $success['success']), $response);
88+
}
89+
90+
$sentMessage = new SentMessage($message, (string) $this);
91+
$sentMessage->setMessageId((int) $success['messages'][0]['id']);
92+
93+
return $sentMessage;
94+
}
95+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\Sevenio;
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 Frank Nägler <[email protected]>
20+
*/
21+
final class SevenIoTransportFactory extends AbstractTransportFactory
22+
{
23+
public function create(Dsn $dsn): SevenIoTransport
24+
{
25+
$scheme = $dsn->getScheme();
26+
27+
if ('sevenio' !== $scheme) {
28+
throw new UnsupportedSchemeException($dsn, 'sevenio', $this->getSupportedSchemes());
29+
}
30+
31+
$apiKey = $this->getUser($dsn);
32+
$from = $dsn->getOption('from');
33+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
34+
$port = $dsn->getPort();
35+
36+
return (new SevenIoTransport($apiKey, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
37+
}
38+
39+
protected function getSupportedSchemes(): array
40+
{
41+
return ['sevenio'];
42+
}
43+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\Sevenio\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Sevenio\SevenIoTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
17+
final class SevenIoTransportFactoryTest extends TransportFactoryTestCase
18+
{
19+
public function createFactory(): SevenIoTransportFactory
20+
{
21+
return new SevenIoTransportFactory();
22+
}
23+
24+
public static function createProvider(): iterable
25+
{
26+
yield [
27+
'sevenio://host.test',
28+
'sevenio://[email protected]',
29+
];
30+
31+
yield [
32+
'sevenio://host.test?from=TEST',
33+
'sevenio://[email protected]?from=TEST',
34+
];
35+
}
36+
37+
public static function incompleteDsnProvider(): iterable
38+
{
39+
yield 'missing api key' => ['sevenio://host?from=TEST'];
40+
}
41+
42+
public static function supportsProvider(): iterable
43+
{
44+
yield [true, 'sevenio://apiKey@default?from=TEST'];
45+
yield [false, 'somethingElse://apiKey@default?from=TEST'];
46+
}
47+
48+
public static function unsupportedSchemeProvider(): iterable
49+
{
50+
yield ['somethingElse://apiKey@default?from=FROM'];
51+
}
52+
}

0 commit comments

Comments
 (0)