Skip to content

Commit f9f81de

Browse files
committed
Add Yunpian Notifier Bridge
1 parent 5010ebd commit f9f81de

File tree

16 files changed

+344
-0
lines changed

16 files changed

+344
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
145145
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
146146
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
147+
use Symfony\Component\Notifier\Bridge\Yunpian\YunpianTransportFactory;
147148
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
148149
use Symfony\Component\Notifier\Notifier;
149150
use Symfony\Component\Notifier\Recipient\Recipient;
@@ -2453,6 +2454,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24532454
TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
24542455
TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',
24552456
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
2457+
YunpianTransportFactory::class => 'notifier.transport_factory.yunpian',
24562458
ZulipTransportFactory::class => 'notifier.transport_factory.zulip',
24572459
];
24582460

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
4747
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
4848
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
49+
use Symfony\Component\Notifier\Bridge\Yunpian\YunpianTransportFactory;
4950
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
5051
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
5152
use Symfony\Component\Notifier\Transport\NullTransportFactory;
@@ -204,5 +205,9 @@
204205
->set('notifier.transport_factory.mailjet', MailjetTransportFactory::class)
205206
->parent('notifier.transport_factory.abstract')
206207
->tag('texter.transport_factory')
208+
209+
->set('notifier.transport_factory.yunpian', YunpianTransportFactory::class)
210+
->parent('notifier.transport_factory.abstract')
211+
->tag('texter.transport_factory')
207212
;
208213
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.4
5+
---
6+
7+
* Add the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Yunpian Notifier
2+
===============
3+
4+
Provides [Yunpian](https://www.yunpian.com) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
YUNPIAN_DSN=yunpian://APIKEY@default
11+
```
12+
13+
where:
14+
- `APIKEY` is your Yunpian API key
15+
16+
Resources
17+
---------
18+
19+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
20+
* [Report issues](https://github.com/symfony/symfony/issues) and
21+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
22+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Yunpian\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Yunpian\YunpianTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
17+
18+
final class YunpianTransportFactoryTest extends TransportFactoryTestCase
19+
{
20+
/**
21+
* @return YunpianTransportFactory
22+
*/
23+
public function createFactory(): TransportFactoryInterface
24+
{
25+
return new YunpianTransportFactory();
26+
}
27+
28+
public function createProvider(): iterable
29+
{
30+
yield [
31+
'yunpian://host.test',
32+
'yunpian://api_key@host.test',
33+
];
34+
}
35+
36+
public function supportsProvider(): iterable
37+
{
38+
yield [true, 'yunpian://api_key@default'];
39+
yield [false, 'somethingElse://api_key@default'];
40+
}
41+
42+
public function unsupportedSchemeProvider(): iterable
43+
{
44+
yield ['somethingElse://api_key@default'];
45+
}
46+
}
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\Yunpian\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Yunpian\YunpianTransport;
15+
use Symfony\Component\Notifier\Message\ChatMessage;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SmsMessage;
18+
use Symfony\Component\Notifier\Test\TransportTestCase;
19+
use Symfony\Component\Notifier\Transport\TransportInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
final class YunpianTransportTest extends TransportTestCase
23+
{
24+
/**
25+
* @return YunpianTransport
26+
*/
27+
public function createTransport(HttpClientInterface $client = null): TransportInterface
28+
{
29+
return new YunpianTransport('api_key', $client ?? $this->createMock(HttpClientInterface::class));
30+
}
31+
32+
public function toStringProvider(): iterable
33+
{
34+
yield ['yunpian://sms.yunpian.com', $this->createTransport()];
35+
}
36+
37+
public function supportedMessagesProvider(): iterable
38+
{
39+
yield [new SmsMessage('+0611223344', 'Hello!')];
40+
}
41+
42+
public function unsupportedMessagesProvider(): iterable
43+
{
44+
yield [new ChatMessage('Hello!')];
45+
yield [$this->createMock(MessageInterface::class)];
46+
}
47+
}
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\Yunpian;
13+
14+
use Symfony\Component\Notifier\Exception\TransportException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
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\Exception\ExceptionInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @author Mathieu Santostefano <msantostefano@protonmail.com>
26+
*/
27+
class YunpianTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'sms.yunpian.com';
30+
31+
private $apiKey;
32+
33+
public function __construct(string $apiKey, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
34+
{
35+
$this->apiKey = $apiKey;
36+
37+
parent::__construct($client, $dispatcher);
38+
}
39+
40+
public function __toString(): string
41+
{
42+
return sprintf('yunpian://%s', $this->getEndpoint());
43+
}
44+
45+
public function supports(MessageInterface $message): bool
46+
{
47+
return $message instanceof SmsMessage;
48+
}
49+
50+
protected function doSend(MessageInterface $message): SentMessage
51+
{
52+
if (!$message instanceof SmsMessage) {
53+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
54+
}
55+
56+
$endpoint = sprintf('https://%s/v2/sms/single_send.json', self::HOST);
57+
$response = $this->client->request('POST', $endpoint, [
58+
'body' => [
59+
'apikey' => $this->apiKey,
60+
'mobile' => $message->getPhone(),
61+
'text' => $message->getSubject(),
62+
],
63+
]);
64+
65+
try {
66+
$data = $response->toArray(false);
67+
} catch (ExceptionInterface $exception) {
68+
throw new TransportException('Unable to send the SMS.', $response);
69+
}
70+
71+
if (isset($data['code']) && 0 !== (int) $data['code']) {
72+
throw new TransportException(sprintf('Unable to send SMS: "Code: "%s". Message: "%s"".', $data['code'], $data['msg'] ?? 'Unknown reason'), $response);
73+
}
74+
75+
return new SentMessage($message, (string) $this);
76+
}
77+
}

0 commit comments

Comments
 (0)