Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #34540 [Notifier] add OvhCloud bridge (antiseptikk)
This PR was squashed before being merged into the 5.1-dev branch. Discussion ---------- [Notifier] add OvhCloud bridge | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | See #33687 | License | MIT This would add OvhCloud sms integration for the Notifier component. Tested with 'ovh-eu' entrypoint. Inspiration : https://github.com/ovh/php-ovh Commits ------- 76bfb85 [Notifier] add OvhCloud bridge
- Loading branch information
Showing
12 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/Symfony/Component/Notifier/Bridge/OvhCloud/.gitattributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitignore export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.1.0 | ||
----- | ||
|
||
* Added the bridge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2019 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. |
124 changes: 124 additions & 0 deletions
124
src/Symfony/Component/Notifier/Bridge/OvhCloud/OvhCloudTransport.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?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\OvhCloud; | ||
|
||
use Symfony\Component\Notifier\Exception\LogicException; | ||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Transport\AbstractTransport; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author Thomas Ferney <thomas.ferney@gmail.com> | ||
* | ||
* @experimental in 5.1 | ||
*/ | ||
final class OvhCloudTransport extends AbstractTransport | ||
{ | ||
private $endpoints = [ | ||
'ovh-eu' => 'https://eu.api.ovh.com/1.0', | ||
'ovh-ca' => 'https://ca.api.ovh.com/1.0', | ||
'ovh-us' => 'https://api.us.ovhcloud.com/1.0', | ||
'kimsufi-eu' => 'https://eu.api.kimsufi.com/1.0', | ||
'kimsufi-ca' => 'https://ca.api.kimsufi.com/1.0', | ||
'soyoustart-eu' => 'https://eu.api.soyoustart.com/1.0', | ||
'soyoustart-ca' => 'https://ca.api.soyoustart.com/1.0', | ||
'runabove-ca' => 'https://api.runabove.com/1.0', | ||
]; | ||
|
||
private $applicationKey; | ||
private $applicationSecret; | ||
private $consumerKey; | ||
private $serviceName; | ||
private $timeDelta; | ||
|
||
public function __construct(string $applicationKey, string $applicationSecret, string $consumerKey, string $serviceName, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->applicationKey = $applicationKey; | ||
$this->applicationSecret = $applicationSecret; | ||
$this->consumerKey = $consumerKey; | ||
$this->serviceName = $serviceName; | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function setEndpointName(?string $endpoint): self | ||
{ | ||
$this->host = $this->endpoints[$endpoint] ?: self::HOST; | ||
|
||
return $this; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('ovhcloud://%s?consumer_key=%s&service_name=%s', $this->getEndpoint(), $this->consumerKey, $this->serviceName); | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return $message instanceof SmsMessage; | ||
} | ||
|
||
protected function doSend(MessageInterface $message): void | ||
{ | ||
if (!$message instanceof SmsMessage) { | ||
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, \get_class($message))); | ||
} | ||
|
||
$endpoint = sprintf('%s/sms/%s/jobs', $this->getEndpoint(), $this->serviceName); | ||
|
||
$content = [ | ||
'charset' => 'UTF-8', | ||
'class' => 'flash', | ||
'coding' => '8bit', | ||
'message' => $message->getSubject(), | ||
'receivers' => [$message->getPhone()], | ||
'noStopClause' => false, | ||
'priority' => 'medium', | ||
'senderForResponse' => true, | ||
]; | ||
|
||
$now = time() + $this->calculateTimeDelta(); | ||
$headers['X-Ovh-Application'] = $this->applicationKey; | ||
$headers['X-Ovh-Timestamp'] = $now; | ||
|
||
$toSign = $this->applicationSecret.'+'.$this->consumerKey.'+POST+'.$endpoint.'+'.json_encode($content, JSON_UNESCAPED_SLASHES).'+'.$now; | ||
$headers['X-Ovh-Consumer'] = $this->consumerKey; | ||
$headers['X-Ovh-Signature'] = '$1$'.sha1($toSign); | ||
|
||
$response = $this->client->request('POST', $endpoint, [ | ||
'headers' => $headers, | ||
'json' => $content, | ||
]); | ||
|
||
if (200 !== $response->getStatusCode()) { | ||
$error = $response->toArray(false); | ||
|
||
throw new TransportException(sprintf('Unable to send the SMS: %s.', $error['message']), $response); | ||
} | ||
} | ||
|
||
/** | ||
* Calculate time delta between local machine and API's server. | ||
*/ | ||
private function calculateTimeDelta(): int | ||
{ | ||
$endpoint = sprintf('%s/auth/time', $this->getEndpoint()); | ||
$response = $this->client->request('GET', $endpoint); | ||
|
||
$serverTimestamp = (int) (string) $response->getContent(); | ||
|
||
return $serverTimestamp - (int) time(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/Notifier/Bridge/OvhCloud/OvhCloudTransportFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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\OvhCloud; | ||
|
||
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 Thomas Ferney <thomas.ferney@gmail.com> | ||
* | ||
* @experimental in 5.1 | ||
*/ | ||
final class OvhCloudTransportFactory extends AbstractTransportFactory | ||
{ | ||
public function create(Dsn $dsn): TransportInterface | ||
{ | ||
$scheme = $dsn->getScheme(); | ||
$applicationKey = $this->getUser($dsn); | ||
$applicationSecret = $this->getPassword($dsn); | ||
$consumerKey = $dsn->getOption('consumer_key'); | ||
$serviceName = $dsn->getOption('service_name'); | ||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
$port = $dsn->getPort(); | ||
|
||
if ('ovhcloud' === $scheme) { | ||
return (new OvhCloudTransport($applicationKey, $applicationSecret, $consumerKey, $serviceName, $this->client, $this->dispatcher))->setEndpointName($host)->setPort($port); | ||
} | ||
|
||
throw new UnsupportedSchemeException($dsn, 'ovhcloud', $this->getSupportedSchemes()); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['ovhcloud']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
OvhCloud Notifier | ||
================= | ||
|
||
Provides OvhCloud integration for Symfony Notifier. | ||
|
||
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) |
35 changes: 35 additions & 0 deletions
35
src/Symfony/Component/Notifier/Bridge/OvhCloud/composer.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "symfony/ovhcloud-notifier", | ||
"type": "symfony-bridge", | ||
"description": "Symfony OvhCloud Notifier Bridge", | ||
"keywords": ["sms", "OvhCloud", "notifier"], | ||
"homepage": "https://symfony.com", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Thomas Ferney", | ||
"email": "thomas.ferney@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.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\OvhCloud\\": "" }, | ||
"exclude-from-classmap": [ | ||
"/Tests/" | ||
] | ||
}, | ||
"minimum-stability": "dev", | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "5.1-dev" | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/Notifier/Bridge/OvhCloud/phpunit.xml.dist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 OvhCloud 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters