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

Skip to content

[Notifier] Add Seven.io bridge #52936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2745,6 +2745,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
NotifierBridge\RocketChat\RocketChatTransportFactory::class => 'notifier.transport_factory.rocket-chat',
NotifierBridge\Sendberry\SendberryTransportFactory::class => 'notifier.transport_factory.sendberry',
NotifierBridge\SimpleTextin\SimpleTextinTransportFactory::class => 'notifier.transport_factory.simple-textin',
NotifierBridge\Sevenio\SevenIoTransportFactory::class => 'notifier.transport_factory.sevenio',
NotifierBridge\Sinch\SinchTransportFactory::class => 'notifier.transport_factory.sinch',
NotifierBridge\Slack\SlackTransportFactory::class => 'notifier.transport_factory.slack',
NotifierBridge\Sms77\Sms77TransportFactory::class => 'notifier.transport_factory.sms77',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
'redlink' => Bridge\Redlink\RedlinkTransportFactory::class,
'ring-central' => Bridge\RingCentral\RingCentralTransportFactory::class,
'sendberry' => Bridge\Sendberry\SendberryTransportFactory::class,
'sevenio' => Bridge\Sevenio\SevenIoTransportFactory::class,
'simple-textin' => Bridge\SimpleTextin\SimpleTextinTransportFactory::class,
'sinch' => Bridge\Sinch\SinchTransportFactory::class,
'sms-biuras' => Bridge\SmsBiuras\SmsBiurasTransportFactory::class,
Expand Down
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 src/Symfony/Component/Notifier/Bridge/Sevenio/.gitignore
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 src/Symfony/Component/Notifier/Bridge/Sevenio/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

7.1
---

* Add the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sevenio/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2023-present 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.
23 changes: 23 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sevenio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Seven.io Notifier
=================

Provides [Seven.io](https://www.seven.io/) integration for Symfony Notifier.

DSN example
-----------

```
SEVENIO_DSN=sevenio://API_KEY@default?from=FROM
```

where:
- `API_KEY` is your seven.io API key
- `FROM` is your sender (optional, default: SMS)

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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Sevenio;

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 Frank Nägler <[email protected]>
*/
final class SevenIoTransport extends AbstractTransport
{
protected const HOST = 'gateway.seven.io';

public function __construct(
#[\SensitiveParameter]
private string $apiKey,
private ?string $from = null,
HttpClientInterface $client = null,
EventDispatcherInterface $dispatcher = null,
) {
parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
return sprintf('sevenio://%s%s', $this->getEndpoint(), null !== $this->from ? '?from='.$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);
}

$response = $this->client->request('POST', sprintf('https://%s/api/sms', $this->getEndpoint()), [
'headers' => [
'Content-Type' => 'application/json',
'SentWith' => 'symfony/sevenio-notifier',
'X-Api-Key' => $this->apiKey,
],
'json' => [
'from' => $message->getFrom() ?: $this->from,
'json' => 1,
'text' => $message->getSubject(),
'to' => $message->getPhone(),
],
]);

try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote seven.io server.', $response, 0, $e);
}

if (200 !== $statusCode) {
$error = $response->toArray(false);

throw new TransportException(sprintf('Unable to send the SMS: "%s" (%s).', $error['description'], $error['code']), $response);
}

$success = $response->toArray(false);

if (false === \in_array($success['success'], [100, 101])) {
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $success['success']), $response);
}

$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId((int) $success['messages'][0]['id']);

return $sentMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Sevenio;

use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;

/**
* @author Frank Nägler <[email protected]>
*/
final class SevenIoTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): SevenIoTransport
{
$scheme = $dsn->getScheme();

if ('sevenio' !== $scheme) {
throw new UnsupportedSchemeException($dsn, 'sevenio', $this->getSupportedSchemes());
}

$apiKey = $this->getUser($dsn);
$from = $dsn->getOption('from');
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

return (new SevenIoTransport($apiKey, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

protected function getSupportedSchemes(): array
{
return ['sevenio'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Sevenio\Tests;

use Symfony\Component\Notifier\Bridge\Sevenio\SevenIoTransportFactory;
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;

final class SevenIoTransportFactoryTest extends TransportFactoryTestCase
{
public function createFactory(): SevenIoTransportFactory
{
return new SevenIoTransportFactory();
}

public static function createProvider(): iterable
{
yield [
'sevenio://host.test',
'sevenio://[email protected]',
];

yield [
'sevenio://host.test?from=TEST',
'sevenio://[email protected]?from=TEST',
];
}

public static function incompleteDsnProvider(): iterable
{
yield 'missing api key' => ['sevenio://host?from=TEST'];
}

public static function supportsProvider(): iterable
{
yield [true, 'sevenio://apiKey@default?from=TEST'];
yield [false, 'somethingElse://apiKey@default?from=TEST'];
}

public static function unsupportedSchemeProvider(): iterable
{
yield ['somethingElse://apiKey@default?from=FROM'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Sevenio\Tests;

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Notifier\Bridge\Sevenio\SevenIoTransport;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Test\TransportTestCase;
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final class SevenIoTransportTest extends TransportTestCase
{
public static function createTransport(HttpClientInterface $client = null, string $from = null): SevenIoTransport
{
return new SevenIoTransport('apiKey', $from, $client ?? new MockHttpClient());
}

public static function toStringProvider(): iterable
{
yield ['sevenio://gateway.seven.io', self::createTransport()];
yield ['sevenio://gateway.seven.io?from=TEST', self::createTransport(null, 'TEST')];
}

public static function supportedMessagesProvider(): iterable
{
yield [new SmsMessage('0611223344', 'Hello!')];
}

public static function unsupportedMessagesProvider(): iterable
{
yield [new ChatMessage('Hello!')];
yield [new DummyMessage()];
}
}
30 changes: 30 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sevenio/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "symfony/sevenio-notifier",
"type": "symfony-notifier-bridge",
"description": "Symfony Seven.io Notifier Bridge",
"keywords": ["sms", "sevenio", "notifier"],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Frank Nägler",
"email": "[email protected]"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"require": {
"php": ">=8.2",
"symfony/http-client": "^6.4|^7.0",
"symfony/notifier": "^7.1"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Sevenio\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev"
}
31 changes: 31 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Sevenio/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />
</php>

<testsuites>
<testsuite name="Symfony Seven.io Notifier Bridge Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ class UnsupportedSchemeException extends LogicException
'class' => Bridge\Sendberry\SendberryTransportFactory::class,
'package' => 'symfony/sendberry-notifier',
],
'sevenio' => [
'class' => Bridge\Sevenio\SevenIoTransportFactory::class,
'package' => 'symfony/sevenio-notifier',
],
'simpletextin' => [
'class' => Bridge\SimpleTextin\SimpleTextinTransportFactory::class,
'package' => 'symfony/simple-textin-notifier',
Expand Down
Loading