[Notifier] add support for smsapi-notifier
This commit is contained in:
parent
5d15ce4e65
commit
91c25303f7
@ -106,6 +106,7 @@ use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
|
||||
@ -2104,6 +2105,7 @@ class FrameworkExtension extends Extension
|
||||
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
|
||||
ZulipTransportFactory::class => 'notifier.transport_factory.zulip',
|
||||
MobytTransportFactory::class => 'notifier.transport_factory.mobyt',
|
||||
SmsapiTransportFactory::class => 'notifier.transport_factory.smsapi',
|
||||
];
|
||||
|
||||
foreach ($classToServices as $class => $service) {
|
||||
|
@ -22,6 +22,7 @@ use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
|
||||
@ -90,6 +91,10 @@ return static function (ContainerConfigurator $container) {
|
||||
->parent('notifier.transport_factory.abstract')
|
||||
->tag('texter.transport_factory')
|
||||
|
||||
->set('notifier.transport_factory.smsapi', SmsapiTransportFactory::class)
|
||||
->parent('notifier.transport_factory.abstract')
|
||||
->tag('texter.transport_factory')
|
||||
|
||||
->set('notifier.transport_factory.null', NullTransportFactory::class)
|
||||
->parent('notifier.transport_factory.abstract')
|
||||
->tag('chatter.transport_factory')
|
||||
|
3
src/Symfony/Component/Notifier/Bridge/Smsapi/.gitattributes
vendored
Normal file
3
src/Symfony/Component/Notifier/Bridge/Smsapi/.gitattributes
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
/Tests export-ignore
|
||||
/phpunit.xml.dist export-ignore
|
||||
/.gitignore export-ignore
|
19
src/Symfony/Component/Notifier/Bridge/Smsapi/LICENSE
Normal file
19
src/Symfony/Component/Notifier/Bridge/Smsapi/LICENSE
Normal file
@ -0,0 +1,19 @@
|
||||
Copyright (c) 2020 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.
|
26
src/Symfony/Component/Notifier/Bridge/Smsapi/README.md
Normal file
26
src/Symfony/Component/Notifier/Bridge/Smsapi/README.md
Normal file
@ -0,0 +1,26 @@
|
||||
SMSAPI Notifier
|
||||
===============
|
||||
|
||||
Provides Smsapi integration for Symfony Notifier.
|
||||
|
||||
DSN example
|
||||
-----------
|
||||
|
||||
```
|
||||
// .env file
|
||||
SMSAPI_DSN=smsapi://TOKEN@default?from=FROM
|
||||
```
|
||||
|
||||
where:
|
||||
- `TOKEN` is API Token (OAuth)
|
||||
- `FROM` is sender name
|
||||
|
||||
See your account info at https://ssl.smsapi.pl/
|
||||
|
||||
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)
|
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Notifier\Bridge\Smsapi;
|
||||
|
||||
use Symfony\Component\Notifier\Exception\LogicException;
|
||||
use Symfony\Component\Notifier\Exception\TransportException;
|
||||
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\HttpClientInterface;
|
||||
|
||||
/**
|
||||
* @author Marcin Szepczynski <szepczynski@gmail.com>
|
||||
* @experimental in 5.2
|
||||
*/
|
||||
final class SmsapiTransport extends AbstractTransport
|
||||
{
|
||||
protected const HOST = 'api.smsapi.pl';
|
||||
|
||||
private $authToken;
|
||||
private $from;
|
||||
|
||||
public function __construct(string $authToken, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
|
||||
{
|
||||
$this->authToken = $authToken;
|
||||
$this->from = $from;
|
||||
|
||||
parent::__construct($client, $dispatcher);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf('smsapi://%s?from=%s', $this->getEndpoint(), $this->from);
|
||||
}
|
||||
|
||||
public function supports(MessageInterface $message): bool
|
||||
{
|
||||
return $message instanceof SmsMessage;
|
||||
}
|
||||
|
||||
protected function doSend(MessageInterface $message): SentMessage
|
||||
{
|
||||
if (!$message instanceof SmsMessage) {
|
||||
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, get_debug_type($message)));
|
||||
}
|
||||
|
||||
$endpoint = sprintf('https://%s/sms.do', $this->getEndpoint());
|
||||
$response = $this->client->request('POST', $endpoint, [
|
||||
'auth_bearer' => $this->authToken,
|
||||
'body' => [
|
||||
'from' => $this->from,
|
||||
'to' => $message->getPhone(),
|
||||
'message' => $message->getSubject(),
|
||||
'format' => 'json',
|
||||
],
|
||||
]);
|
||||
|
||||
if (200 !== $response->getStatusCode()) {
|
||||
$error = $response->toArray(false);
|
||||
|
||||
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error['message']), $response);
|
||||
}
|
||||
|
||||
return new SentMessage($message, (string) $this);
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Notifier\Bridge\Smsapi;
|
||||
|
||||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
|
||||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
|
||||
use Symfony\Component\Notifier\Transport\Dsn;
|
||||
use Symfony\Component\Notifier\Transport\TransportInterface;
|
||||
|
||||
/**
|
||||
* @author Marcin Szepczynski <szepczynski@gmail.com>
|
||||
* @experimental in 5.2
|
||||
*/
|
||||
class SmsapiTransportFactory extends AbstractTransportFactory
|
||||
{
|
||||
/**
|
||||
* @return SmsapiTransport
|
||||
*/
|
||||
public function create(Dsn $dsn): TransportInterface
|
||||
{
|
||||
$scheme = $dsn->getScheme();
|
||||
$authToken = $dsn->getUser();
|
||||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
|
||||
$from = $dsn->getOption('from');
|
||||
$port = $dsn->getPort();
|
||||
|
||||
if ('smsapi' === $scheme) {
|
||||
return (new SmsapiTransport($authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
|
||||
}
|
||||
|
||||
throw new UnsupportedSchemeException($dsn, 'smsapi', $this->getSupportedSchemes());
|
||||
}
|
||||
|
||||
protected function getSupportedSchemes(): array
|
||||
{
|
||||
return ['smsapi'];
|
||||
}
|
||||
}
|
37
src/Symfony/Component/Notifier/Bridge/Smsapi/composer.json
Normal file
37
src/Symfony/Component/Notifier/Bridge/Smsapi/composer.json
Normal file
@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "symfony/smsapi-notifier",
|
||||
"type": "symfony-bridge",
|
||||
"description": "Symfony Smsapi Notifier Bridge",
|
||||
"keywords": ["sms", "smsapi", "notifier"],
|
||||
"homepage": "https://mvpdoers.com",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Marcin Szepczynski",
|
||||
"email": "szepczynski@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "https://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.2.5",
|
||||
"symfony/http-client": "^4.3|^5.0",
|
||||
"symfony/notifier": "^5.2"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Symfony\\Component\\Notifier\\Bridge\\Smsapi\\": ""
|
||||
},
|
||||
"exclude-from-classmap": [
|
||||
"/Tests/"
|
||||
]
|
||||
},
|
||||
"minimum-stability": "dev",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "5.2-dev"
|
||||
}
|
||||
}
|
||||
}
|
@ -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 Smsapi 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>
|
@ -74,6 +74,10 @@ class UnsupportedSchemeException extends LogicException
|
||||
'class' => Bridge\Zulip\ZulipTransportFactory::class,
|
||||
'package' => 'symfony/zulip-notifier',
|
||||
],
|
||||
'smsapi' => [
|
||||
'class' => Bridge\Smsapi\SmsapiTransportFactory::class,
|
||||
'package' => 'symfony/smsapi-notifier',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -21,6 +21,7 @@ use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
|
||||
@ -56,6 +57,7 @@ class Transport
|
||||
FreeMobileTransportFactory::class,
|
||||
ZulipTransportFactory::class,
|
||||
MobytTransportFactory::class,
|
||||
SmsapiTransportFactory::class,
|
||||
];
|
||||
|
||||
private $factories;
|
||||
|
Reference in New Issue
Block a user