Skip to content

Commit

Permalink
feature #50170 [Notifier] Added redlink notifier (plotkabytes)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 6.4 branch.

Discussion
----------

[Notifier] Added redlink notifier

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        | symfony/symfony-docs#18263

Added [Redlink](https://docs.redlink.pl) notifier bridge

Commits
-------

e0bd368 Added redlink notifier
  • Loading branch information
fabpot committed Jun 21, 2023
2 parents 4530e95 + e0bd368 commit bad7e38
Show file tree
Hide file tree
Showing 17 changed files with 486 additions and 0 deletions.
Expand Up @@ -2761,6 +2761,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
NotifierBridge\PagerDuty\PagerDutyTransportFactory::class => 'notifier.transport_factory.pager-duty',
NotifierBridge\Plivo\PlivoTransportFactory::class => 'notifier.transport_factory.plivo',
NotifierBridge\Pushover\PushoverTransportFactory::class => 'notifier.transport_factory.pushover',
NotifierBridge\Redlink\RedlinkTransportFactory::class => 'notifier.transport_factory.redlink',
NotifierBridge\RingCentral\RingCentralTransportFactory::class => 'notifier.transport_factory.ring-central',
NotifierBridge\RocketChat\RocketChatTransportFactory::class => 'notifier.transport_factory.rocket-chat',
NotifierBridge\Sendberry\SendberryTransportFactory::class => 'notifier.transport_factory.sendberry',
Expand Down
Expand Up @@ -295,5 +295,9 @@
->set('notifier.transport_factory.ntfy', Bridge\Ntfy\NtfyTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.redlink', Bridge\Redlink\RedlinkTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')
;
};
4 changes: 4 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Redlink/.gitattributes
@@ -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/Redlink/.gitignore
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Redlink/CHANGELOG.md
@@ -0,0 +1,7 @@
CHANGELOG
=========

6.3
---

* Add the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Redlink/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.
26 changes: 26 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Redlink/README.md
@@ -0,0 +1,26 @@
Redlink Notifier
=================

Provides [Redlink](https://redlink.pl) integration for Symfony Notifier.

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

```
REDLINK_DSN=redlink://API_TOKEN:APP_TOKEN@default?from=SENDER_NAME&version=VERSION
```

where:

- `API_TOKEN` is your user API token, you can get it from the user dashboard
- `APP_TOKEN` is your application's API token
- `SENDER_NAME` is sender ID that was previously added through the user dashboard
- `VERSION` is API version that you want to use, ex. v2.1, optional

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)
94 changes: 94 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Redlink/RedlinkOptions.php
@@ -0,0 +1,94 @@
<?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\Redlink;

use Symfony\Component\Notifier\Message\MessageOptionsInterface;

/**
* @author Mateusz Żyła <https://github.com/plotkabytes>
*/
final class RedlinkOptions implements MessageOptionsInterface
{
public function __construct(protected array $options = [])
{
}

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

public function getRecipientId(): ?string
{
return $this->options['externalId'] ?? null;
}

/**
* @return $this
*/
public function validity(int $validity): static
{
$this->options['validity'] = $validity;

return $this;
}

/**
* @return $this
*/
public function scheduleTime(int $scheduleTime): static
{
$this->options['scheduleTime'] = $scheduleTime;

return $this;
}

/**
* @return $this
*/
public function type(int $type): static
{
$this->options['type'] = $type;

return $this;
}

/**
* @return $this
*/
public function shortLink(bool $shortLink): static
{
$this->options['shortLink'] = $shortLink;

return $this;
}

/**
* @return $this
*/
public function webhookUrl(string $webhookUrl): static
{
$this->options['webhookUrl'] = $webhookUrl;

return $this;
}

/**
* @return $this
*/
public function externalId(string $externalId): static
{
$this->options['externalId'] = $externalId;

return $this;
}
}
109 changes: 109 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Redlink/RedlinkTransport.php
@@ -0,0 +1,109 @@
<?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\Redlink;

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 Mateusz Żyła <https://github.com/plotkabytes>
*/
final class RedlinkTransport extends AbstractTransport
{
protected const HOST = 'api.redlink.pl';

public function __construct(
#[\SensitiveParameter]
private readonly string $apiToken,
#[\SensitiveParameter]
private readonly string $appToken,
private readonly ?string $from,
private readonly ?string $version,
HttpClientInterface $client = null,
EventDispatcherInterface $dispatcher = null,
) {
parent::__construct($client, $dispatcher);
}

public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage;
}

public function __toString(): string
{
return sprintf(
'redlink://%s?from=%s&version=%s',
$this->getEndpoint(),
$this->from,
$this->version
);
}

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

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

$from = $message->getFrom() ?: $this->from;

$endpoint = sprintf('https://%s/%s/sms', $this->getEndpoint(), $this->version);

$response = $this->client->request('POST', $endpoint, [
'headers' => [
'Authorization' => $this->apiToken,
'Application-Key' => $this->appToken,
],
'json' => array_merge([
'sender' => $from,
'message' => $message->getSubject(),
'phoneNumbers' => [
$message->getPhone(),
],
], array_filter($options)),
]);

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

$content = $response->toArray(false);

if (200 !== $statusCode) {
$requestUniqueIdentifier = $content['meta']['uniqId'] ?? '';

$errorMessage = $content['errors'][0]['message'] ?? '';

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

$messageId = $content['data'][0]['externalId'] ?? '';

$sentMessage = new SentMessage($message, (string) $this);

$sentMessage->setMessageId($messageId);

return $sentMessage;
}
}
@@ -0,0 +1,45 @@
<?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\Redlink;

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 Mateusz Żyła <https://github.com/plotkabytes>
*/
final class RedlinkTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
if ('redlink' !== $dsn->getScheme()) {
throw new UnsupportedSchemeException($dsn, 'redlink', $this->getSupportedSchemes());
}

$apiKey = $dsn->getUser();
$appToken = $dsn->getPassword();
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

$from = $dsn->getRequiredOption('from');
$version = $dsn->getRequiredOption('version');

return (new RedlinkTransport($apiKey, $appToken, $from, $version, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

protected function getSupportedSchemes(): array
{
return ['redlink'];
}
}
@@ -0,0 +1,50 @@
<?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\Redlink\Tests;

use Symfony\Component\Notifier\Bridge\Redlink\RedlinkTransportFactory;
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;

final class RedlinkTransportFactoryTest extends TransportFactoryTestCase
{
public function createFactory(): RedlinkTransportFactory
{
return new RedlinkTransportFactory();
}

public static function createProvider(): iterable
{
yield [
'redlink://api.redlink.pl?from=TEST&version=v2.1',
'redlink://aaaaa:bbbbbb@api.redlink.pl?from=TEST&version=v2.1',
];
}

public static function supportsProvider(): iterable
{
yield [true, 'redlink://aaaaa:bbbbbb@default?from=TEST'];
yield [false, 'somethingElse://aaaaa:bbbbbb@default?from=TEST'];
}

public static function missingRequiredOptionProvider(): iterable
{
yield 'missing option: from' => ['redlink://apiToken:appToken@default'];
yield 'missing option: version' => ['redlink://apiToken:appToken@default?from=TEST'];
}

public static function unsupportedSchemeProvider(): iterable
{
yield ['somethingElse://apiToken:appToken@default?from=FROM&version=FROM'];
yield ['somethingElse://apiToken:appToken@default?from=FROM'];
yield ['somethingElse://apiToken:appToken@default'];
}
}

0 comments on commit bad7e38

Please sign in to comment.