Add Octopush notifier transport
This commit is contained in:
parent
e55762dbdd
commit
93dde15dd2
@ -113,6 +113,7 @@ use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory as SendinblueNotifierTransportFactory;
|
||||
@ -2238,6 +2239,7 @@ class FrameworkExtension extends Extension
|
||||
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
|
||||
LinkedInTransportFactory::class => 'notifier.transport_factory.linkedin',
|
||||
GatewayApiTransportFactory::class => 'notifier.transport_factory.gatewayapi',
|
||||
OctopushTransportFactory::class => 'notifier.transport_factory.octopush',
|
||||
];
|
||||
|
||||
foreach ($classToServices as $class => $service) {
|
||||
|
@ -23,6 +23,7 @@ use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory;
|
||||
@ -117,6 +118,10 @@ return static function (ContainerConfigurator $container) {
|
||||
->parent('notifier.transport_factory.abstract')
|
||||
->tag('texter.transport_factory')
|
||||
|
||||
->set('notifier.transport_factory.octopush', OctopushTransportFactory::class)
|
||||
->parent('notifier.transport_factory.abstract')
|
||||
->tag('texter.transport_factory')
|
||||
|
||||
->set('notifier.transport_factory.discord', DiscordTransportFactory::class)
|
||||
->parent('notifier.transport_factory.abstract')
|
||||
->tag('chatter.transport_factory')
|
||||
|
4
src/Symfony/Component/Notifier/Bridge/Octopush/.gitattributes
vendored
Normal file
4
src/Symfony/Component/Notifier/Bridge/Octopush/.gitattributes
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/Tests export-ignore
|
||||
/phpunit.xml.dist export-ignore
|
||||
/.gitattributes export-ignore
|
||||
/.gitignore export-ignore
|
@ -0,0 +1,7 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
5.3
|
||||
---
|
||||
|
||||
* Add the bridge
|
19
src/Symfony/Component/Notifier/Bridge/Octopush/LICENSE
Normal file
19
src/Symfony/Component/Notifier/Bridge/Octopush/LICENSE
Normal file
@ -0,0 +1,19 @@
|
||||
Copyright (c) 2021 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.
|
@ -0,0 +1,90 @@
|
||||
<?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\Octopush;
|
||||
|
||||
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\HttpClientInterface;
|
||||
|
||||
/**
|
||||
* @author Aurélien Martin <pro@aurelienmartin.com>
|
||||
*/
|
||||
final class OctopushTransport extends AbstractTransport
|
||||
{
|
||||
protected const HOST = 'www.octopush-dm.com';
|
||||
|
||||
private $userLogin;
|
||||
private $apiKey;
|
||||
private $from;
|
||||
private $type;
|
||||
|
||||
public function __construct(string $userLogin, string $apiKey, string $from, string $type, ?HttpClientInterface $client = null, ?EventDispatcherInterface $dispatcher = null)
|
||||
{
|
||||
$this->userLogin = $userLogin;
|
||||
$this->apiKey = $apiKey;
|
||||
$this->from = $from;
|
||||
$this->type = $type;
|
||||
|
||||
parent::__construct($client, $dispatcher);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf('octopush://%s?from=%s&type=%s', $this->getEndpoint(), $this->from, $this->type);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
$endpoint = sprintf('https://%s/api/sms/json', $this->getEndpoint());
|
||||
|
||||
$response = $this->client->request('POST', $endpoint, [
|
||||
'headers' => [
|
||||
'content_type' => 'multipart/form-data',
|
||||
],
|
||||
'body' => [
|
||||
'user_login' => $this->userLogin,
|
||||
'api_key' => $this->apiKey,
|
||||
'sms_text' => $message->getSubject(),
|
||||
'sms_recipients' => $message->getPhone(),
|
||||
'sms_sender' => $this->from,
|
||||
'sms_type' => $this->type,
|
||||
],
|
||||
]);
|
||||
|
||||
if (200 !== $response->getStatusCode()) {
|
||||
$error = $response->toArray(false);
|
||||
|
||||
throw new TransportException('Unable to send the SMS: '.$error['error_code'], $response);
|
||||
}
|
||||
|
||||
$success = $response->toArray(false);
|
||||
|
||||
$sentMessage = new SentMessage($message, (string) $this);
|
||||
$sentMessage->setMessageId($success['ticket']);
|
||||
|
||||
return $sentMessage;
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
<?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\Octopush;
|
||||
|
||||
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 Aurélien Martin <pro@aurelienmartin.com>
|
||||
*/
|
||||
final class OctopushTransportFactory extends AbstractTransportFactory
|
||||
{
|
||||
/**
|
||||
* @return OctopushTransport
|
||||
*/
|
||||
public function create(Dsn $dsn): TransportInterface
|
||||
{
|
||||
$scheme = $dsn->getScheme();
|
||||
|
||||
if ('octopush' !== $scheme) {
|
||||
throw new UnsupportedSchemeException($dsn, 'octopush', $this->getSupportedSchemes());
|
||||
}
|
||||
|
||||
$userLogin = urlencode($this->getUser($dsn));
|
||||
$apiKey = $this->getPassword($dsn);
|
||||
$from = $dsn->getRequiredOption('from');
|
||||
$type = $dsn->getRequiredOption('type');
|
||||
|
||||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
|
||||
$port = $dsn->getPort();
|
||||
|
||||
return (new OctopushTransport($userLogin, $apiKey, $from, $type, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
|
||||
}
|
||||
|
||||
protected function getSupportedSchemes(): array
|
||||
{
|
||||
return ['octopush'];
|
||||
}
|
||||
}
|
24
src/Symfony/Component/Notifier/Bridge/Octopush/README.md
Normal file
24
src/Symfony/Component/Notifier/Bridge/Octopush/README.md
Normal file
@ -0,0 +1,24 @@
|
||||
Octopush Notifier
|
||||
=================
|
||||
|
||||
Provides [Octopush](https://www.octopush.com) integration for Symfony Notifier.
|
||||
|
||||
DSN example
|
||||
-----------
|
||||
|
||||
```
|
||||
OCTOPUSH_DSN=octopush://USERLOGIN:APIKEY@default?from=FROM&type=TYPE
|
||||
```
|
||||
|
||||
where:
|
||||
- `USERLOGIN` is your Octopush email
|
||||
- `APIKEY` is your Octopush token
|
||||
- `FROM` is your sender
|
||||
- `TYPE` is Octopush sms type (`XXX` = SMS LowCost; `FR` = SMS Premium; `WWW` = SMS World)
|
||||
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,53 @@
|
||||
<?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\Octopush\Tests;
|
||||
|
||||
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransportFactory;
|
||||
use Symfony\Component\Notifier\Tests\TransportFactoryTestCase;
|
||||
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
|
||||
|
||||
final class OctopushTransportFactoryTest extends TransportFactoryTestCase
|
||||
{
|
||||
/**
|
||||
* @return OctopushTransportFactory
|
||||
*/
|
||||
public function createFactory(): TransportFactoryInterface
|
||||
{
|
||||
return new OctopushTransportFactory();
|
||||
}
|
||||
|
||||
public function createProvider(): iterable
|
||||
{
|
||||
yield [
|
||||
'octopush://host.test?from=Heyliot&type=FR',
|
||||
'octopush://userLogin:apiKey@host.test?from=Heyliot&type=FR',
|
||||
];
|
||||
}
|
||||
|
||||
public function supportsProvider(): iterable
|
||||
{
|
||||
yield [true, 'octopush://userLogin:apiKey@default?from=Heyliot&type=FR'];
|
||||
yield [false, 'somethingElse://userLogin:apiKet@default?from=Heyliot&type=FR'];
|
||||
}
|
||||
|
||||
public function incompleteDsnProvider(): iterable
|
||||
{
|
||||
yield 'missing option: from' => ['octopush://userLogin:apiKey@default?type=FR'];
|
||||
yield 'missing option: type' => ['octopush://userLogin:apiKey@default?from=Heyliot'];
|
||||
}
|
||||
|
||||
public function unsupportedSchemeProvider(): iterable
|
||||
{
|
||||
yield ['somethingElse://userLogin:apiKey@default?from=0611223344'];
|
||||
yield ['somethingElse://userLogin:apiKey@default']; // missing "from" option
|
||||
}
|
||||
}
|
@ -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\Octopush\Tests;
|
||||
|
||||
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransport;
|
||||
use Symfony\Component\Notifier\Message\ChatMessage;
|
||||
use Symfony\Component\Notifier\Message\MessageInterface;
|
||||
use Symfony\Component\Notifier\Message\SmsMessage;
|
||||
use Symfony\Component\Notifier\Tests\TransportTestCase;
|
||||
use Symfony\Component\Notifier\Transport\TransportInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
final class OctopushTransportTest extends TransportTestCase
|
||||
{
|
||||
/**
|
||||
* @return OctopushTransport
|
||||
*/
|
||||
public function createTransport(?HttpClientInterface $client = null): TransportInterface
|
||||
{
|
||||
return new OctopushTransport('userLogin', 'apiKey', 'from', 'type', $client ?: $this->createMock(HttpClientInterface::class));
|
||||
}
|
||||
|
||||
public function toStringProvider(): iterable
|
||||
{
|
||||
yield ['octopush://www.octopush-dm.com?from=from&type=type', $this->createTransport()];
|
||||
}
|
||||
|
||||
public function supportedMessagesProvider(): iterable
|
||||
{
|
||||
yield [new SmsMessage('33611223344', 'Hello!')];
|
||||
}
|
||||
|
||||
public function unsupportedMessagesProvider(): iterable
|
||||
{
|
||||
yield [new ChatMessage('Hello!')];
|
||||
yield [$this->createMock(MessageInterface::class)];
|
||||
}
|
||||
}
|
30
src/Symfony/Component/Notifier/Bridge/Octopush/composer.json
Normal file
30
src/Symfony/Component/Notifier/Bridge/Octopush/composer.json
Normal file
@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "symfony/octopush-notifier",
|
||||
"type": "symfony-bridge",
|
||||
"description": "Symfony Octopush Notifier Bridge",
|
||||
"keywords": ["sms", "octopush", "notifier"],
|
||||
"homepage": "https://symfony.com",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Aurélien Martin",
|
||||
"email": "pro@aurelienmartin.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "https://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.2.5",
|
||||
"symfony/http-client": "^4.3|^5.0",
|
||||
"symfony/notifier": "^5.3"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Octopush\\": "" },
|
||||
"exclude-from-classmap": [
|
||||
"/Tests/"
|
||||
]
|
||||
},
|
||||
"minimum-stability": "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 Octopush 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>
|
@ -92,6 +92,10 @@ class UnsupportedSchemeException extends LogicException
|
||||
'class' => Bridge\GatewayApi\GatewayApiTransportFactory::class,
|
||||
'package' => 'symfony/gatewayapi-notifier',
|
||||
],
|
||||
'octopush' => [
|
||||
'class' => Bridge\Octopush\OctopushTransportFactory::class,
|
||||
'package' => 'symfony/octopush-notifier',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
|
@ -21,6 +21,7 @@ use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
|
||||
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory;
|
||||
@ -66,6 +67,7 @@ class Transport
|
||||
SendinblueTransportFactory::class,
|
||||
DiscordTransportFactory::class,
|
||||
GatewayApiTransportFactory::class,
|
||||
OctopushTransportFactory::class,
|
||||
];
|
||||
|
||||
private $factories;
|
||||
|
Reference in New Issue
Block a user