Skip to content

Commit cc29772

Browse files
committed
feature #40691 [Notifier] Add SmsBiuras notifier bridge (StaffNowa)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [Notifier] Add SmsBiuras notifier bridge | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT | Doc PR | symfony/symfony-docs#15219 | Recipe PR | symfony/recipes/pull/932 Sms Biuras notifier bridge Commits ------- 79572b2 [Notifier] Add SmsBiuras notifier bridge
2 parents dd919a7 + 79572b2 commit cc29772

File tree

14 files changed

+401
-0
lines changed

14 files changed

+401
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
3939
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
4040
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
41+
use Symfony\Component\Notifier\Bridge\SmsBiuras\SmsBiurasTransportFactory;
4142
use Symfony\Component\Notifier\Bridge\SpotHit\SpotHitTransportFactory;
4243
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
4344
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
@@ -179,5 +180,9 @@
179180
->set('notifier.transport_factory.lightsms', LightSmsTransportFactory::class)
180181
->parent('notifier.transport_factory.abstract')
181182
->tag('texter.transport_factory')
183+
184+
->set('notifier.transport_factory.smsbiuras', SmsBiurasTransportFactory::class)
185+
->parent('notifier.transport_factory.abstract')
186+
->tag('texter.transport_factory')
182187
;
183188
};
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.3
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
SmsBiuras Notifier
2+
==================
3+
4+
Provides [SmsBiuras](https://www.smsbiuras.lt) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
SMSBIURAS_DSN=smsbiuras://UID:API_KEY@default?from=FROM&test_mode=0
11+
```
12+
13+
where:
14+
- `UID` is your client code
15+
- `API_KEY` is your SmsBiuras api key
16+
- `FROM` is your sender
17+
- `TEST_MODE` the test parameter is used during system connection testing.
18+
Possible values: 0 (real SMS sent), 1 (test SMS, will not be delivered to the phone and will not be charged)
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: 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 <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\SmsBiuras;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SentMessage;
19+
use Symfony\Component\Notifier\Message\SmsMessage;
20+
use Symfony\Component\Notifier\Transport\AbstractTransport;
21+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @author Vasilij Duško <vasilij@prado.lt>
26+
*/
27+
final class SmsBiurasTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'savitarna.smsbiuras.lt';
30+
31+
private $uid;
32+
private $apiKey;
33+
private $from;
34+
private $testMode;
35+
36+
private const ERROR_CODES = [
37+
1 => 'The message was processed and sent to the mobile operator. But delivery confirmations have not yet been returned.',
38+
2 => 'SMS not delivered.',
39+
3 => 'The SMS message was successfully delivered to the recipient.',
40+
4 => 'The message was sent and expired because it could not be delivered to the recipient during its validity period (48 hours according to our default platform).',
41+
5 => 'The message was received but the operator returned "Rejected" as the final status.',
42+
6 => 'Missing parameters, check that you are using all required parameters.',
43+
7 => ' Wrong apikey or uid.',
44+
8 => 'Sender ID - "from". Must be approved by an administrator.',
45+
9 => 'Balance insufficient, please top up the account.',
46+
10 => 'Bad date format for schedule parameter. Ex: urlencode("2021-03-11 12:00").',
47+
999 => 'Unknown Error',
48+
];
49+
50+
public function __construct(string $uid, string $apiKey, string $from, bool $testMode, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
51+
{
52+
$this->uid = $uid;
53+
$this->apiKey = $apiKey;
54+
$this->from = $from;
55+
$this->testMode = $testMode;
56+
57+
parent::__construct($client, $dispatcher);
58+
}
59+
60+
public function __toString(): string
61+
{
62+
if ($this->testMode) {
63+
return sprintf('smsbiuras://%s?from=%s&test_mode=%s', $this->getEndpoint(), $this->from, $this->testMode);
64+
}
65+
66+
return sprintf('smsbiuras://%s?from=%s', $this->getEndpoint(), $this->from);
67+
}
68+
69+
public function supports(MessageInterface $message): bool
70+
{
71+
return $message instanceof SmsMessage;
72+
}
73+
74+
protected function doSend(MessageInterface $message): SentMessage
75+
{
76+
if (!$message instanceof SmsMessage) {
77+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
78+
}
79+
80+
$endpoint = sprintf('https://%s/api?', $this->getEndpoint());
81+
82+
$response = $this->client->request('GET', $endpoint, [
83+
'query' => [
84+
'uid' => $this->uid,
85+
'apikey' => $this->apiKey,
86+
'message' => $message->getSubject(),
87+
'from' => $this->from,
88+
'test' => $this->testMode ? 0 : 1,
89+
'to' => $message->getPhone(),
90+
],
91+
]);
92+
93+
if (Response::HTTP_OK !== $response->getStatusCode()) {
94+
throw new TransportException('Unable to send the SMS.', $response);
95+
}
96+
97+
$matches = [];
98+
if (preg_match('/^ERROR: (\d+)$/', $response->getContent(), $matches)) {
99+
throw new TransportException('Unable to send the SMS: '.$this->getErrorMsg($matches[1] ?? 999), $response);
100+
}
101+
102+
$matches = [];
103+
if (preg_match('/^OK: (\d+)$/', $response->getContent(), $matches)) {
104+
$sentMessage = new SentMessage($message, (string) $this);
105+
$sentMessage->setMessageId($matches[1] ?? 0);
106+
107+
return $sentMessage;
108+
}
109+
110+
throw new TransportException('Unable to send the SMS.', $response);
111+
}
112+
113+
private function getErrorMsg(int $errorCode): string
114+
{
115+
return self::ERROR_CODES[$errorCode] ?? self::ERROR_CODES[999];
116+
}
117+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\SmsBiuras;
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 Vasilij Duško <vasilij@prado.lt>
21+
*/
22+
final class SmsBiurasTransportFactory extends AbstractTransportFactory
23+
{
24+
/**
25+
* @return SmsBiurasTransport
26+
*/
27+
public function create(Dsn $dsn): TransportInterface
28+
{
29+
$scheme = $dsn->getScheme();
30+
31+
if ('smsbiuras' !== $scheme) {
32+
throw new UnsupportedSchemeException($dsn, 'smsbiuras', $this->getSupportedSchemes());
33+
}
34+
35+
$uid = $this->getUser($dsn);
36+
$apiKey = $this->getPassword($dsn);
37+
$from = $dsn->getRequiredOption('from');
38+
$testMode = filter_var($dsn->getOption('test_mode', false), \FILTER_VALIDATE_BOOLEAN);
39+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
40+
$port = $dsn->getPort();
41+
42+
return (new SmsBiurasTransport($uid, $apiKey, $from, $testMode, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
43+
}
44+
45+
protected function getSupportedSchemes(): array
46+
{
47+
return ['smsbiuras'];
48+
}
49+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\SmsBiuras\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\SmsBiuras\SmsBiurasTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
17+
18+
final class SmsBiurasTransportFactoryTest extends TransportFactoryTestCase
19+
{
20+
/**
21+
* @return SmsBiurasTransportFactory
22+
*/
23+
public function createFactory(): TransportFactoryInterface
24+
{
25+
return new SmsBiurasTransportFactory();
26+
}
27+
28+
public function createProvider(): iterable
29+
{
30+
yield [
31+
'smsbiuras://host.test?from=0611223344',
32+
'smsbiuras://uid:api_key@host.test?from=0611223344&test_mode=0',
33+
];
34+
35+
yield [
36+
'smsbiuras://host.test?from=0611223344&test_mode=1',
37+
'smsbiuras://uid:api_key@host.test?from=0611223344&test_mode=1',
38+
];
39+
}
40+
41+
public function supportsProvider(): iterable
42+
{
43+
yield [true, 'smsbiuras://uid:api_key@default?from=0611223344'];
44+
yield [false, 'somethingElse://uid:api_key@default?from=0611223344'];
45+
}
46+
47+
public function missingRequiredOptionProvider(): iterable
48+
{
49+
yield 'missing option: from' => ['smsbiuras://uid:api_key@default'];
50+
}
51+
52+
public function unsupportedSchemeProvider(): iterable
53+
{
54+
yield ['somethingElse://uid:api_key@default?from=0611223344'];
55+
yield ['somethingElse://uid:api_key@default']; // missing "from" option
56+
}
57+
}
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\SmsBiuras\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\SmsBiuras\SmsBiurasTransport;
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 SmsBiurasTransportTest extends TransportTestCase
23+
{
24+
/**
25+
* @return SmsBiurasTransport
26+
*/
27+
public function createTransport(?HttpClientInterface $client = null): TransportInterface
28+
{
29+
return new SmsBiurasTransport('uid', 'api_key', 'from', true, $client ?? $this->createMock(HttpClientInterface::class));
30+
}
31+
32+
public function toStringProvider(): iterable
33+
{
34+
yield ['smsbiuras://savitarna.smsbiuras.lt?from=from&test_mode=1', $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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy