Skip to content

Commit

Permalink
feature #52976 [Notifier] Add sms-sluzba.cz bridge (dfridrich)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Dec 28, 2023
2 parents ce22a97 + 1b4e01e commit edfba7a
Show file tree
Hide file tree
Showing 16 changed files with 404 additions and 0 deletions.
Expand Up @@ -2754,6 +2754,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
NotifierBridge\Smsc\SmscTransportFactory::class => 'notifier.transport_factory.smsc',
NotifierBridge\SmsFactor\SmsFactorTransportFactory::class => 'notifier.transport_factory.sms-factor',
NotifierBridge\Smsmode\SmsmodeTransportFactory::class => 'notifier.transport_factory.smsmode',
NotifierBridge\SmsSluzba\SmsSluzbaTransportFactory::class => 'notifier.transport_factory.sms-sluzba',
NotifierBridge\SpotHit\SpotHitTransportFactory::class => 'notifier.transport_factory.spot-hit',
NotifierBridge\Telegram\TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
NotifierBridge\Telnyx\TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',
Expand Down
Expand Up @@ -107,6 +107,7 @@
'light-sms' => Bridge\LightSms\LightSmsTransportFactory::class,
'sms-biuras' => Bridge\SmsBiuras\SmsBiurasTransportFactory::class,
'smsbox' => Bridge\Smsbox\SmsboxTransportFactory::class,
'sms-sluzba' => Bridge\SmsSluzba\SmsSluzbaTransportFactory::class,
];

foreach ($texterFactories as $name => $class) {
Expand Down
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/SmsSluzba/.gitignore
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/SmsSluzba/CHANGELOG.md
@@ -0,0 +1,7 @@
CHANGELOG
=========

7.1
---

* Add the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/SmsSluzba/LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2023-present Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
23 changes: 23 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/SmsSluzba/README.md
@@ -0,0 +1,23 @@
sms-sluzba.cz Notifier
======================

Provides [sms-sluzba.cz](https://www.sms-sluzba.cz/) integration for Symfony Notifier.

DSN example
-----------

```
MAILER_DSN=sms-sluzba://USERNAME:PASSWORD@default
```

where:
- `USERNAME` is your sms-sluzba.cz login
- `PASSWORD` is your sms-sluzba.cz password

Resources
---------

* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\SmsSluzba;

use Symfony\Component\Notifier\Message\MessageOptionsInterface;

final class SmsSluzbaOptions implements MessageOptionsInterface
{
public function __construct(
private array $options = [],
) {
}

public function toArray(): array
{
return $this->options;
}

public function getRecipientId(): ?string
{
return null;
}

/**
* @return $this
*/
public function sendAt(\DateTime $sendAt): static
{
$sendAt->setTimezone(new \DateTimeZone('Europe/Prague'));

$this->options['send_at'] = $sendAt->format('YmdHis');

return $this;
}
}
100 changes: 100 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/SmsSluzba/SmsSluzbaTransport.php
@@ -0,0 +1,100 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\SmsSluzba;

use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Dennis Fridrich <fridrich.dennis@gmail.com>
*/
final class SmsSluzbaTransport extends AbstractTransport
{
protected const HOST = 'smsgateapi.sms-sluzba.cz';

public function __construct(
#[\SensitiveParameter]
private string $username,
#[\SensitiveParameter]
private string $password,
HttpClientInterface $client = null,
EventDispatcherInterface $dispatcher = null
) {
parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
return sprintf('sms-sluzba://%s', $this->getEndpoint());
}

public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof SmsSluzbaOptions);
}

protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof SmsMessage) {
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
}

$endpoint = sprintf(
'https://%s/apixml30/receiver?login=%s&password=%s',
$this->getEndpoint(),
$this->username,
$this->password
);

$options = $message->getOptions()?->toArray() ?? [];

