Skip to content

Commit 032a4d5

Browse files
plotkabytesm-zyla
authored andcommitted
Added redlink notifier
1 parent 1f8c592 commit 032a4d5

File tree

12 files changed

+478
-0
lines changed

12 files changed

+478
-0
lines changed
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 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: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
4+
/*
5+
* This file is part of the Symfony package.
6+
*
7+
* (c) Fabien Potencier <fabien@symfony.com>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace Symfony\Component\Notifier\Bridge\Redlink;
14+
15+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
16+
17+
/**
18+
* @author Mateusz Żyła <https://github.com/plotkabytes>
19+
*/
20+
final class RedlinkOptions implements MessageOptionsInterface
21+
{
22+
public function __construct(protected array $options = [])
23+
{
24+
}
25+
26+
public function toArray(): array
27+
{
28+
return array_filter($this->options);
29+
}
30+
31+
public function getRecipientId(): ?string
32+
{
33+
return $this->options['externalId'] ?? null;
34+
}
35+
36+
/**
37+
* @return $this
38+
*/
39+
public function validity(int $validity): static
40+
{
41+
$this->options['validity'] = $validity;
42+
43+
return $this;
44+
}
45+
46+
/**
47+
* @return $this
48+
*/
49+
public function scheduleTime(int $scheduleTime): static
50+
{
51+
$this->options['scheduleTime'] = $scheduleTime;
52+
53+
return $this;
54+
}
55+
56+
/**
57+
* @return $this
58+
*/
59+
public function type(int $type): static
60+
{
61+
$this->options['type'] = $type;
62+
63+
return $this;
64+
}
65+
66+
/**
67+
* @return $this
68+
*/
69+
public function shortLink(bool $shortLink): static
70+
{
71+
$this->options['shortLink'] = $shortLink;
72+
73+
return $this;
74+
}
75+
76+
/**
77+
* @return $this
78+
*/
79+
public function webhookUrl(string $webhookUrl): static
80+
{
81+
$this->options['webhookUrl'] = $webhookUrl;
82+
83+
return $this;
84+
}
85+
86+
/**
87+
* @return $this
88+
*/
89+
public function externalId(string $externalId): static
90+
{
91+
$this->options['externalId'] = $externalId;
92+
93+
return $this;
94+
}
95+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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] private readonly string $apiToken,
33+
#[\SensitiveParameter] private readonly string $appToken,
34+
private readonly ?string $from,
35+
private readonly ?string $version,
36+
HttpClientInterface $client = null,
37+
EventDispatcherInterface $dispatcher = null,
38+
)
39+
{
40+
parent::__construct($client, $dispatcher);
41+
}
42+
43+
public function supports(MessageInterface $message): bool
44+
{
45+
return $message instanceof SmsMessage;
46+
}
47+
48+
public function __toString(): string
49+
{
50+
return sprintf(
51+
'redlink://%s:%s@%s?from=%s&version=%s',
52+
$this->apiToken,
53+
$this->appToken,
54+
$this->getEndpoint(),
55+
$this->from,
56+
$this->version
57+
);
58+
}
59+
60+
protected function doSend(MessageInterface $message): SentMessage
61+
{
62+
if (!$message instanceof SmsMessage) {
63+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
64+
}
65+
66+
$options = ($opts = $message->getOptions()) ? $opts->toArray() : [];
67+
68+
$from = $message->getFrom() ?: $this->from;
69+
70+
$endpoint = sprintf('https://%s/%s/sms', $this->getEndpoint(), $this->version);
71+
72+
$response = $this->client->request('POST', $endpoint, [
73+
'headers' => [
74+
'Authorization' => $this->apiToken,
75+
'Application-Key' => $this->appToken
76+
],
77+
'json' => array_merge([
78+
'sender' => $from,
79+
'message' => $message->getSubject(),
80+
'phoneNumbers' => [
81+
$message->getPhone()
82+
],
83+
84+
], array_filter($options)),
85+
]);
86+
87+
try {
88+
$statusCode = $response->getStatusCode();
89+
} catch (TransportExceptionInterface $e) {
90+
throw new TransportException('Could not reach the remote Redlink server.', $response, 0, $e);
91+
}
92+
93+
if (200 !== $statusCode) {
94+
95+
$content = $response->toArray(false);
96+
97+
$requestUniqueIdentifier = $content['meta']['uniqId'] ?? '';
98+
99+
$errorMessage = $content['errors'][0]['message'] ?? '';
100+
101+
throw new TransportException(sprintf('Unable to send the SMS: ' . $errorMessage . '. UniqId: (%s).', $requestUniqueIdentifier), $response);
102+
}
103+
104+
$messageId = $content['data'][0]['externalId'] ?? '';
105+
106+
$sentMessage = new SentMessage($message, (string)$this);
107+
108+
$sentMessage->setMessageId($messageId);
109+
110+
return $sentMessage;
111+
}
112+
}
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+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Redlink\RedlinkTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
17+
final class RedlinkTransportFactoryTest extends TransportFactoryTestCase
18+
{
19+
public function createFactory(): RedlinkTransportFactory
20+
{
21+
return new RedlinkTransportFactory();
22+
}
23+
24+
public static function createProvider(): iterable
25+
{
26+
yield [
27+
'redlink://aaaaa:bbbbbb@api.redlink.pl?from=TEST&version=v2.1',
28+
'redlink://aaaaa:bbbbbb@default?from=TEST&version=v2.1',
29+
];
30+
}
31+
32+
public static function supportsProvider(): iterable
33+
{
34+
yield [true, 'redlink://aaaaa:bbbbbb@default?from=TEST'];
35+
yield [false, 'somethingElse://aaaaa:bbbbbb@default?from=TEST'];
36+
}
37+
38+
public static function missingRequiredOptionProvider(): iterable
39+
{
40+
yield 'missing option: from' => ['redlink://apiToken:appToken@default'];
41+
yield 'missing option: version' => ['redlink://apiToken:appToken@default?from=TEST'];
42+
}
43+
44+
public static function unsupportedSchemeProvider(): iterable
45+
{
46+
yield ['somethingElse://apiToken:appToken@default?from=FROM&version=FROM'];
47+
yield ['somethingElse://apiToken:appToken@default?from=FROM'];
48+
yield ['somethingElse://apiToken:appToken@default'];
49+
}
50+
}

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