Skip to content

Commit

Permalink
Added redlink notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
m-zyla committed Apr 27, 2023
1 parent 1f8c592 commit 5e46f19
Show file tree
Hide file tree
Showing 12 changed files with 478 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Redlink/.gitattributes
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/Redlink/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Redlink/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

6.3
---

* Add the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Redlink/LICENSE
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 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)
95 changes: 95 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Redlink/RedlinkOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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;
}
}
112 changes: 112 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Redlink/RedlinkTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?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:%s@%s?from=%s&version=%s',
$this->apiToken,
$this->appToken,
$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);
}

if (200 !== $statusCode) {

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

$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;
}
}
Original file line number Diff line number Diff line change
@@ -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'];
}
}
Original file line number Diff line number Diff line change
@@ -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://aaaaa:bbbbbb@api.redlink.pl?from=TEST&version=v2.1',
'redlink://aaaaa:bbbbbb@default?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 5e46f19

Please sign in to comment.