Skip to content

Commit edfba7a

Browse files
committed
feature #52976 [Notifier] Add sms-sluzba.cz bridge (dfridrich)
This PR was merged into the 7.1 branch. Discussion ---------- [Notifier] Add sms-sluzba.cz bridge | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #52975 | License | MIT Adding support for [sms-sluzba.cz](https://www.sms-sluzba.cz/) as I mention in * #52975 Commits ------- 1b4e01e Add sms-sluzba.cz Notifier Bridge
2 parents ce22a97 + 1b4e01e commit edfba7a

File tree

16 files changed

+404
-0
lines changed

16 files changed

+404
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2754,6 +2754,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
27542754
NotifierBridge\Smsc\SmscTransportFactory::class => 'notifier.transport_factory.smsc',
27552755
NotifierBridge\SmsFactor\SmsFactorTransportFactory::class => 'notifier.transport_factory.sms-factor',
27562756
NotifierBridge\Smsmode\SmsmodeTransportFactory::class => 'notifier.transport_factory.smsmode',
2757+
NotifierBridge\SmsSluzba\SmsSluzbaTransportFactory::class => 'notifier.transport_factory.sms-sluzba',
27572758
NotifierBridge\SpotHit\SpotHitTransportFactory::class => 'notifier.transport_factory.spot-hit',
27582759
NotifierBridge\Telegram\TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
27592760
NotifierBridge\Telnyx\TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
'light-sms' => Bridge\LightSms\LightSmsTransportFactory::class,
108108
'sms-biuras' => Bridge\SmsBiuras\SmsBiurasTransportFactory::class,
109109
'smsbox' => Bridge\Smsbox\SmsboxTransportFactory::class,
110+
'sms-sluzba' => Bridge\SmsSluzba\SmsSluzbaTransportFactory::class,
110111
];
111112

112113
foreach ($texterFactories as $name => $class) {
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
sms-sluzba.cz Notifier
2+
======================
3+
4+
Provides [sms-sluzba.cz](https://www.sms-sluzba.cz/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
MAILER_DSN=sms-sluzba://USERNAME:PASSWORD@default
11+
```
12+
13+
where:
14+
- `USERNAME` is your sms-sluzba.cz login
15+
- `PASSWORD` is your sms-sluzba.cz password
16+
17+
Resources
18+
---------
19+
20+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
21+
* [Report issues](https://github.com/symfony/symfony/issues) and
22+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
23+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\SmsSluzba;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
final class SmsSluzbaOptions implements MessageOptionsInterface
17+
{
18+
public function __construct(
19+
private array $options = [],
20+
) {
21+
}
22+
23+
public function toArray(): array
24+
{
25+
return $this->options;
26+
}
27+
28+
public function getRecipientId(): ?string
29+
{
30+
return null;
31+
}
32+
33+
/**
34+
* @return $this
35+
*/
36+
public function sendAt(\DateTime $sendAt): static
37+
{
38+
$sendAt->setTimezone(new \DateTimeZone('Europe/Prague'));
39+
40+
$this->options['send_at'] = $sendAt->format('YmdHis');
41+
42+
return $this;
43+
}
44+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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\SmsSluzba;
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\Component\Serializer\Encoder\XmlEncoder;
21+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
23+
use Symfony\Contracts\HttpClient\HttpClientInterface;
24+
25+
/**
26+
* @author Dennis Fridrich <fridrich.dennis@gmail.com>
27+
*/
28+
final class SmsSluzbaTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'smsgateapi.sms-sluzba.cz';
31+
32+
public function __construct(
33+
#[\SensitiveParameter]
34+
private string $username,
35+
#[\SensitiveParameter]
36+
private string $password,
37+
HttpClientInterface $client = null,
38+
EventDispatcherInterface $dispatcher = null
39+
) {
40+
parent::__construct($client, $dispatcher);
41+
}
42+
43+
public function __toString(): string
44+
{
45+
return sprintf('sms-sluzba://%s', $this->getEndpoint());
46+
}
47+
48+
public function supports(MessageInterface $message): bool
49+
{
50+
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof SmsSluzbaOptions);
51+
}
52+
53+
protected function doSend(MessageInterface $message): SentMessage
54+
{
55+
if (!$message instanceof SmsMessage) {
56+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
57+
}
58+
59+
$endpoint = sprintf(
60+
'https://%s/apixml30/receiver?login=%s&password=%s',
61+
$this->getEndpoint(),
62+
$this->username,
63+
$this->password
64+
);
65+
66+
$options = $message->getOptions()?->toArray() ?? [];
67+
68+
$response = $this->client->request('POST', $endpoint, [
69+
'headers' => [
70+
'Content-Type' => 'text/xml',
71+
],
72+
'body' => [
73+
'outgoing_message' => [
74+
'dr_request' => 20, // 0 = delivery report is not required; 20 = delivery report is required
75+
'recipient' => $message->getPhone(),
76+
'text' => $message->getSubject(),
77+
'send_at' => $options['send_at'] ?? null,
78+
],
79+
],
80+
]);
81+
82+
try {
83+
$response->getStatusCode();
84+
} catch (TransportExceptionInterface $e) {
85+
throw new TransportException('Could not reach the remote sms-sluzba.cz server.', $response, 0, $e);
86+
}
87+
88+
$xmlEncoder = new XmlEncoder();
89+
$responseXml = $xmlEncoder->decode($response->getContent(), 'xml');
90+
91+
if (isset($responseXml['message']) && \is_string($responseXml['message'])) {
92+
throw new TransportException(sprintf('Unable to send the SMS: "%s" (%s).', $responseXml['message'], (int) substr($responseXml['id'], 0, 3)), $response);
93+
}
94+
95+
$sentMessage = new SentMessage($message, (string) $this);
96+
$sentMessage->setMessageId($responseXml['message']['id']);
97+
98+
return $sentMessage;
99+
}
100+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\SmsSluzba;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
18+
/**
19+
* @author Dennis Fridrich <fridrich.dennis@gmail.com>
20+
*/
21+
final class SmsSluzbaTransportFactory extends AbstractTransportFactory
22+
{
23+
public function create(Dsn $dsn): SmsSluzbaTransport
24+
{
25+
$scheme = $dsn->getScheme();
26+
27+
if ('sms-sluzba' !== $scheme) {
28+
throw new UnsupportedSchemeException($dsn, 'sms-sluzba', $this->getSupportedSchemes());
29+
}
30+
31+
$username = $this->getUser($dsn);
32+
$password = $this->getPassword($dsn);
33+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
34+
$port = $dsn->getPort();
35+
36+
return (new SmsSluzbaTransport($username, $password, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
37+
}
38+
39+
protected function getSupportedSchemes(): array
40+
{
41+
return ['sms-sluzba'];
42+
}
43+
}

0 commit comments

Comments
 (0)