Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 08366b0

Browse files
committed
[Notifier] Add Unifonic notifier bridge
1 parent 0af28a0 commit 08366b0

File tree

15 files changed

+438
-0
lines changed

15 files changed

+438
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2760,6 +2760,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
27602760
NotifierBridge\TurboSms\TurboSmsTransport::class => 'notifier.transport_factory.turbo-sms',
27612761
NotifierBridge\Twilio\TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
27622762
NotifierBridge\Twitter\TwitterTransportFactory::class => 'notifier.transport_factory.twitter',
2763+
NotifierBridge\Unifonic\UnifonicTransportFactory::class => 'notifier.transport_factory.unifonic',
27632764
NotifierBridge\Vonage\VonageTransportFactory::class => 'notifier.transport_factory.vonage',
27642765
NotifierBridge\Yunpian\YunpianTransportFactory::class => 'notifier.transport_factory.yunpian',
27652766
NotifierBridge\Zendesk\ZendeskTransportFactory::class => 'notifier.transport_factory.zendesk',

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
->parent('notifier.transport_factory.abstract')
6767
->tag('chatter.transport_factory')
6868

69+
->set('notifier.transport_factory.unifonic', Bridge\Unifonic\UnifonicTransportFactory::class)
70+
->parent('notifier.transport_factory.abstract')
71+
->tag('texter.transport_factory')
72+
6973
->set('notifier.transport_factory.all-my-sms', Bridge\AllMySms\AllMySmsTransportFactory::class)
7074
->parent('notifier.transport_factory.abstract')
7175
->tag('texter.transport_factory')
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+
7.1
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) 2023-present 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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Unifonic Notifier
2+
================
3+
4+
Provides [Unifonic](https://www.unifonic.com/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
UNIFONIC_DSN=unifonic://default?appSid={APP_SID}&from={SENDER}
11+
```
12+
13+
Resources
14+
---------
15+
16+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
17+
* [Report issues](https://github.com/symfony/symfony/issues) and
18+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
19+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Unifonic\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Unifonic\UnifonicTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
17+
final class UnifonicTransportFactoryTest extends TransportFactoryTestCase
18+
{
19+
public function createFactory(): UnifonicTransportFactory
20+
{
21+
return new UnifonicTransportFactory();
22+
}
23+
24+
public static function createProvider(): iterable
25+
{
26+
yield [
27+
'unifonic://host.test?from=Sender',
28+
'unifonic://host.test?from=Sender&appSid=s3cr3t',
29+
];
30+
yield [
31+
'unifonic://host.test',
32+
'unifonic://host.test?appSid=s3cr3t',
33+
];
34+
}
35+
36+
public static function supportsProvider(): iterable
37+
{
38+
yield [true, 'unifonic://host.test?from=Sender'];
39+
yield [true, 'unifonic://default?from=Sender'];
40+
yield [false, 'somethingElse://host.test?from=Sender'];
41+
}
42+
43+
public static function missingRequiredOptionProvider(): iterable
44+
{
45+
yield ['unifonic://host.test', 'The option "appSid" is required but missing.'];
46+
}
47+
48+
public static function unsupportedSchemeProvider(): iterable
49+
{
50+
yield ['somethingElse://host.test?from=Sender'];
51+
yield ['somethingElse://host.test?from=Sender&appSid=s3cr3t'];
52+
}
53+
54+
public static function incompleteDsnProvider(): iterable
55+
{
56+
yield ['unifonic://host.test', 'The option "appSid" is required but missing'];
57+
}
58+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Unifonic\Tests;
13+
14+
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\Notifier\Bridge\Unifonic\UnifonicTransport;
16+
use Symfony\Component\Notifier\Exception\TransportException;
17+
use Symfony\Component\Notifier\Message\ChatMessage;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Component\Notifier\Test\TransportTestCase;
20+
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
use Symfony\Contracts\HttpClient\ResponseInterface;
23+
24+
final class UnifonicTransportTest extends TransportTestCase
25+
{
26+
public static function createTransport(HttpClientInterface $client = null, string $host = null): UnifonicTransport
27+
{
28+
return (new UnifonicTransport('S3cr3t', 'Sender', $client ?? new MockHttpClient()))
29+
->setHost($host);
30+
}
31+
32+
public static function toStringProvider(): iterable
33+
{
34+
yield ['unifonic://el.cloud.unifonic.com?from=Sender', self::createTransport()];
35+
yield ['unifonic://api.unifonic.com?from=Sender', self::createTransport(host: 'api.unifonic.com')];
36+
}
37+
38+
public static function supportedMessagesProvider(): iterable
39+
{
40+
yield [new SmsMessage('0611223344', 'Hello!')];
41+
}
42+
43+
public static function unsupportedMessagesProvider(): iterable
44+
{
45+
yield [new ChatMessage('Hello!')];
46+
yield [new DummyMessage()];
47+
}
48+
49+
public function testSendFailedByStatusCode()
50+
{
51+
$response = $this->createMock(ResponseInterface::class);
52+
$response
53+
->method('getStatusCode')
54+
->willReturn(400)
55+
;
56+
57+
$client = new MockHttpClient(static fn (): ResponseInterface => $response);
58+
59+
$transport = self::createTransport($client);
60+
61+
$this->expectException(TransportException::class);
62+
$this->expectExceptionMessage('Unable to send SMS');
63+
64+
$transport->send(new SmsMessage('0611223344', 'Hello!'));
65+
}
66+
67+
public function testSendFailed()
68+
{
69+
$response = $this->createMock(ResponseInterface::class);
70+
$response
71+
->method('getStatusCode')
72+
->willReturn(200)
73+
;
74+
$response
75+
->expects(self::once())
76+
->method('getContent')
77+
->willReturn(json_encode([
78+
'success' => false,
79+
'errorCode' => 'ER-123',
80+
'message' => 'Lorem Ipsum',
81+
]))
82+
;
83+
84+
$client = new MockHttpClient(static fn (): ResponseInterface => $response);
85+
86+
$transport = self::createTransport($client);
87+
88+
$this->expectException(TransportException::class);
89+
$this->expectExceptionMessage('Unable to send the SMS. Reason: Lorem Ipsum. Error code: ER-123');
90+
91+
$transport->send(new SmsMessage('0611223344', 'Hello!'));
92+
}
93+
94+
public function testSendSuccess()
95+
{
96+
$response = $this->createMock(ResponseInterface::class);
97+
$response
98+
->method('getStatusCode')
99+
->willReturn(200)
100+
;
101+
$response
102+
->expects(self::once())
103+
->method('getContent')
104+
->willReturn(json_encode([
105+
'success' => true,
106+
]))
107+
;
108+
109+
$client = new MockHttpClient(static fn (): ResponseInterface => $response);
110+
111+
$transport = self::createTransport($client, host: 'localhost');
112+
$sentMessage = $transport->send(new SmsMessage('0611223344', 'Hello!'));
113+
114+
$this->assertSame('unifonic://localhost?from=Sender', $sentMessage->getTransport());
115+
$this->assertSame('Hello!', $sentMessage->getOriginalMessage()->getSubject());
116+
}
117+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Unifonic;
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\TransportExceptionInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @author Farhad Safarov <[email protected]>
26+
*/
27+
final class UnifonicTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'el.cloud.unifonic.com';
30+
31+
public function __construct(
32+
#[\SensitiveParameter]
33+
private readonly string $appSid,
34+
private readonly ?string $from = null,
35+
HttpClientInterface $client = null,
36+
EventDispatcherInterface $dispatcher = null,
37+
) {
38+
parent::__construct($client, $dispatcher);
39+
}
40+
41+
public function __toString(): string
42+
{
43+
return sprintf('unifonic://%s%s', $this->getEndpoint(), null !== $this->from ? '?from='.$this->from : '');
44+
}
45+
46+
public function supports(MessageInterface $message): bool
47+
{
48+
return $message instanceof SmsMessage;
49+
}
50+
51+
protected function doSend(MessageInterface $message): SentMessage
52+
{
53+
if (!$message instanceof SmsMessage) {
54+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
55+
}
56+
57+
$endpoint = sprintf('https://%s/rest/SMS/messages', $this->getEndpoint());
58+
59+
$body = [
60+
'AppSid' => $this->appSid,
61+
'Body' => $message->getSubject(),
62+
'Recipient' => $message->getPhone(),
63+
];
64+
65+
if ('' !== $message->getFrom()) {
66+
$body['SenderID'] = $message->getFrom();
67+
} elseif (null !== $this->from) {
68+
$body['SenderID'] = $this->from;
69+
}
70+
71+
$response = $this->client->request('POST', $endpoint, [
72+
'body' => $body,
73+
]);
74+
75+
try {
76+
$statusCode = $response->getStatusCode();
77+
} catch (TransportExceptionInterface $e) {
78+
throw new TransportException(sprintf('Could not reach "%s" endpoint.', $endpoint), $response, previous: $e);
79+
}
80+
81+
if (200 !== $statusCode) {
82+
throw new TransportException('Unable to send SMS.', $response);
83+
}
84+
85+
$content = $response->toArray(false);
86+
87+
if ('true' != $content['success']) {
88+
throw new TransportException(sprintf('Unable to send the SMS. Reason: "%s". Error code: "%s".', $content['message'], $content['errorCode']), $response);
89+
}
90+
91+
return new SentMessage($message, (string) $this);
92+
}
93+
}

0 commit comments

Comments
 (0)