Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Notifier] [Bridge] [KazInfoTeh] added the bridge #44360

Merged
merged 1 commit into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
use Symfony\Component\Notifier\Bridge\KazInfoTeh\KazInfoTehTransportFactory;
use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransportFactory;
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
use Symfony\Component\Notifier\Bridge\Mailjet\MailjetTransportFactory as MailjetNotifierTransportFactory;
Expand Down Expand Up @@ -2429,6 +2430,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
GoogleChatTransportFactory::class => 'notifier.transport_factory.google-chat',
InfobipTransportFactory::class => 'notifier.transport_factory.infobip',
IqsmsTransportFactory::class => 'notifier.transport_factory.iqsms',
KazInfoTehTransportFactory::class => 'notifier.transport_factory.kaz-info-teh',
LightSmsTransportFactory::class => 'notifier.transport_factory.light-sms',
LinkedInTransportFactory::class => 'notifier.transport_factory.linked-in',
MailjetNotifierTransportFactory::class => 'notifier.transport_factory.mailjet',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
use Symfony\Component\Notifier\Bridge\KazInfoTeh\KazInfoTehTransportFactory;
use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransportFactory;
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
use Symfony\Component\Notifier\Bridge\Mailjet\MailjetTransportFactory;
Expand Down Expand Up @@ -251,5 +252,9 @@
->set('notifier.transport_factory.expo', ExpoTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.kaz-info-teh', KazInfoTehTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')
;
};
Original file line number Diff line number Diff line change
@@ -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/KazInfoTeh/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

6.1
---

* Add the bridge
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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\KazInfoTeh;

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\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Egor Taranov <dev@taranovegor.com>
*/
class KazInfoTehTransport extends AbstractTransport
{
protected const HOST = 'kazinfoteh.org';

private string $username;
private string $password;
private string $sender;

public function __construct(string $username, string $password, string $sender, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
$this->username = $username;
$this->password = $password;
$this->sender = $sender;

parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
return sprintf('kaz-info-teh://%s?sender=%s', $this->getEndpoint(), $this->sender);
}

public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage
&& str_starts_with($message->getPhone(), '77')
&& 11 === \strlen($message->getPhone())
;
}

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

$endpoint = sprintf('http://%s/api', $this->getEndpoint());
taranovegor marked this conversation as resolved.
Show resolved Hide resolved
$response = $this->client->request('POST', $endpoint, [
'query' => [
'action' => 'sendmessage',
'username' => $this->username,
'password' => $this->password,
'recipient' => $message->getPhone(),
'messagetype' => 'SMS:TEXT',
'originator' => $this->sender,
'messagedata' => $message->getSubject(),
],
]);

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

try {
$content = new \SimpleXMLElement($response->getContent(false));
} catch (\Exception $e) {
throw new TransportException('Unable to send the SMS: "Couldn\'t read response".', $response, previous: $e);
}

if (200 !== $statusCode || '0' !== (string) $content->statuscode) {
$error = (string) $content->statusmessage ?: $content->errormessage ?: 'unknown error';

throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error), $response);
}

return new SentMessage($message, (string) $this);
}

protected function getEndpoint(): string
{
$endpoint = $this->host ?: $this->getDefaultHost();
if ($this->getDefaultHost() === $endpoint && null === $this->port) {
$endpoint .= ':9507';
} elseif (null !== $this->port) {
$endpoint .= ':'.$this->port;
}

return $endpoint;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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\KazInfoTeh;

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

/**
* @author Egor Taranov <dev@taranovegor.com>
*/
final class KazInfoTehTransportFactory extends AbstractTransportFactory
{
/**
* {@inheritdoc}
*/
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();

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

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

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

/**
* {@inheritdoc}
*/
protected function getSupportedSchemes(): array
{
return ['kaz-info-teh'];
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/KazInfoTeh/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2021-2022 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.
24 changes: 24 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/KazInfoTeh/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
KazInfoTeh Notifier
===============

Provides [KazInfoTeh](https://kazinfoteh.kz/) integration for Symfony Notifier.

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

```
KAZ_INFO_TEH_DSN=kaz-info-teh://USERNAME:PASSWORD@default?sender=FROM
```

where:
- `USERNAME` is your login from account
- `PASSWORD` is your password from account
- `FROM` is the alpha name

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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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\KazInfoTeh\Tests;

use Symfony\Component\Notifier\Bridge\KazInfoTeh\KazInfoTehTransportFactory;
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;

/**
* @author Egor Taranov <dev@taranovegor.com>
*/
final class KazInfoTehTransportFactoryTest extends TransportFactoryTestCase
{
public function createFactory(): KazInfoTehTransportFactory
{
return new KazInfoTehTransportFactory();
}

public function createProvider(): iterable
{
yield [
'kaz-info-teh://kazinfoteh.org:9507?sender=symfony',
'kaz-info-teh://username:password@default?sender=symfony',
];

yield [
'kaz-info-teh://host.test?sender=Symfony',
'kaz-info-teh://username:password@host.test?sender=Symfony',
];
}

public function supportsProvider(): iterable
{
yield [true, 'kaz-info-teh://username:password@default?sender=Symfony'];
yield [false, 'somethingElse://username:password@default?sender=Symfony'];
}

public function missingRequiredOptionProvider(): iterable
{
yield 'missing option: sender' => ['kaz-info-teh://username:password@default'];
}

public function incompleteDsnProvider(): iterable
{
yield 'missing username' => ['kaz-info-teh://default?sender=0611223344'];
yield 'missing password' => ['kaz-info-teh://username@default?sender=0611223344'];
}

public function unsupportedSchemeProvider(): iterable
{
yield ['somethingElse://username:password@default?sender=acme'];
yield ['somethingElse://username:password@default'];
yield ['somethingElse://default'];
}
}