8000 feature #48494 [Notifier] Add ClickSend notifier bridge (gnito-org) · symfony/symfony@6ec3943 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ec3943

Browse files
feature #48494 [Notifier] Add ClickSend notifier bridge (gnito-org)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [Notifier] Add ClickSend notifier bridge | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | | License | MIT | Doc PR | symfony/symfony-docs#17509 | Recipe | symfony/recipes#1159 <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- a3ca045 [Notifier] Add ClickSend notifier bridge
2 parents 0801f41 + a3ca045 commit 6ec3943

18 files changed

+646
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,6 +2704,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
27042704
NotifierBridge\Bandwidth\BandwidthTransportFactory::class => 'notifier.transport_factory.bandwidth',
27052705
NotifierBridge\Chatwork\ChatworkTransportFactory::class => 'notifier.transport_factory.chatwork',
27062706
NotifierBridge\Clickatell\ClickatellTransportFactory::class => 'notifier.transport_factory.clickatell',
2707+
NotifierBridge\ClickSend\ClickSendTransportFactory::class => 'notifier.transport_factory.click-send',
27072708
NotifierBridge\ContactEveryone\ContactEveryoneTransportFactory::class => 'notifier.transport_factory.contact-everyone',
27082709
NotifierBridge\Discord\DiscordTransportFactory::class => 'notifier.transport_factory.discord',
27092710
NotifierBridge\Engagespot\EngagespotTransportFactory::class => 'notifier.transport_factory.engagespot',

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,5 +279,9 @@
279279
->set('notifier.transport_factory.simple-textin', Bridge\SimpleTextin\SimpleTextinTransportFactory::class)
280280
->parent('notifier.transport_factory.abstract')
281281
->tag('texter.transport_factory')
282+
283+
->set('notifier.transport_factory.click-send', Bridge\ClickSend\ClickSendTransportFactory::class)
284+
->parent('notifier.transport_factory.abstract')
285+
->tag('texter.transport_factory')
282286
;
283287
};
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: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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\ClickSend;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author gnito-org <https://github.com/gnito-org>
18+
*/
19+
final class ClickSendOptions implements MessageOptionsInterface
20+
{
21+
private array $options;
22+
23+
public function __construct(array $options = [])
24+
{
25+
$this->options = $options;
26+
}
27+
28+
public function getCountry(): ?string
29+
{
30+
return $this->options['country'] ?? null;
31+
}
32+
33+
public function getCustomString(): ?string
34+
{
35+
return $this->options['custom_string'] ?? null;
36+
}
37+
38+
public function getFrom(): ?string
39+
{
40+
return $this->options['from'] ?? null;
41+
}
42+
43+
public function getFromEmail(): ?string
44+
{
45+
return $this->options['from_email'] ?? null;
46+
}
47+
48+
public function getListId(): ?string
49+
{
50+
return $this->options['list_id'] ?? null;
51+
}
52+
53+
public function getRecipientId(): ?string
54+
{
55+
return $this->options['recipient_id'] ?? null;
56+
}
< 111C /td>57+
58+
public function getSchedule(): ?int
59+
{
60+
return $this->options['schedule'] ?? null;
61+
}
62+
63+
public function getSource(): ?string
64+
{
65+
return $this->options['source'] ?? null;
66+
}
67+
68+
public function setCountry(string $country): self
69+
{
70+
$this->options['country'] = $country;
71+
72+
return $this;
73+
}
74+
75+
public function setCustomString(string $customString): self
76+
{
77+
$this->options['custom_string'] = $customString;
78+
79+
return $this;
80+
}
81+
82+
public function setFrom(string $from): self
83+
{
84+
$this->options['from'] = $from;
85+
86+
return $this;
87+
}
88+
89+
public function setFromEmail(string $fromEmail): self
90+
{
91+
$this->options['from_email'] = $fromEmail;
92+
93+
return $this;
94+
}
95+
96+
public function setListId(string $listId): self
97+
{
98+
$this->options['list_id'] = $listId;
99+
100+
return $this;
101+
}
102+
103+
public function setRecipientId(string $id): self
104+
{
105+
$this->options['recipient_id'] = $id;
106+
107+
return $this;
108+
}
109+
110+
public function setSchedule(int $schedule): self
111+
{
112+
$this->options['schedule'] = $schedule;
113+
114+
return $this;
115+
}
116+
117+
public function setSource(string $source): self
118+
{
119+
$this->options['source'] = $source;
120+
121+
return $this;
122+
}
123+
124+
public function toArray(): array
125+
{
126+
$options = $this->options;
127+
if (isset($options['recipient_id'])) {
128+
unset($options['recipient_id']);
129+
}
130+
131+
return $options;
132+
}
133+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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\ClickSend;
13+
14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SentMessage;
19+
use Symfony\Component\Notifier\Message\SmsMessage;
20+
use Symfony\Component\Notifier\Transport\AbstractTransport;
21+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
23+
use Symfony\Contracts\HttpClient\HttpClientInterface;
24+
25+
/**
26+
* @author gnito-org <https://github.com/gnito-org>
27+
*/
28+
final class ClickSendTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'rest.clicksend.com';
31+
32+
public function __construct(
33+
private readonly string $apiUsername,
34+
#[\SensitiveParameter] private readonly string $apiKey,
35+
private readonly ?string $from = null,
36+
private readonly ?string $source = null,
37+
private readonly ?string $listId = null,
38+
private readonly ?string $fromEmail = null,
39+
HttpClientInterface $client = null,
40+
EventDispatcherInterface $dispatcher = null
41+
) {
42+
parent::__construct($client, $dispatcher);
43+
}
44+
45+
public function __toString(): string
46+
{
47+
$queryParameters = [];
48+
if ($this->from) {
49+
$queryParameters['from'] = $this->from;
50+
}
51+
if ($this->source) {
52+
$queryParameters['source'] = $this->source;
53+
}
54+
if ($this->listId) {
55+
$queryParameters['list_id'] = $this->listId;
56+
}
57+
if ($this->fromEmail) {
58+
$queryParameters['from_email'] = $this->fromEmail;
59+
}
60+
61+
return sprintf('clicksend://%s', $this->getEndpoint()).($queryParameters ? '?'.http_build_query($queryParameters) : null);
62+
}
63+
64+
public function supports(MessageInterface $message): bool
65+
{
66+
return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof ClickSendOptions);
67+
}
68+
69+
/**
70+
* https://developers.clicksend.com/docs/rest/v3/#send-sms.
71+
*/
72+
protected function doSend(MessageInterface $message): SentMessage
73+
{
74+
if (!$message instanceof SmsMessage) {
75+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
76+
}
77+
78+
$endpoint = sprintf('https://%s/v3/sms/send', $this->getEndpoint());
79+
80+
$opts = $message->getOptions();
81+
$options = $opts ? $opts->toArray() : [];
82+
$options['body'] = $message->getSubject();
83+
84+
if (!isset($options['from']) && $this->from) {
85+
$options['from'] = $this->from;
86+
}
87+
88+
if (isset($options['from']) && !preg_match('/^[a-zA-Z0-9\s]{3,11}$/', $options['from']) && !preg_match('/^\+[1-9]\d{1,14}$/', $options['from'])) {
89+
throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number, shortcode, or alphanumeric sender ID.', $this->from));
90+
}
91+
92+
if (!isset($options['source']) && $this->source) {
93+
$options['source'] = $this->source;
94+
}
95+
96+
if (!isset($options['list_id']) && $this->listId) {
97+
$options['list_id'] = $this->listId;
98+
}
99+
100+
if (!isset($options['from_email']) && $this->fromEmail) {
101+
$options['from_email'] = urldecode($this->fromEmail);
102+
}
103+
104+
if ($options['list_id'] ?? false) {
105+
$options['to'] = $message->getPhone();
106+
}
107+
108+
$response = $this->client->request('POST', $endpoint, [
109+
'auth_basic' => $this->apiUsername.':'.$this->apiKey,
110+
'json' => array_filter($options),
111+
]);
112+
113+
try {
114+
$statusCode = $response->getStatusCode();
115+
} catch (TransportExceptionInterface $e) {
116+
throw new TransportException('Could not reach the remote ClickSend server.', $response, 0, $e);
117+
}
118+
119+
if (200 !== $statusCode) {
120+
$error = $response->getContent(false);
121+
throw new TransportException(sprintf('Unable to send the SMS - "%s".', $error ?: 'unknown failure'), $response);
122+
}
123+
124+
return new SentMessage($message, (string) $this);
125+
}
126+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\ClickSend;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
18+
/**
19+
* @author gnito-org <https://github.com/gnito-org>
20+
*/
21+
final class ClickSendTransportFactory extends AbstractTransportFactory
22+
{
23+
private const TRANSPORT_SCHEME = 'clicksend';
24+
25+
public function create(Dsn $dsn): ClickSendTransport
26+
{
27+
$scheme = $dsn->getScheme();
28+
if (self::TRANSPORT_SCHEME !== $scheme) {
29+
throw new UnsupportedSchemeException($dsn, self::TRANSPORT_SCHEME, $this->getSupportedSchemes());
30+
}
31+
$apiUsername = $this->getUser($dsn);
32+
$apiKey = $this->getPassword($dsn);
33+
$from = $dsn->getOption('from');
34+
$source = $dsn->getOption('source');
35+
$listId = $dsn->getOption('list_id');
36+
$fromEmail = $dsn->getOption('from_email');
37+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
38+
$port = $dsn->getPort();
39+
40+
return (new ClickSendTransport($apiUsername, $apiKey, $from, $source, $listId, $fromEmail, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
41+
}
42+
43+
protected function getSupportedSchemes(): array
44+
{
45+
return [self::TRANSPORT_SCHEME];
46+
}
47+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2022-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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
ClickSend Notifier
2+
==================
3+
4+
Provides [ClickSend](https://www.clicksend.com/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
CLICKSEND_DSN=clicksend://API_USERNAME:API_KEY@default?from=FROM&source=SOURCE&list_id=LIST_ID&from_email=FROM_EMAIL
11+
```
12+
13+
where:
14+
15+
- `API_USERNAME` is your ClickSend API username
16+
- `API_KEY` is your ClickSend API key
17+
- `FROM` is your sender (optional)
18+
- `SOURCE` is your source method of sending (optional)
19+
- `LIST_ID` is your recipient list ID (optional)
20+
- `FROM_EMAIL` is your from email where replies must be emailed (optional)
21+
22+
Resources
23+
---------
24+
25+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
26+
* [Report issues](https://github.com/symfony/symfony/issues) and
27+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
28+
in the [main Symfony repository](https://github.com/symfony/symfony)

0 commit comments

Comments
 (0)
0