$response = $this->client->request('POST', $endpoint, [
'headers' => [
'Content-Type' => 'text/xml',
],
'body' => [
'outgoing_message' => [
'dr_request' => 20, // 0 = delivery report is not required; 20 = delivery report is required
'recipient' => $message->getPhone(),
'text' => $message->getSubject(),
'send_at' => $options['send_at'] ?? null,
],
],
]);

try {
$response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote sms-sluzba.cz server.', $response, 0, $e);
}

$xmlEncoder = new XmlEncoder();
$responseXml = $xmlEncoder->decode($response->getContent(), 'xml');

if (isset($responseXml['message']) && \is_string($responseXml['message'])) {
throw new TransportException(sprintf('Unable to send the SMS: "%s" (%s).', $responseXml['message'], (int) substr($responseXml['id'], 0, 3)), $response);
}

$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId($responseXml['message']['id']);

return $sentMessage;
}
}
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\SmsSluzba;

use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;

/**
* @author Dennis Fridrich <fridrich.dennis@gmail.com>
*/
final class SmsSluzbaTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): SmsSluzbaTransport
{
$scheme = $dsn->getScheme();

if ('sms-sluzba' !== $scheme) {
throw new UnsupportedSchemeException($dsn, 'sms-sluzba', $this->getSupportedSchemes());
}

$username = $this->getUser($dsn);
$password = $this->getPassword($dsn);
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

return (new SmsSluzbaTransport($username, $password, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

protected function getSupportedSchemes(): array
{
return ['sms-sluzba'];
}
}
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\SmsSluzba\Tests;

use Symfony\Component\Notifier\Bridge\SmsSluzba\SmsSluzbaTransportFactory;
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;

final class SmsSluzbaTransportFactoryTest extends TransportFactoryTestCase
{
public function createFactory(): SmsSluzbaTransportFactory
{
return new SmsSluzbaTransportFactory();
}

public static function createProvider(): iterable
{
yield [
'sms-sluzba://host.test',
'sms-sluzba://username:password@host.test',
];
}

public static function incompleteDsnProvider(): iterable
{
yield 'missing username and password' => ['sms-sluzba://host'];
yield 'missing password' => ['sms-sluzba://username@host'];
}

public static function supportsProvider(): iterable
{
yield [true, 'sms-sluzba://username:password@default'];
yield [false, 'somethingElse://username:password@default'];
}

public static function unsupportedSchemeProvider(): iterable
{
yield ['somethingElse://username:password@default'];
}
}
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\SmsSluzba\Tests;

use Symfony\Component\Notifier\Bridge\SmsSluzba\SmsSluzbaTransport;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Test\TransportTestCase;
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final class SmsSluzbaTransportTest extends TransportTestCase
{
public static function createTransport(HttpClientInterface $client = null, string $from = null): SmsSluzbaTransport
{
return new SmsSluzbaTransport('username', 'password');
}

public static function toStringProvider(): iterable
{
yield ['sms-sluzba://smsgateapi.sms-sluzba.cz', self::createTransport()];
yield ['sms-sluzba://smsgateapi.sms-sluzba.cz', self::createTransport(null, 'TEST')];
}

public static function supportedMessagesProvider(): iterable
{
yield [new SmsMessage('608123456', 'Hello!')];
}

public static function unsupportedMessagesProvider(): iterable
{
yield [new ChatMessage('Hello!')];
yield [new DummyMessage()];
}
}
31 changes: 31 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/SmsSluzba/composer.json
@@ -0,0 +1,31 @@
{
"name": "symfony/sms-sluzba-notifier",
"type": "symfony-notifier-bridge",
"description": "Symfony sms-sluzba.cz Notifier Bridge",
"keywords": ["sms", "sms-sluzba.cz", "notifier"],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Dennis Fridrich",
"email": "fridrich.dennis@gmail.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"require": {
"php": ">=8.1",
"symfony/http-client": "^6.4|^7.1",
"symfony/notifier": "^7.1",
"symfony/serializer": "^6.4|^7.1"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\SmsSluzba\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev"
}

0 comments on commit edfba7a

Please sign in to comment.