8000 feature #34540 [Notifier] add OvhCloud bridge (antiseptikk) · symfony/symfony@e0a6a73 · GitHub
[go: up one dir, main page]

Skip to content

Commit e0a6a73

Browse files
committed
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
2 parents 8c694d6 + 76bfb85 commit e0a6a73

File tree

12 files changed

+290
-0
lines changed

12 files changed

+290
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
9494
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
9595
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
96+
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
9697
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
9798
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
9899
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
@@ -2005,6 +2006,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
20052006
RocketChatTransportFactory::class => 'notifier.transport_factory.rocketchat',
20062007
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
20072008
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',
2009+
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
20082010
];
20092011

20102012
foreach ($classToServices as $class => $service) {

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
<tag name="texter.transport_factory" />
3939
</service>
4040

41+
<service id="notifier.transport_factory.ovhcloud" class="Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory" parent="notifier.transport_factory.abstract">
42+
<tag name="texter.transport_factory" />
43+
</service>
44+
4145
<service id="notifier.transport_factory.null" class="Symfony\Component\Notifier\Transport\NullTransportFactory" parent="notifier.transport_factory.abstract">
4246
<tag name="notifier.transport_factory" />
4347
</service>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitignore export-ignore
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.1.0
5+
-----
6+
7+
* Added the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\OvhCloud;
13+
14+
use Symfony\Component\Notifier\Exception\LogicException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SmsMessage;
18+
use Symfony\Component\Notifier\Transport\AbstractTransport;
19+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
/**
23+
* @author Thomas Ferney <thomas.ferney@gmail.com>
24+
*
25+
* @experimental in 5.1
26+
*/
27+
final class OvhCloudTransport extends AbstractTransport
28+
{
29+
private $endpoints = [
30+
'ovh-eu' => 'https://eu.api.ovh.com/1.0',
31+
'ovh-ca' => 'https://ca.api.ovh.com/1.0',
32+
'ovh-us' => 'https://api.us.ovhcloud.com/1.0',
33+
'kimsufi-eu' => 'https://eu.api.kimsufi.com/1.0',
34+
'kimsufi-ca' => 'https://ca.api.kimsufi.com/1.0',
35+
'soyoustart-eu' => 'https://eu.api.soyoustart.com/1.0',
36+
'soyoustart-ca' => 'https://ca.api.soyoustart.com/1.0',
37+
'runabove-ca' => 'https://api.runabove.com/1.0',
38+
];
39+
40+
private $applicationKey;
41+
private $applicationSecret;
42+
private $consumerKey;
43+
private $serviceName;
44+
private $timeDelta;
45+
46+
public function __construct(string $applicationKey, string $applicationSecret, string $consumerKey, string $serviceName, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
47+
{
48+
$this->applicationKey = $applicationKey;
49+
$this->applicationSecret = $applicationSecret;
50+
$this->consumerKey = $consumerKey;
51+
$this->serviceName = $serviceName;
52+
53+
parent::__construct($client, $dispatcher);
54+
}
55+
56+
public function setEndpointName(?string $endpoint): self
57+
{
58+
$this->host = $this->endpoints[$endpoint] ?: self::HOST;
59+
60+
return $this;
61+
}
62+
63+
public function __toString(): string
64+
{
65+
return sprintf('ovhcloud://%s?consumer_key=%s&service_name=%s', $this->getEndpoint(), $this->consumerKey, $this->serviceName);
66+
}
67+
68+
public function supports(MessageInterface $message): bool
69+
{
70+
return $message instanceof SmsMessage;
71+
}
72+
73+
protected function doSend(MessageInterface $message): void
74+
{
75+
if (!$message instanceof SmsMessage) {
76+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, \get_class($message)));
77+
}
78+
79+
$endpoint = sprintf('%s/sms/%s/jobs', $this->getEndpoint(), $this->serviceName);
80+
81+
$content = [
82+
'charset' => 'UTF-8',
83+
'class' => 'flash',
84+
'coding' => '8bit',
85+
'message' => $message->getSubject(),
86+
'receivers' => [$message->getPhone()],
87+
'noStopClause' => false,
88+
'priority' => 'medium',
89+
'senderForResponse' => true,
90+
];
91+
92+
$now = time() + $this->calculateTimeDelta();
93+
$headers['X-Ovh-Application'] = $this->applicationKey;
94+
$headers['X-Ovh-Timestamp'] = $now;
95+
96+
$toSign = $this->applicationSecret.'+'.$this->consumerKey.'+POST+'.$endpoint.'+'.json_encode($content, JSON_UNESCAPED_SLASHES).'+'.$now;
97+
$headers['X-Ovh-Consumer'] = $this->consumerKey;
98+
$headers['X-Ovh-Signature'] = '$1$'.sha1($toSign);
99+
100+
$response = $this->client->request('POST', $endpoint, [
101+
'headers' => $headers,
102+
'json' => $content,
103+
]);
104+
105+
if (200 !== $response->getStatusCode()) {
106+
$error = $response->toArray(false);
107+
108+
throw new TransportException(sprintf('Unable to send the SMS: %s.', $error['message']), $response);
109+
}
110+
}
111+
112+
/**
113+
* Calculate time delta between local machine and API's server.
114+
*/
115+
private function calculateTimeDelta(): int
116+
{
117+
$endpoint = sprintf('%s/auth/time', $this->getEndpoint());
118+
$response = $this->client->request('GET', $endpoint);
119+
120+
$serverTimestamp = (int) (string) $response->getContent();
121+
122+
return $serverTimestamp - (int) time();
123+
}
124+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\OvhCloud;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @author Thomas Ferney <thomas.ferney@gmail.com>
21+
*
22+
* @experimental in 5.1
23+
*/
24+
final class OvhCloudTransportFactory extends AbstractTransportFactory
25+
{
26+
public function create(Dsn $dsn): TransportInterface
27+
{
28+
$scheme = $dsn->getScheme();
29+
$applicationKey = $this->getUser($dsn);
30+
$applicationSecret = $this->getPassword($dsn);
31+
$consumerKey = $dsn->getOption('consumer_key');
32+
$serviceName = $dsn->getOption('service_name');
33+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
34+
$port = $dsn->getPort();
35+
36+
if ('ovhcloud' === $scheme) {
37+
return (new OvhCloudTransport($applicationKey, $applicationSecret, $consumerKey, $serviceName, $this->client, $this->dispatcher))->setEndpointName($host)->setPort($port);
38+
}
39+
40+
throw new UnsupportedSchemeException($dsn, 'ovhcloud', $this->getSupportedSchemes());
41+
}
42+
43+
protected function getSupportedSchemes(): array
44+
{
45+
return ['ovhcloud'];
46+
}
47+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
OvhCloud Notifier
2+
=================
3+
4+
Provides OvhCloud integration for Symfony Notifier.
5+
6+
Resources
7+
---------
8+
9+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
10+
* [Report issues](https://github.com/symfony/symfony/issues) and
11+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
12+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "symfony/ovhcloud-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony OvhCloud Notifier Bridge",
5+
"keywords": ["sms", "OvhCloud", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Thomas Ferney",
11+
"email": "thomas.ferney@gmail.com"
12+
},
13+
{
14+
"name": "Symfony Community",
15+
"homepage": "https://symfony.com/contributors"
16+
}
17+
],
18+
"require": {
19+
"php": "^7.2.5",
20+
"symfony/http-client": "^4.3|^5.0",
21+
"symfony/notifier": "^5.0"
22+
},
23+
"autoload": {
24+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\OvhCloud\\": "" },
25+
"exclude-from-classmap": [
26+
"/Tests/"
27+
]
28+
},
29+
"minimum-stability": "dev",
30+
"extra": {
31+
"branch-alias": {
32+
"dev-master": "5.1-dev"
33+
}
34+
}
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
failOnRisky="true"
9+
failOnWarning="true"
10+
>
11+
<php>
12+
<ini name="error_reporting" value="-1" />
13+
</php>
14+
15+
<testsuites>
16+
<testsuite name="Symfony OvhCloud Notifier Bridge Test Suite">
17+
<directory>./Tests/</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<filter>
22+
<whitelist>
23+
<directory>./</directory>
24+
<exclude>
25+
<directory>./Resources</directory>
26+
<directory>./Tests</directory>
27+
<directory>./vendor</directory>
28+
</exclude>
29+
</whitelist>
30+
</filter>
31+
</phpunit>

src/Symfony/Component/Notifier/Exception/UnsupportedSchemeException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ class UnsupportedSchemeException extends LogicException
5050
'class' => Bridge\Firebase\FirebaseTransportFactory::class,
5151
'package' => 'symfony/firebase-notifier',
5252
],
53+
'ovhcloud' => [
54+
'class' => Bridge\OvhCloud\OvhCloudTransportFactory::class,
55+
'package' => 'symfony/ovhcloud-notifier',
56+
],
5357
];
5458

5559
/**

src/Symfony/Component/Notifier/Transport.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
1515
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
1616
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
17+
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
1718
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
1819 6D06
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
1920
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
@@ -43,6 +44,7 @@ class Transport
4344
NexmoTransportFactory::class,
4445
RocketChatTransportFactory::class,
4546
TwilioTransportFactory::class,
47+
OvhCloudTransportFactory::class,
4648
FirebaseTransportFactory::class,
4749
];
4850

0 commit comments

Comments
 (0)
0