Add Octopush notifier transport

This commit is contained in:
Aurélien MARTIN 2020-12-30 11:50:55 +01:00 committed by Oskar Stark
parent e55762dbdd
commit 93dde15dd2
14 changed files with 368 additions and 0 deletions

View File

@ -113,6 +113,7 @@ use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory; use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory; use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory; 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\OvhCloud\OvhCloudTransportFactory;
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory; use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory as SendinblueNotifierTransportFactory; use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory as SendinblueNotifierTransportFactory;
@ -2238,6 +2239,7 @@ class FrameworkExtension extends Extension
DiscordTransportFactory::class => 'notifier.transport_factory.discord', DiscordTransportFactory::class => 'notifier.transport_factory.discord',
LinkedInTransportFactory::class => 'notifier.transport_factory.linkedin', LinkedInTransportFactory::class => 'notifier.transport_factory.linkedin',
GatewayApiTransportFactory::class => 'notifier.transport_factory.gatewayapi', GatewayApiTransportFactory::class => 'notifier.transport_factory.gatewayapi',
OctopushTransportFactory::class => 'notifier.transport_factory.octopush',
]; ];
foreach ($classToServices as $class => $service) { foreach ($classToServices as $class => $service) {

View File

@ -23,6 +23,7 @@ use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory; use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory; use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory; 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\OvhCloud\OvhCloudTransportFactory;
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory; use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory; use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory;
@ -117,6 +118,10 @@ return static function (ContainerConfigurator $container) {
->parent('notifier.transport_factory.abstract') ->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory') ->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) ->set('notifier.transport_factory.discord', DiscordTransportFactory::class)
->parent('notifier.transport_factory.abstract') ->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory') ->tag('chatter.transport_factory')

View File

@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore

View File

@ -0,0 +1,7 @@
CHANGELOG
=========
5.3
---
* Add the bridge

View 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.

View File

@ -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;
}
}

View File

@ -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'];
}
}

View 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)

View File

@ -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
}
}

View File

@ -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)];
}
}

View 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"
}

View File

@ -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>

View File

@ -92,6 +92,10 @@ class UnsupportedSchemeException extends LogicException
'class' => Bridge\GatewayApi\GatewayApiTransportFactory::class, 'class' => Bridge\GatewayApi\GatewayApiTransportFactory::class,
'package' => 'symfony/gatewayapi-notifier', 'package' => 'symfony/gatewayapi-notifier',
], ],
'octopush' => [
'class' => Bridge\Octopush\OctopushTransportFactory::class,
'package' => 'symfony/octopush-notifier',
],
]; ];
/** /**

View File

@ -21,6 +21,7 @@ use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory; use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory; use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory; 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\OvhCloud\OvhCloudTransportFactory;
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory; use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory; use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory;
@ -66,6 +67,7 @@ class Transport
SendinblueTransportFactory::class, SendinblueTransportFactory::class,
DiscordTransportFactory::class, DiscordTransportFactory::class,
GatewayApiTransportFactory::class, GatewayApiTransportFactory::class,
OctopushTransportFactory::class,
]; ];
private $factories; private $factories;