Skip to content

Commit bad7e38

Browse files
committed
feature #50170 [Notifier] Added redlink notifier (plotkabytes)
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
2 parents 4530e95 + e0bd368 commit bad7e38

File tree

17 files changed

+486
-0
lines changed

17 files changed

+486
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2761,6 +2761,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
27612761
NotifierBridge\PagerDuty\PagerDutyTransportFactory::class => 'notifier.transport_factory.pager-duty',
27622762
NotifierBridge\Plivo\PlivoTransportFactory::class => 'notifier.transport_factory.plivo',
27632763
NotifierBridge\Pushover\PushoverTransportFactory::class => 'notifier.transport_factory.pushover',
2764+
NotifierBridge\Redlink\RedlinkTransportFactory::class => 'notifier.transport_factory.redlink',
27642765
NotifierBridge\RingCentral\RingCentralTransportFactory::class => 'notifier.transport_factory.ring-central',
27652766
NotifierBridge\RocketChat\RocketChatTransportFactory::class => 'notifier.transport_factory.rocket-chat',
27662767
NotifierBridge\Sendberry\SendberryTransportFactory::class => 'notifier.transport_factory.sendberry',

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,5 +295,9 @@
295295
->set('notifier.transport_factory.ntfy', Bridge\Ntfy\NtfyTransportFactory::class)
296296
->parent('notifier.transport_factory.abstract')
297297
->tag('texter.transport_factory')
298+
299+
->set('notifier.transport_factory.redlink', Bridge\Redlink\RedlinkTransportFactory::class)
300+
->parent('notifier.transport_factory.abstract')
301+
->tag('texter.transport_factory')
298302
;
299303
};
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+
6.3
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Redlink Notifier
2+
=================
3+
4+
Provides [Redlink](https://redlink.pl) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
REDLINK_DSN=redlink://API_TOKEN:APP_TOKEN@default?from=SENDER_NAME&version=VERSION
11+
```
12+
13+
where:
14+
15+
- `API_TOKEN` is your user API token, you can get it from the user dashboard
16+
- `APP_TOKEN` is your application's API token
17+
- `SENDER_NAME` is sender ID that was previously added through the user dashboard
18+
- `VERSION` is API version that you want to use, ex. v2.1, optional
19+
20+
Resources
21+
---------
22+
23+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
24+
* [Report issues](https://github.com/symfony/symfony/issues) and
25+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
26+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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\Redlink;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author Mateusz Żyła <https://github.com/plotkabytes>
18+
*/
19+
final class RedlinkOptions implements MessageOptionsInterface
20+
{
21+
public function __construct(protected array $options = [])
22+
{
23+
}
24+
25+
public function toArray(): array
26+
{
27+
return array_filter($this->options);
28+
}
29+
30+
public function getRecipientId(): ?string
31+
{
32+
return $this->options['externalId'] ?? null;
33+
}
34+
35+
/**
36+
* @return $this
37+
*/
38+
public function validity(int $validity): static
39+
{
40+
$this->options['validity'] = $validity;
41+
42+
return $this;
43+
}
44+
45+
/**
46+
* @return $this
47+
*/
48+
public function scheduleTime(int $scheduleTime): static
49+
{
50+
$this->options['scheduleTime'] = $scheduleTime;
51+
52+
return $this;
53+
}
54+
55+
/**
56+
* @return $this
57+
*/
58+
public function type(int $type): static
59+
{
60+
$this->options['type'] = $type;
61+
62+
return $this;
63+
}
64+
65+
/**
66+
* @return $this
67+
*/
68+
public function shortLink(bool $shortLink): static
69+
{
70+
$this->options['shortLink'] = $shortLink;
71+
72+
return $this;
73+
}
74+
75+
/**
76+
* @return $this
77+
*/
78+
public function webhookUrl(string $webhookUrl): static
79+
{
80+
$this->options['webhookUrl'] = $webhookUrl;
81+
82+
return $this;
83+
}
84+
85+
/**
86+
* @return $this
87+
*/
88+
public function externalId(string $externalId): static
89+
{
90+
$this->options['externalId'] = $externalId;
91+
92+
return $this;
93+
}
94+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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\Redlink;
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\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @author Mateusz Żyła <https://github.com/plotkabytes>
26+
*/
27+
final class RedlinkTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'api.redlink.pl';
30+
31+
public function __construct(
32+
#[\SensitiveParameter]
33+
private readonly string $apiToken,
34+
#[\SensitiveParameter]
35+
private readonly string $appToken,
36+
private readonly ?string $from,
37+
private readonly ?string $version,
38+
HttpClientInterface $client = null,
39+
EventDispatcherInterface $dispatcher = null,
40+
) {
41+
parent::__construct($client, $dispatcher);
42+
}
43+
44+
public function supports(MessageInterface $message): bool
45+
{
46+
return $message instanceof SmsMessage;
47+
}
48+
49+
public function __toString(): string
50+
{
51+
return sprintf(
52+
'redlink://%s?from=%s&version=%s',
53+
$this->getEndpoint(),
54+
$this->from,
55+
$this->version
56+
);
57+
}
58+
59+
protected function doSend(MessageInterface $message): SentMessage
60+
{
61+
if (!$message instanceof SmsMessage) {
62+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
63+
}
64+
65+
$options = ($opts = $message->getOptions()) ? $opts->toArray() : [];
66+
67+
$from = $message->getFrom() ?: $this->from;
68+
69+
$endpoint = sprintf('https://%s/%s/sms', $this->getEndpoint(), $this->version);
70+
71+
$response = $this->client->request('POST', $endpoint, [
72+
'headers' => [
73+
'Authorization' => $this->apiToken,
74+
'Application-Key' => $this->appToken,
75+
],
76+
'json' => array_merge([
77+
'sender' => $from,
78+
'message' => $message->getSubject(),
79+
'phoneNumbers' => [
80+
$message->getPhone(),
81+
],
82+
], array_filter($options)),
83+
]);
84+
85+
try {
86+
$statusCode = $response->getStatusCode();
87+
} catch (TransportExceptionInterface $e) {
88+
throw new TransportException('Could not reach the remote Redlink server.', $response, 0, $e);
89+
}
90+
91+
$content = $response->toArray(false);
92+
93+
if (200 !== $statusCode) {
94+
$requestUniqueIdentifier = $content['meta']['uniqId'] ?? '';
95+
96+
$errorMessage = $content['errors'][0]['message'] ?? '';
97+
98+
throw new TransportException(sprintf('Unable to send the SMS: '.$errorMessage.'. UniqId: (%s).', $requestUniqueIdentifier), $response);
99+
}
100+
101+
$messageId = $content['data'][0]['externalId'] ?? '';
102+
103+
$sentMessage = new SentMessage($message, (string) $this);
104+
105+
$sentMessage->setMessageId($messageId);
106+
107+
return $sentMessage;
108+
}
109+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\Redlink;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @author Mateusz Żyła <https://github.com/plotkabytes>
21+
*/
22+
final class RedlinkTransportFactory extends AbstractTransportFactory
23+
{
24+
public function create(Dsn $dsn): TransportInterface
25+
{
26+
if ('redlink' !== $dsn->getScheme()) {
27+
throw new UnsupportedSchemeException($dsn, 'redlink', $this->getSupportedSchemes());
28+
}
29+
30+
$apiKey = $dsn->getUser();
31+
$appToken = $dsn->getPassword();
32+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
33+
$port = $dsn->getPort();
34+
35+
$from = $dsn->getRequiredOption('from');
36+
$version = $dsn->getRequiredOption('version');
37+
38+
return (new RedlinkTransport($apiKey, $appToken, $from, $version, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
39+
}
40+
41+
protected function getSupportedSchemes(): array
42+
{
43+
return ['redlink'];
44+
}
45+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy