8000 feature #36940 [Notifier] add support for smsapi-notifier (szepczynski) · jeremyFreeAgent/symfony@e6cdba6 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit e6cdba6

Browse files
committed
feature symfony#36940 [Notifier] add support for smsapi-notifier (szepczynski)
This PR was squashed before being merged into the 5.2-dev branch. Discussion ---------- [Notifier] add support for smsapi-notifier | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT | Doc PR | symfony/symfony-docs#13716 Hi, I've created integration to notifier to support polish sms operator - smsapi.pl Here is smsapi-notifier integration: https://github.com/szepczynski/smsapi-notifier. Can you grab this code and make as symfony/smsapi-notifier? This PR includes changes in notifier and framework-bundle to support smsapi transport as well as other included in notifier component. Could someone integrate this into notifier component? Commits ------- 91c2530 [Notifier] add support for smsapi-notifier
2 parents 14c9d05 + 91c2530 commit e6cdba6

File tree

11 files changed

+253
-0
lines changed

11 files changed

+253
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
107107
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
108108
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
109+
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
109110
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
110111
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
111112
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
@@ -2108,6 +2109,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
21082109
SinchTransportFactory::class => 'notifier.transport_factory.sinch',
21092110
ZulipTransportFactory::class => 'notifier.transport_factory.zulip',
21102111
MobytTransportFactory::class => 'notifier.transport_factory.mobyt',
2112+
SmsapiTransportFactory::class => 'notifier.transport_factory.smsapi',
21112113
];
21122114

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
2424
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
2525
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
26+
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
2627
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
2728
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
2829
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
@@ -95,6 +96,10 @@
9596
->parent('notifier.transport_factory.abstract')
9697
->tag('texter.transport_factory')
9798

99+
->set('notifier.transport_factory.smsapi', SmsapiTransportFactory::class)
100+
->parent('notifier.transport_factory.abstract')
101+
->tag('texter.transport_factory')
102+
98103
->set('notifier.transport_factory.null', NullTransportFactory::class)
99104
->parent('notifier.transport_factory.abstract')
100105
->tag('chatter.transport_factory')
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020 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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
SMSAPI Notifier
2+
===============
3+
4+
Provides Smsapi integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
// .env file
11+
SMSAPI_DSN=smsapi://TOKEN@default?from=FROM
12+
```
13+
14+
where:
15+
- `TOKEN` is API Token (OAuth)
16+
- `FROM` is sender name
17+
18+
See your account info at https://ssl.smsapi.pl/
19+
20+
Resources
21+
---------
22+
23+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
24+
* [Report issues](https://github.com/symfony/symfony/issues) and
25+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
26+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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\Smsapi;
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\SentMessage;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Component\Notifier\Transport\AbstractTransport;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author Marcin Szepczynski <szepczynski@gmail.com>
25+
* @experimental in 5.2
26+
*/
27+
final class SmsapiTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'api.smsapi.pl';
30+
31+
private $authToken;
32+
private $from;
33+
34+
public function __construct(string $authToken, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
35+
{
36+
$this->authToken = $authToken;
37+
$this->from = $from;
38+
39+
parent::__construct($client, $dispatcher);
40+
}
41+
42+
public function __toString(): string
43+
{
44+
return sprintf('smsapi://%s?from=%s', $this->getEndpoint(), $this->from);
45+
}
46+
47+
public function supports(MessageInterface $message): bool
48+
{
49+
return $message instanceof SmsMessage;
50+
}
51+
52+
protected function doSend(MessageInterface $message): SentMessage
53+
{
54+
if (!$message instanceof SmsMessage) {
55+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, get_debug_type($message)));
56+
}
57+
58+
$endpoint = sprintf('https://%s/sms.do', $this->getEndpoint());
59+
$response = $this->client->request('POST', $endpoint, [
60+
'auth_bearer' => $this->authTo 4F6 ken,
61+
'body' => [
62+
'from' => $this->from,
63+
'to' => $message->getPhone(),
64+
'message' => $message->getSubject(),
65+
'format' => 'json',
66+
],
67+
]);
68+
69+
if (200 !== $response->getStatusCode()) {
70+
$error = $response->toArray(false);
71+
72+
throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error['message']), $response);
73+
}
74+
75+
return new SentMessage($message, (string) $this);
76+
}
77+
}
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\Smsapi;
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 Marcin Szepczynski <szepczynski@gmail.com>
21+
* @experimental in 5.2
22+
*/
23+
class SmsapiTransportFactory extends AbstractTransportFactory
24+
{
25+
/**
26+
* @return SmsapiTransport
27+
*/
28+
public function create(Dsn $dsn): TransportInterface
29+
{
30+
$scheme = $dsn->getScheme();
31+
$authToken = $dsn->getUser();
32+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
33+
$from = $dsn->getOption('from');
34+
$port = $dsn->getPort();
35+
36+
if ('smsapi' === $scheme) {
37+
return (new SmsapiTransport($authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
38+
}
39+
40+
throw new UnsupportedSchemeException($dsn, 'smsapi', $this->getSupportedSchemes());
41+
}
42+
43+
protected function getSupportedSchemes(): array
44+
{
45+
return ['smsapi'];
46+
}
47+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "symfony/smsapi-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony Smsapi Notifier Bridge",
5+
"keywords": ["sms", "smsapi", "notifier"],
6+
"homepage": "https://mvpdoers.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Marcin Szepczynski",
11+
"email": "szepczynski@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.2"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Symfony\\Component\\Notifier\\Bridge\\Smsapi\\": ""
26+
},
27+
"exclude-from-classmap": [
28+
"/Tests/"
29+
]
30+
},
31+
"minimum-stability": "dev",
32+
"extra": {
33+
"branch-alias": {
34+
"dev-master": "5.2-dev"
35+
}
36+
}
37+
}
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 Smsapi Notifier Bridge Test Suite">
17+
<directory>./Tests/</directory>
18+
</testsuite>
19+
</testsuites> E377 ;
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
@@ -74,6 +74,10 @@ class UnsupportedSchemeException extends LogicException
7474
'class' => Bridge\Zulip\ZulipTransportFactory::class,
7575
'package' => 'symfony/zulip-notifier',
7676
],
77+
'smsapi' => [
78+
'class' => Bridge\Smsapi\SmsapiTransportFactory::class,
79+
'package' => 'symfony/smsapi-notifier',
80+
],
7781
];
7882

7983
/**

src/Symfony/Component/Notifier/Transport.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
2222
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
2323
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
24+
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
2425
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
2526
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
2627
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
@@ -56,6 +57,7 @@ class Transport
5657
FreeMobileTransportFactory::class,
5758
ZulipTransportFactory::class,
5859
MobytTransportFactory::class,
60+
SmsapiTransportFactory::class,
5961
];
6062

6163
private $factories;

0 commit comments

Comments
 (0)
0